Skip to content
This repository has been archived by the owner on Aug 1, 2021. It is now read-only.

Commit

Permalink
Use a persistent requests session instead of recreating it (#39)
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
Rapptz authored and b1naryth1ef committed Jun 27, 2017
1 parent 8aca1b4 commit 20b9e49
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion disco/api/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ def __init__(self, token):
if token:
self.headers['Authorization'] = 'Bot ' + token

self.session = requests.Session()

def __call__(self, route, args=None, **kwargs):
return self.call(route, args, **kwargs)

Expand Down Expand Up @@ -257,7 +259,7 @@ def call(self, route, args=None, **kwargs):
# 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)
Expand Down

0 comments on commit 20b9e49

Please sign in to comment.