Skip to content

Commit 11aebb2

Browse files
committed
Serialize HTTP requests to one pool.
This patch will solve the problem, when qira outruns the amount of free TCP ports on the system. The proposed solution is to use a single pool of connections, upper bounded by a number less then the amount of free ports on typical linux system (28k on my box).
1 parent 41248ed commit 11aebb2

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

bap.py

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,26 @@
2020

2121
DEBUG_LEVEL = ["Critical", "Error"]
2222

23-
2423
storage = threading.local()
2524
servers = dict()
2625
server_lock = threading.Lock()
26+
requests_lock = threading.Lock()
27+
request = None
28+
29+
def init_requests():
30+
global request
31+
with requests_lock:
32+
if request == None:
33+
request = requests.Session()
34+
adapter = requests.adapters.HTTPAdapter(
35+
pool_connections=1000,
36+
pool_maxsize=1000,
37+
max_retries=10,
38+
pool_block=True)
39+
request.mount('http://', adapter)
40+
41+
init_requests()
42+
2743

2844
def del_instance():
2945
instance = getattr(storage, 'instance', None)
@@ -282,20 +298,21 @@ def __exit__(self):
282298
self.server.terminate()
283299
self.temp.close()
284300

285-
def call(self, data):
286-
def dumps(dic):
287-
self.last_id += 1
288-
dic['id'] = Id(self.last_id)
289-
return json.dumps(dic, default=str)
301+
def dumps(self,dic):
302+
self.last_id += 1
303+
dic['id'] = Id(self.last_id)
304+
return json.dumps(dic, default=str)
290305

306+
def call(self, data):
291307
if isinstance(data, dict):
292-
method = requests.post
308+
method = request.post
293309
if 'get_insns' or 'get_resource' in data:
294-
method = requests.get
295-
return jsons(method(self.url, data=dumps(data)))
310+
method = request.get
311+
return jsons(method(self.url, data=self.dumps(data)))
296312
else:
297-
gen = (dumps(msg) for msg in data)
298-
return jsons(requests.post(self.uri, data=gen))
313+
gen = (self.dumps(msg) for msg in data)
314+
return jsons(request.post(self.uri, data=gen))
315+
299316

300317
def mmap(self, data):
301318
url = "mmap://{0}?offset=0&length={1}".format(

0 commit comments

Comments
 (0)