-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtxwebretry.py
More file actions
75 lines (49 loc) · 2.13 KB
/
txwebretry.py
File metadata and controls
75 lines (49 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
''' txwebretry - retry mechanisms for web requests.
Utilities for automatically retrying web requests made with twisted.web.client.
Usage:
>>> # GET localhost:8080 up to 3 times with exponential backoff
>>> d = retry3_exponential(treq.get, 'http://localhost:8080')
>>> # GET localhost:8080 up to 5 times without delay
>>> d = retry5_immediate(treq.get, 'http://localhost:8080')
'''
from itertools import repeat
from twisted.internet.error import ConnectionRefusedError, \
ConnectingCancelledError, ConnectionDone, TimeoutError
from twisted.web.client import ResponseFailed
from txretry.retry import RetryingCall, simpleBackoffIterator
class Retry(object):
''' Defines a context for making retrying calls. '''
web_errors = [
ResponseFailed,
ConnectionRefusedError,
ConnectingCancelledError,
ConnectionDone,
TimeoutError]
def __init__(self, backoff_func, *backoff_args, **backoff_kwargs):
self.backoff_func = backoff_func
self.backoff_args = backoff_args
self.backoff_kwargs = backoff_kwargs
def __call__(self, f, *args, **kwargs):
retrying_call = RetryingCall(f, *args, **kwargs)
return retrying_call.start(
backoffIterator=self._backoff_iterator(),
failureTester=self._test_failure)
def _backoff_iterator(self):
return self.backoff_func(*self.backoff_args, **self.backoff_kwargs)
def _test_failure(self, failure):
if not failure.check(*self.web_errors):
return failure
def ImmediateRetry(attempts=3):
''' Returns a Retry context that will retry calls immediately. '''
return Retry(repeat, 0, attempts)
def ExponentialBackoffRetry(attempts=3):
''' Returns a Retry context that will retry calls using exponential
backoff. '''
return Retry(simpleBackoffIterator, attempts)
# For convenience
retry3_exponential = ExponentialBackoffRetry(3)
retry5_exponential = ExponentialBackoffRetry(5)
retry10_exponential = ExponentialBackoffRetry(10)
retry3_immediate = ImmediateRetry(3)
retry5_immediate = ImmediateRetry(5)
retry10_immediate = ImmediateRetry(10)