Skip to content

Commit 541b8bd

Browse files
bednarrhajek
authored andcommitted
feat: allow to configure a connection pool maxsize (#215)
1 parent fffbae7 commit 541b8bd

File tree

6 files changed

+41
-11
lines changed

6 files changed

+41
-11
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
## 1.16.0 [unreleased]
22

33
### Features
4-
1. [#203](https://github.com/influxdata/influxdb-client-python/pull/203): Allow configuring client via TOML file.
4+
1. [#203](https://github.com/influxdata/influxdb-client-python/pull/203): Configure a client via TOML file
5+
1. [#215](https://github.com/influxdata/influxdb-client-python/pull/215): Configure a connection pool maxsize
56

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

README.rst

+2
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ The following options are supported:
174174
- ``timeout`` - socket timeout in ms (default value is 10000)
175175
- ``verify_ssl`` - set this to false to skip verifying SSL certificate when calling API from https server
176176
- ``ssl_ca_cert`` - set this to customize the certificate file to verify the peer
177+
- ``connection_pool_maxsize`` - set the number of connections to save that can be reused by urllib3
177178

178179
.. code-block:: python
179180
@@ -200,6 +201,7 @@ Supported properties are:
200201
- ``INFLUXDB_V2_TIMEOUT`` - socket timeout in ms (default value is 10000)
201202
- ``INFLUXDB_V2_VERIFY_SSL`` - set this to false to skip verifying SSL certificate when calling API from https server
202203
- ``INFLUXDB_V2_SSL_CA_CERT`` - set this to customize the certificate file to verify the peer
204+
- ``INFLUXDB_V2_CONNECTION_POOL_MAXSIZE`` - set this to customize the certificate file to verify the peer
203205

204206
.. code-block:: python
205207

influxdb_client/client/influxdb_client.py

+24-10
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ def __init__(self, url, token, debug=None, timeout=10000, enable_gzip=False, org
3535
:key bool verify_ssl: Set this to false to skip verifying SSL certificate when calling API from https server.
3636
:key str ssl_ca_cert: Set this to customize the certificate file to verify the peer.
3737
:key str proxy: Set this to configure the http proxy to be used (ex. http://localhost:3128)
38+
:key int connection_pool_maxsize: Number of connections to save that can be reused by urllib3.
39+
Defaults to "multiprocessing.cpu_count() * 5".
3840
:key urllib3.util.retry.Retry retries: Set the default retry strategy that is used for all HTTP requests
3941
except batching writes. As a default there is no one retry strategy.
4042
@@ -56,6 +58,7 @@ def __init__(self, url, token, debug=None, timeout=10000, enable_gzip=False, org
5658
conf.verify_ssl = kwargs.get('verify_ssl', True)
5759
conf.ssl_ca_cert = kwargs.get('ssl_ca_cert', None)
5860
conf.proxy = kwargs.get('proxy', None)
61+
conf.connection_pool_maxsize = kwargs.get('connection_pool_maxsize', conf.connection_pool_maxsize)
5962

6063
auth_token = self.token
6164
auth_header_name = "Authorization"
@@ -97,6 +100,7 @@ def from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gz
97100
- timeout,
98101
- verify_ssl
99102
- ssl_ca_cert
103+
- connection_pool_maxsize
100104
101105
config.ini example::
102106
@@ -105,6 +109,7 @@ def from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gz
105109
org=my-org
106110
token=my-token
107111
timeout=6000
112+
connection_pool_maxsize=25
108113
109114
[tags]
110115
id = 132-987-655
@@ -118,6 +123,7 @@ def from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gz
118123
token = "my-token"
119124
org = "my-org"
120125
timeout = 6000
126+
connection_pool_maxsize = 25
121127
122128
[tags]
123129
id = "132-987-655"
@@ -152,18 +158,19 @@ def config_value(key: str):
152158
if config.has_option('influx2', 'ssl_ca_cert'):
153159
ssl_ca_cert = config_value('ssl_ca_cert')
154160

161+
connection_pool_maxsize = None
162+
if config.has_option('influx2', 'connection_pool_maxsize'):
163+
connection_pool_maxsize = config_value('connection_pool_maxsize')
164+
155165
default_tags = None
156166

157167
if config.has_section('tags'):
158168
tags = {k: v.strip('"') for k, v in config.items('tags')}
159169
default_tags = dict(tags)
160170

161-
if timeout:
162-
return cls(url, token, debug=debug, timeout=int(timeout), org=org, default_tags=default_tags,
163-
enable_gzip=enable_gzip, verify_ssl=_to_bool(verify_ssl), ssl_ca_cert=ssl_ca_cert)
164-
165-
return cls(url, token, debug=debug, org=org, default_tags=default_tags, enable_gzip=enable_gzip,
166-
verify_ssl=_to_bool(verify_ssl), ssl_ca_cert=ssl_ca_cert)
171+
return cls(url, token, debug=debug, timeout=_to_int(timeout), org=org, default_tags=default_tags,
172+
enable_gzip=enable_gzip, verify_ssl=_to_bool(verify_ssl), ssl_ca_cert=ssl_ca_cert,
173+
connection_pool_maxsize=_to_int(connection_pool_maxsize))
167174

168175
@classmethod
169176
def from_env_properties(cls, debug=None, enable_gzip=False):
@@ -177,22 +184,25 @@ def from_env_properties(cls, debug=None, enable_gzip=False):
177184
- INFLUXDB_V2_TIMEOUT
178185
- INFLUXDB_V2_VERIFY_SSL
179186
- INFLUXDB_V2_SSL_CA_CERT
187+
- INFLUXDB_V2_CONNECTION_POOL_MAXSIZE
180188
"""
181189
url = os.getenv('INFLUXDB_V2_URL', "http://localhost:8086")
182190
token = os.getenv('INFLUXDB_V2_TOKEN', "my-token")
183191
timeout = os.getenv('INFLUXDB_V2_TIMEOUT', "10000")
184192
org = os.getenv('INFLUXDB_V2_ORG', "my-org")
185193
verify_ssl = os.getenv('INFLUXDB_V2_VERIFY_SSL', "True")
186194
ssl_ca_cert = os.getenv('INFLUXDB_V2_SSL_CA_CERT', None)
195+
connection_pool_maxsize = os.getenv('INFLUXDB_V2_CONNECTION_POOL_MAXSIZE', None)
187196

188197
default_tags = dict()
189198

190199
for key, value in os.environ.items():
191200
if key.startswith("INFLUXDB_V2_TAG_"):
192201
default_tags[key[16:].lower()] = value
193202

194-
return cls(url, token, debug=debug, timeout=int(timeout), org=org, default_tags=default_tags,
195-
enable_gzip=enable_gzip, verify_ssl=_to_bool(verify_ssl), ssl_ca_cert=ssl_ca_cert)
203+
return cls(url, token, debug=debug, timeout=_to_int(timeout), org=org, default_tags=default_tags,
204+
enable_gzip=enable_gzip, verify_ssl=_to_bool(verify_ssl), ssl_ca_cert=ssl_ca_cert,
205+
connection_pool_maxsize=_to_int(connection_pool_maxsize))
196206

197207
def write_api(self, write_options=WriteOptions(), point_settings=PointSettings()) -> WriteApi:
198208
"""
@@ -337,5 +347,9 @@ def update_request_body(self, path: str, body):
337347
return _body
338348

339349

340-
def _to_bool(verify_ssl):
341-
return str(verify_ssl).lower() in ("yes", "true")
350+
def _to_bool(bool_value):
351+
return str(bool_value).lower() in ("yes", "true")
352+
353+
354+
def _to_int(int_value):
355+
return int(int_value) if int_value is not None else None

tests/config.ini

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ url=http://localhost:8086
33
org=my-org
44
token=my-token
55
timeout=6000
6+
connection_pool_maxsize=55
67

78
[tags]
89
id = 132-987-655

tests/config.toml

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
org = "my-org"
55
active = true
66
timeout = 6000
7+
connection_pool_maxsize = 55
78

89
[tags]
910
id = "132-987-655"

tests/test_InfluxDBClient.py

+11
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ def tearDown(self) -> None:
1818
if hasattr(self, 'httpd_thread'):
1919
self.httpd_thread.join()
2020

21+
def test_default_conf(self):
22+
self.client = InfluxDBClient(url="http://localhost:8086", token="my-token", org="my-org")
23+
self.assertIsNotNone(self.client.api_client.configuration.connection_pool_maxsize)
24+
2125
def test_TrailingSlashInUrl(self):
2226
self.client = InfluxDBClient(url="http://localhost:8086", token="my-token", org="my-org")
2327
self.assertEqual('http://localhost:8086', self.client.api_client.configuration.host)
@@ -67,6 +71,7 @@ def assertConfig(self):
6771
self.assertEqual("132-987-655", self.client.default_tags["id"])
6872
self.assertEqual("California Miner", self.client.default_tags["customer"])
6973
self.assertEqual("${env.data_center}", self.client.default_tags["data_center"])
74+
self.assertEqual(55, self.client.api_client.configuration.connection_pool_maxsize)
7075

7176
def test_init_from_file_ssl_default(self):
7277
self.client = InfluxDBClient.from_config_file(f'{os.path.dirname(__file__)}/config.ini')
@@ -114,6 +119,12 @@ def test_init_from_env_ssl_ca_cert(self):
114119

115120
self.assertEqual("/my/custom/path/to/cert", self.client.api_client.configuration.ssl_ca_cert)
116121

122+
def test_init_from_env_connection_pool_maxsize(self):
123+
os.environ["INFLUXDB_V2_CONNECTION_POOL_MAXSIZE"] = "29"
124+
self.client = InfluxDBClient.from_env_properties()
125+
126+
self.assertEqual(29, self.client.api_client.configuration.connection_pool_maxsize)
127+
117128
def _start_http_server(self):
118129
import http.server
119130
import ssl

0 commit comments

Comments
 (0)