commit 1c3a5067ae9a26fc5c537e956e8c0847ad85fe4a Author: Arturo Filastò art@fuffa.org Date: Thu Sep 12 15:00:31 2013 +0200
Implement retry logic inside of the oonibclient. --- ooni/oonibclient.py | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-)
diff --git a/ooni/oonibclient.py b/ooni/oonibclient.py index 24dc374..455aea8 100644 --- a/ooni/oonibclient.py +++ b/ooni/oonibclient.py @@ -42,26 +42,41 @@ class Collector(object): return False
class OONIBClient(object): + retries = 3 + def __init__(self, address): self.address = address self.agent = Agent(reactor, sockshost="127.0.0.1", socksport=config.tor.socks_port)
def _request(self, method, urn, genReceiver, bodyProducer=None): - finished = defer.Deferred() + attempts = 0
- uri = self.address + urn - headers = {} - d = self.agent.request(method, uri, bodyProducer=bodyProducer) + finished = defer.Deferred()
- @d.addCallback - def callback(response): - content_length = int(response.headers.getRawHeaders('content-length')[0]) - response.deliverBody(genReceiver(finished, content_length)) + def perform_request(): + uri = self.address + urn + headers = {} + d = self.agent.request(method, uri, bodyProducer=bodyProducer)
- @d.addErrback - def eb(err): - finished.errback(err) + @d.addCallback + def callback(response): + content_length = int(response.headers.getRawHeaders('content-length')[0]) + response.deliverBody(genReceiver(finished, content_length)) + + def errback(err, attempts): + # We we will recursively keep trying to perform a request until + # we have reached the retry count. + if attempts < self.retries: + log.err("Lookup failed. Retrying.") + attempts += 1 + perform_request() + else: + log.err("Failed. Giving up.") + finished.errback(err) + d.addErrback(errback, attempts) + + perform_request()
return finished
tor-commits@lists.torproject.org