Use a requests.Session object to enable pooling.

Without this a new Session, which has a new PoolManager and thus a
new HTTPS connection pool is used for every single request.

We may have an issue with leaking connections (based on my debug runs
so far) but the default behaviour in that case degrades to unpooled
(new connection made, used, thrown away) so it will be no worse other
than the small overhead of 10 (the default) sockets & SSL session
metadata.

Change-Id: I4b2a4efa414dfc5f8f57b6640ba6796b8a109f58
This commit is contained in:
Robert Collins 2014-07-04 11:10:12 +12:00 committed by James E. Blair
parent a9ac0a5664
commit 7f0ebb25bd

View File

@ -505,6 +505,7 @@ class Sync(object):
self.app = app
self.log = logging.getLogger('gertty.sync')
self.queue = MultiQueue([HIGH_PRIORITY, NORMAL_PRIORITY, LOW_PRIORITY])
self.session = requests.Session()
self.submitTask(UploadReviewsTask(HIGH_PRIORITY))
self.submitTask(SyncProjectListTask(HIGH_PRIORITY))
self.submitTask(SyncSubscribedProjectsTask(HIGH_PRIORITY))
@ -562,7 +563,7 @@ class Sync(object):
def get(self, path):
url = self.url(path)
self.log.debug('GET: %s' % (url,))
r = requests.get(url,
r = self.session.get(url,
verify=self.app.config.verify_ssl,
auth=requests.auth.HTTPDigestAuth(self.app.config.username,
self.app.config.password),
@ -576,7 +577,7 @@ class Sync(object):
url = self.url(path)
self.log.debug('POST: %s' % (url,))
self.log.debug('data: %s' % (data,))
r = requests.post(url, data=json.dumps(data).encode('utf8'),
r = self.session.post(url, data=json.dumps(data).encode('utf8'),
verify=self.app.config.verify_ssl,
auth=requests.auth.HTTPDigestAuth(self.app.config.username,
self.app.config.password),