2525INFLUX_AUTH_SCHEME = "INFLUX_AUTH_SCHEME"
2626INFLUX_GZIP_THRESHOLD = "INFLUX_GZIP_THRESHOLD"
2727INFLUX_WRITE_NO_SYNC = "INFLUX_WRITE_NO_SYNC"
28+ INFLUX_WRITE_TIMEOUT = "INFLUX_WRITE_TIMEOUT"
29+ INFLUX_QUERY_TIMEOUT = "INFLUX_QUERY_TIMEOUT"
2830
2931
3032def write_client_options (** kwargs ):
@@ -97,7 +99,7 @@ def _merge_options(defaults, exclude_keys=None, custom=None):
9799 return _deep_merge (defaults , {key : value for key , value in custom .items () if key not in exclude_keys })
98100
99101
100- def _parse_precision (precision ) :
102+ def _parse_precision (precision : str ) -> WritePrecision :
101103 """
102104 Parses the precision value and ensures it is valid.
103105
@@ -124,7 +126,7 @@ def _parse_precision(precision):
124126 raise ValueError (f"Invalid precision value: { precision } " )
125127
126128
127- def _parse_gzip_threshold (threshold ) :
129+ def _parse_gzip_threshold (threshold : str ) -> int :
128130 """
129131 Parses and validates the provided threshold value.
130132
@@ -148,7 +150,7 @@ def _parse_gzip_threshold(threshold):
148150 return threshold
149151
150152
151- def _parse_write_no_sync (write_no_sync ):
153+ def _parse_write_no_sync (write_no_sync : str ):
152154 """
153155 Parses and validates the provided write no sync value.
154156
@@ -163,6 +165,16 @@ def _parse_write_no_sync(write_no_sync):
163165 return write_no_sync .strip ().lower () in ['true' , '1' , 't' , 'y' , 'yes' ]
164166
165167
168+ def _parse_timeout (to : str ) -> int :
169+ try :
170+ timeout = int (to )
171+ except (TypeError , ValueError ):
172+ raise ValueError (f"Invalid timeout value: { to } . Must be a number." )
173+ if timeout < 0 :
174+ raise ValueError (f"Invalid timeout value: { to } . Must be non-negative." )
175+ return timeout
176+
177+
166178class InfluxDBClient3 :
167179 def __init__ (
168180 self ,
@@ -211,24 +223,30 @@ def __init__(
211223 (defaults to false, don't set to true when talking to InfluxDB 2)
212224 :key str username: ``username`` to authenticate via username and password credentials to the InfluxDB 2.x
213225 :key str password: ``password`` to authenticate via username and password credentials to the InfluxDB 2.x
214- :key str query_timeout: float value used to set the client query API timeout in seconds.
226+ :key str query_timeout: int value used to set the client query API timeout in milliseconds.
227+ :key str write_timeout: int value used to set the client write API timeout in milliseconds.
215228 :key list[str] profilers: list of enabled Flux profilers
216229 """
217230 self ._org = org if org is not None else "default"
218231 self ._database = database
219232 self ._token = token
233+ kw_keys = kwargs .keys ()
220234
221235 write_type = DefaultWriteOptions .write_type .value
222236 write_precision = DefaultWriteOptions .write_precision .value
223237 write_no_sync = DefaultWriteOptions .no_sync .value
224238 write_timeout = DefaultWriteOptions .timeout .value
239+
225240 if isinstance (write_client_options , dict ) and write_client_options .get ('write_options' ) is not None :
226241 write_opts = write_client_options ['write_options' ]
227242 write_type = getattr (write_opts , 'write_type' , write_type )
228243 write_precision = getattr (write_opts , 'write_precision' , write_precision )
229244 write_no_sync = getattr (write_opts , 'no_sync' , write_no_sync )
230245 write_timeout = getattr (write_opts , 'timeout' , write_timeout )
231246
247+ if kw_keys .__contains__ ('write_timeout' ):
248+ write_timeout = kwargs .get ('write_timeout' )
249+
232250 write_options = WriteOptions (
233251 write_type = write_type ,
234252 write_precision = write_precision ,
@@ -269,15 +287,15 @@ def __init__(
269287 connection_string = f"grpc+tcp://{ hostname } :{ port } "
270288
271289 q_opts_builder = QueryApiOptionsBuilder ()
272- kw_keys = kwargs .keys ()
273290 if kw_keys .__contains__ ('ssl_ca_cert' ):
274291 q_opts_builder .root_certs (kwargs .get ('ssl_ca_cert' , None ))
275292 if kw_keys .__contains__ ('verify_ssl' ):
276293 q_opts_builder .tls_verify (kwargs .get ('verify_ssl' , True ))
277294 if kw_keys .__contains__ ('proxy' ):
278295 q_opts_builder .proxy (kwargs .get ('proxy' , None ))
279296 if kw_keys .__contains__ ('query_timeout' ):
280- q_opts_builder .timeout (kwargs .get ('query_timeout' , None ))
297+ query_timeout_float = float (kwargs .get ('query_timeout' ))
298+ q_opts_builder .timeout (query_timeout_float / 1000.0 )
281299 self ._query_api = _QueryApi (connection_string = connection_string , token = token ,
282300 flight_client_options = flight_client_options ,
283301 proxy = kwargs .get ("proxy" , None ), options = q_opts_builder .build ())
@@ -325,6 +343,15 @@ def from_env(cls, **kwargs: Any) -> 'InfluxDBClient3':
325343 if precision is not None :
326344 write_options .write_precision = _parse_precision (precision )
327345
346+ write_timeout = os .getenv (INFLUX_WRITE_TIMEOUT )
347+ if write_timeout is not None :
348+ # N.B. write_options value has precedent over kwargs['write_timeout'] above
349+ write_options .timeout = _parse_timeout (write_timeout )
350+
351+ query_timeout = os .getenv (INFLUX_QUERY_TIMEOUT )
352+ if query_timeout is not None :
353+ kwargs ['query_timeout' ] = _parse_timeout (query_timeout )
354+
328355 write_client_option = {'write_options' : write_options }
329356
330357 if os .getenv (INFLUX_AUTH_SCHEME ) is not None :
0 commit comments