From 20b9e496bc8231a5bcf551ef36948229acbf50e1 Mon Sep 17 00:00:00 2001 From: Danny Date: Tue, 27 Jun 2017 11:40:00 -0400 Subject: [PATCH] Use a persistent requests session instead of recreating it (#39) As per the documentation for requests, this allows us to have a persistent TCP connection and uses connection pooling, causing some speed ups instead of constantly recreating the session object via `requests.request`. --- disco/api/http.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/disco/api/http.py b/disco/api/http.py index cc592ca..e429a28 100644 --- a/disco/api/http.py +++ b/disco/api/http.py @@ -202,6 +202,8 @@ class HTTPClient(LoggingClass): if token: self.headers['Authorization'] = 'Bot ' + token + self.session = requests.Session() + def __call__(self, route, args=None, **kwargs): return self.call(route, args, **kwargs) @@ -257,7 +259,7 @@ class HTTPClient(LoggingClass): # Make the actual request url = self.BASE_URL + route[1].format(**args) self.log.info('%s %s (%s)', route[0].value, url, kwargs.get('params')) - r = requests.request(route[0].value, url, **kwargs) + r = self.session.request(route[0].value, url, **kwargs) # Update rate limiter self.limiter.update(bucket, r)