Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: pass configured timeout to HTTP client #222

Merged
merged 2 commits into from
Apr 9, 2021
Merged
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
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
### Features
1. [#203](https://github.com/influxdata/influxdb-client-python/issues/219): Bind query parameters

### Bug Fixes
1. [#222](https://github.com/influxdata/influxdb-client-python/pull/222): Pass configured timeout to HTTP client

## 1.16.0 [2021-04-01]

### Features
Expand All @@ -11,7 +14,7 @@

### Bug Fixes
1. [#206](https://github.com/influxdata/influxdb-client-python/pull/207): Use default (system) certificates instead of Mozilla's root certificates (certifi.where())
1. [#217](https://github.com/influxdata/influxdb-client-python/pull/217): Fixed clone_task function
1. [#217](https://github.com/influxdata/influxdb-client-python/pull/217): Fix clone_task function

### API
1. [#209](https://github.com/influxdata/influxdb-client-python/pull/209): Allow setting shard-group durations for buckets via API
Expand Down
8 changes: 5 additions & 3 deletions influxdb_client/client/influxdb_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@
class InfluxDBClient(object):
"""InfluxDBClient is client for InfluxDB v2."""

def __init__(self, url, token, debug=None, timeout=10000, enable_gzip=False, org: str = None,
def __init__(self, url, token, debug=None, timeout=10_000, enable_gzip=False, org: str = None,
default_tags: dict = None, **kwargs) -> None:
"""
Initialize defaults.

:param url: InfluxDB server API url (ex. http://localhost:8086).
:param token: auth token
:param debug: enable verbose logging of http requests
:param timeout: default http client timeout
:param timeout: HTTP client timeout setting for a request specified in milliseconds.
If one number provided, it will be total request timeout.
It can also be a pair (tuple) of (connection, read) timeouts.
:param enable_gzip: Enable Gzip compression for http requests. Currently only the "Write" and "Query" endpoints
supports the Gzip compression.
:param org: organization name (used as a default in query and write API)
Expand All @@ -43,7 +45,6 @@ def __init__(self, url, token, debug=None, timeout=10000, enable_gzip=False, org
"""
self.url = url
self.token = token
self.timeout = timeout
self.org = org

self.default_tags = default_tags
Expand All @@ -59,6 +60,7 @@ def __init__(self, url, token, debug=None, timeout=10000, enable_gzip=False, org
conf.ssl_ca_cert = kwargs.get('ssl_ca_cert', None)
conf.proxy = kwargs.get('proxy', None)
conf.connection_pool_maxsize = kwargs.get('connection_pool_maxsize', conf.connection_pool_maxsize)
conf.timeout = timeout

auth_token = self.token
auth_header_name = "Authorization"
Expand Down
3 changes: 3 additions & 0 deletions influxdb_client/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ def __init__(self):
# requests to the same host, which is often the case here.
# cpu_count * 5 is used as default value to increase performance.
self.connection_pool_maxsize = multiprocessing.cpu_count() * 5
# Timeout setting for a request. If one number provided, it will be total request timeout.
# It can also be a pair (tuple) of (connection, read) timeouts.
self.timeout = None

# Proxy URL
self.proxy = None
Expand Down
13 changes: 7 additions & 6 deletions influxdb_client/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,14 @@ def request(self, method, url, query_params=None, headers=None,
headers = headers or {}

timeout = None
if _request_timeout:
if isinstance(_request_timeout, (int, ) if six.PY3 else (int, long)): # noqa: E501,F821
timeout = urllib3.Timeout(total=_request_timeout)
elif (isinstance(_request_timeout, tuple) and
len(_request_timeout) == 2):
_configured_timeout = _request_timeout or self.configuration.timeout
if _configured_timeout:
if isinstance(_configured_timeout, (int, ) if six.PY3 else (int, long)): # noqa: E501,F821
timeout = urllib3.Timeout(total=_configured_timeout / 1_000)
elif (isinstance(_configured_timeout, tuple) and
len(_configured_timeout) == 2):
timeout = urllib3.Timeout(
connect=_request_timeout[0], read=_request_timeout[1])
connect=_configured_timeout[0] / 1_000, read=_configured_timeout[1] / 1_000)

if 'Content-Type' not in headers:
headers['Content-Type'] = 'application/json'
Expand Down
2 changes: 1 addition & 1 deletion tests/test_InfluxDBClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def assertConfig(self):
self.assertEqual("http://localhost:8086", self.client.url)
self.assertEqual("my-org", self.client.org)
self.assertEqual("my-token", self.client.token)
self.assertEqual(6000, self.client.timeout)
self.assertEqual(6000, self.client.api_client.configuration.timeout)
self.assertEqual(3, len(self.client.default_tags))
self.assertEqual("132-987-655", self.client.default_tags["id"])
self.assertEqual("California Miner", self.client.default_tags["customer"])
Expand Down
2 changes: 1 addition & 1 deletion tests/test_WriteApi.py
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ def _check_connection_settings(self):

self.assertEqual("my-org", self.client.org)
self.assertEqual("my-token", self.client.token)
self.assertEqual(6000, self.client.timeout)
self.assertEqual(6000, self.client.api_client.configuration.timeout)

def test_default_tags_from_conf_file(self):
self.client.close()
Expand Down