From b999abb9ea7e0a60a6ce0392fd997975fb49008c Mon Sep 17 00:00:00 2001 From: Victor Shyba Date: Wed, 10 Oct 2018 15:45:14 -0300 Subject: [PATCH] improve request cancelling and timeouts during switches --- torba/basenetwork.py | 10 ++++++++++ torba/constants.py | 2 ++ 2 files changed, 12 insertions(+) diff --git a/torba/basenetwork.py b/torba/basenetwork.py index eec68577d..452386120 100644 --- a/torba/basenetwork.py +++ b/torba/basenetwork.py @@ -6,9 +6,11 @@ from twisted.internet import defer, reactor, protocol from twisted.application.internet import ClientService, CancelledError from twisted.internet.endpoints import clientFromString from twisted.protocols.basic import LineOnlyReceiver +from twisted.python import failure from torba import __version__ from torba.stream import StreamController +from torba.constants import TIMEOUT log = logging.getLogger(__name__) @@ -69,7 +71,11 @@ class StratumClientProtocol(LineOnlyReceiver): log.warning("Error setting up socket: %s", err) def connectionLost(self, reason=None): + self.connected = 0 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): log.debug('received: %s', line) @@ -105,6 +111,10 @@ class StratumClientProtocol(LineOnlyReceiver): log.debug('sent: %s', message) self.sendLine(message.encode('latin-1')) 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 diff --git a/torba/constants.py b/torba/constants.py index c1497ac19..6c790f02c 100644 --- a/torba/constants.py +++ b/torba/constants.py @@ -2,3 +2,5 @@ NULL_HASH32 = b'\x00'*32 CENT = 1000000 COIN = 100*CENT + +TIMEOUT = 30.0