Skip to content

Commit ac8736f

Browse files
rootautophagy
authored andcommitted
Add backoff_factor in connection to configure retry interval
1 parent 768b7fe commit ac8736f

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

CHANGES.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ Changes for crate
55
Unreleased
66
==========
77

8+
- Added ``backoff_factor`` in connection to configure retry interval.
9+
810
- Added official Python 3.8 support.
911

1012
- Made it so that the SQLAlchemy dialect is now aware of the return type of the

docs/connect.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,18 @@ the optional ``error_trace`` argument to ``True``, like so::
169169

170170
.. _authentication:
171171

172+
Backoff Factor
173+
..............
174+
175+
When attempting to make a request, the connection can be configured so that
176+
retries are made in increasing time intervals. This can be configured like so::
177+
178+
>>> connection = client.connect(..., backoff_factor=0.1)
179+
180+
If ``backoff_factor``is set to 0.1, then the delay between retries will be 0.0,
181+
0.1, 0.2, 0.4 etc. The maximum backoff factor cannot exceed 120 seconds and by
182+
default its value is 0.
183+
172184
Authentication
173185
==============
174186

src/crate/client/connection.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
class Connection(object):
3030

31-
def __init__(self, servers=None, timeout=None, client=None,
31+
def __init__(self, servers=None, timeout=None, backoff_factor=0, client=None,
3232
verify_ssl_cert=False, ca_cert=None, error_trace=False,
3333
cert_file=None, key_file=None, username=None, password=None,
3434
schema=None):
@@ -37,6 +37,7 @@ def __init__(self, servers=None, timeout=None, client=None,
3737
else:
3838
self.client = Client(servers,
3939
timeout=timeout,
40+
backoff_factor=backoff_factor,
4041
verify_ssl_cert=verify_ssl_cert,
4142
ca_cert=ca_cert,
4243
error_trace=error_trace,
@@ -103,6 +104,7 @@ def __exit__(self, *excs):
103104

104105
def connect(servers=None,
105106
timeout=None,
107+
backoff_factor=0,
106108
client=None,
107109
verify_ssl_cert=False,
108110
ca_cert=None,
@@ -140,12 +142,15 @@ def connect(servers=None,
140142
the username in the database.
141143
:param password:
142144
the password of the user in the database.
143-
145+
:param backoff_factor:
146+
(optional)
147+
define the retry interval for unreachable servers in seconds
144148
>>> connect(['host1:4200', 'host2:4200'])
145149
<Connection <Client ['http://host1:4200', 'http://host2:4200']>>
146150
"""
147151
return Connection(servers=servers,
148152
timeout=timeout,
153+
backoff_factor=backoff_factor,
149154
client=client,
150155
verify_ssl_cert=verify_ssl_cert,
151156
ca_cert=ca_cert,

src/crate/client/http.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ def request(self,
101101
username=None,
102102
password=None,
103103
schema=None,
104+
backoff_factor=0,
104105
**kwargs):
105106
"""Send a request
106107
@@ -130,7 +131,7 @@ def request(self,
130131
headers['Content-Type'] = 'application/json'
131132
kwargs['assert_same_host'] = False
132133
kwargs['redirect'] = False
133-
kwargs['retries'] = Retry(read=0)
134+
kwargs['retries'] = Retry(read=0, backoff_factor=backoff_factor)
134135
return self.pool.urlopen(
135136
method,
136137
path,
@@ -274,6 +275,7 @@ class Client(object):
274275
def __init__(self,
275276
servers=None,
276277
timeout=None,
278+
backoff_factor=0,
277279
verify_ssl_cert=False,
278280
ca_cert=None,
279281
error_trace=False,
@@ -290,6 +292,7 @@ def __init__(self,
290292
self._inactive_servers = []
291293
pool_kw = _pool_kw_args(verify_ssl_cert, ca_cert, cert_file, key_file)
292294
pool_kw['timeout'] = timeout
295+
self.backoff_factor = backoff_factor
293296
self.server_pool = {}
294297
self._update_server_pool(servers, **pool_kw)
295298
self._pool_kw = pool_kw
@@ -403,7 +406,14 @@ def _request(self, method, path, server=None, **kwargs):
403406
next_server = server or self._get_server()
404407
try:
405408
response = self.server_pool[next_server].request(
406-
method, path, username=self.username, password=self.password, schema=self.schema, **kwargs)
409+
method,
410+
path,
411+
username=self.username,
412+
password=self.password,
413+
backoff_factor=self.backoff_factor,
414+
schema=self.schema,
415+
**kwargs
416+
)
407417
redirect_location = response.get_redirect_location()
408418
if redirect_location and 300 <= response.status <= 308:
409419
redirect_server = _server_url(redirect_location)

0 commit comments

Comments
 (0)