improve request cancelling and timeouts during switches

This commit is contained in:
Victor Shyba 2018-10-10 15:45:14 -03:00 committed by Lex Berezhny
parent 55f5c7491c
commit b999abb9ea
2 changed files with 12 additions and 0 deletions

View file

@ -6,9 +6,11 @@ from twisted.internet import defer, reactor, protocol
from twisted.application.internet import ClientService, CancelledError from twisted.application.internet import ClientService, CancelledError
from twisted.internet.endpoints import clientFromString from twisted.internet.endpoints import clientFromString
from twisted.protocols.basic import LineOnlyReceiver from twisted.protocols.basic import LineOnlyReceiver
from twisted.python import failure
from torba import __version__ from torba import __version__
from torba.stream import StreamController from torba.stream import StreamController
from torba.constants import TIMEOUT
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -69,7 +71,11 @@ class StratumClientProtocol(LineOnlyReceiver):
log.warning("Error setting up socket: %s", err) log.warning("Error setting up socket: %s", err)
def connectionLost(self, reason=None): def connectionLost(self, reason=None):
self.connected = 0
self.on_disconnected_controller.add(True) self.on_disconnected_controller.add(True)
for deferred in self.lookup_table.values():
if not deferred.called:
deferred.errback(TimeoutError("Connection dropped."))
def lineReceived(self, line): def lineReceived(self, line):
log.debug('received: %s', line) log.debug('received: %s', line)
@ -105,6 +111,10 @@ class StratumClientProtocol(LineOnlyReceiver):
log.debug('sent: %s', message) log.debug('sent: %s', message)
self.sendLine(message.encode('latin-1')) self.sendLine(message.encode('latin-1'))
d = self.lookup_table[message_id] = defer.Deferred() d = self.lookup_table[message_id] = defer.Deferred()
d.addTimeout(
TIMEOUT, reactor, onTimeoutCancel=lambda *_: failure.Failure(TimeoutError(
"Timeout: Stratum request for '%s' took more than %s seconds" % (method, TIMEOUT)))
)
return d return d

View file

@ -2,3 +2,5 @@ NULL_HASH32 = b'\x00'*32
CENT = 1000000 CENT = 1000000
COIN = 100*CENT COIN = 100*CENT
TIMEOUT = 30.0