20
20
import msgpack
21
21
import requests
22
22
import requests .exceptions
23
+ from requests .adapters import HTTPAdapter
23
24
from six .moves .urllib .parse import urlparse
24
25
25
26
from influxdb .line_protocol import make_lines , quote_ident , quote_literal
@@ -87,6 +88,11 @@ class InfluxDBClient(object):
87
88
:param headers: headers to add to Requests, will add 'Content-Type'
88
89
and 'Accept' unless these are already present, defaults to {}
89
90
:type headers: dict
91
+ :param socket_options: use custom tcp socket options,
92
+ If not specified, then defaults are loaded from
93
+ ``HTTPConnection.default_socket_options``
94
+ :type socket_options: list
95
+
90
96
:raises ValueError: if cert is provided but ssl is disabled (set to False)
91
97
"""
92
98
@@ -109,6 +115,7 @@ def __init__(self,
109
115
gzip = False ,
110
116
session = None ,
111
117
headers = None ,
118
+ socket_options = None ,
112
119
):
113
120
"""Construct a new InfluxDBClient object."""
114
121
self .__host = host
@@ -128,9 +135,10 @@ def __init__(self,
128
135
session = requests .Session ()
129
136
130
137
self ._session = session
131
- adapter = requests . adapters . HTTPAdapter (
138
+ adapter = _SocketOptionsAdapter (
132
139
pool_connections = int (pool_size ),
133
- pool_maxsize = int (pool_size )
140
+ pool_maxsize = int (pool_size ),
141
+ socket_options = socket_options
134
142
)
135
143
136
144
if use_udp :
@@ -626,7 +634,7 @@ def _batches(iterable, size):
626
634
# http://code.activestate.com/recipes/303279-getting-items-in-batches/
627
635
iterator = iter (iterable )
628
636
while True :
629
- try : # Try get the first element in the iterator...
637
+ try : # Try get the first element in the iterator...
630
638
head = (next (iterator ),)
631
639
except StopIteration :
632
640
return # ...so that we can stop if there isn't one
@@ -1249,3 +1257,16 @@ def _msgpack_parse_hook(code, data):
1249
1257
timestamp += datetime .timedelta (microseconds = (epoch_ns / 1000 ))
1250
1258
return timestamp .isoformat () + 'Z'
1251
1259
return msgpack .ExtType (code , data )
1260
+
1261
+
1262
+ class _SocketOptionsAdapter (HTTPAdapter ):
1263
+ """_SocketOptionsAdapter injects socket_options into HTTP Adapter."""
1264
+
1265
+ def __init__ (self , * args , ** kwargs ):
1266
+ self .socket_options = kwargs .pop ("socket_options" , None )
1267
+ super (_SocketOptionsAdapter , self ).__init__ (* args , ** kwargs )
1268
+
1269
+ def init_poolmanager (self , * args , ** kwargs ):
1270
+ if self .socket_options is not None :
1271
+ kwargs ["socket_options" ] = self .socket_options
1272
+ super (_SocketOptionsAdapter , self ).init_poolmanager (* args , ** kwargs )
0 commit comments