Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
Changes for crate
=================


Unreleased
==========

- Added ``backoff_factor`` in connection to configure retry interval.

- Added official Python 3.8 support.

- Made it so that the SQLAlchemy dialect is now aware of the return type of the
Expand Down
11 changes: 11 additions & 0 deletions docs/connect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,17 @@ the optional ``error_trace`` argument to ``True``, like so::

.. _authentication:

Backoff Factor
..............

When attempting to make a request, the connection can be configured so that retries
are made at lengthening time intervals.This can be configured like so::

>>> connection = client.connect(..., backoff_factor=0.1)

If ``backoff_factor``is set to 0.1,then the delay between retries will be 0.0, 0.1, 0.2,
0.4 etc.The maximum backoff factor cannot exceed 120 seconds and by default its value is 0

Authentication
==============

Expand Down
11 changes: 8 additions & 3 deletions src/crate/client/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@


class Connection(object):

def __init__(self, servers=None, timeout=None, client=None,
def __init__(self, servers=None, timeout=None, backoff_factor=0, client=None,
verify_ssl_cert=False, ca_cert=None, error_trace=False,
cert_file=None, key_file=None, username=None, password=None,
schema=None):
Expand All @@ -37,6 +37,7 @@ def __init__(self, servers=None, timeout=None, client=None,
else:
self.client = Client(servers,
timeout=timeout,
backoff_factor=backoff_factor,
verify_ssl_cert=verify_ssl_cert,
ca_cert=ca_cert,
error_trace=error_trace,
Expand Down Expand Up @@ -103,6 +104,7 @@ def __exit__(self, *excs):

def connect(servers=None,
timeout=None,
backoff_factor=0,
client=None,
verify_ssl_cert=False,
ca_cert=None,
Expand Down Expand Up @@ -140,12 +142,15 @@ def connect(servers=None,
the username in the database.
:param password:
the password of the user in the database.

:param backoff_factor:
(optional)
define the retry interval for unreachable servers in seconds
>>> connect(['host1:4200', 'host2:4200'])
<Connection <Client ['http://host1:4200', 'http://host2:4200']>>
"""
return Connection(servers=servers,
timeout=timeout,
backoff_factor=backoff_factor,
client=client,
verify_ssl_cert=verify_ssl_cert,
ca_cert=ca_cert,
Expand Down
7 changes: 5 additions & 2 deletions src/crate/client/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ def request(self,
username=None,
password=None,
schema=None,
backoff_factor=0,
**kwargs):
"""Send a request

Expand Down Expand Up @@ -130,7 +131,7 @@ def request(self,
headers['Content-Type'] = 'application/json'
kwargs['assert_same_host'] = False
kwargs['redirect'] = False
kwargs['retries'] = Retry(read=0)
kwargs['retries'] = Retry(read=0,backoff_factor=backoff_factor)
return self.pool.urlopen(
method,
path,
Expand Down Expand Up @@ -274,6 +275,7 @@ class Client(object):
def __init__(self,
servers=None,
timeout=None,
backoff_factor=0,
verify_ssl_cert=False,
ca_cert=None,
error_trace=False,
Expand All @@ -290,6 +292,7 @@ def __init__(self,
self._inactive_servers = []
pool_kw = _pool_kw_args(verify_ssl_cert, ca_cert, cert_file, key_file)
pool_kw['timeout'] = timeout
self.backoff_factor = backoff_factor
self.server_pool = {}
self._update_server_pool(servers, **pool_kw)
self._pool_kw = pool_kw
Expand Down Expand Up @@ -403,7 +406,7 @@ def _request(self, method, path, server=None, **kwargs):
next_server = server or self._get_server()
try:
response = self.server_pool[next_server].request(
method, path, username=self.username, password=self.password, schema=self.schema, **kwargs)
method, path, username=self.username, password=self.password,backoff_factor=self.backoff_factor, schema=self.schema, **kwargs)
redirect_location = response.get_redirect_location()
if redirect_location and 300 <= response.status <= 308:
redirect_server = _server_url(redirect_location)
Expand Down