Release connection after consuming the content

When using stream=True, we need to make sure we release a connection
back to the connection pool to avoid warnings clogging up the logs. We
can do this by releasing the connection once we've read all the content
from the response. In situations where stream=False, requests already
does this for us.

Related-bug: 1424732
Closes-bug: 1341777
Change-Id: Id1c72ad86135a49d4b985992d788736b65f7dbda
This commit is contained in:
Ian Cordasco 2015-01-21 14:38:02 -06:00 committed by Christian Schwede
parent 731707f06d
commit 8756591b0a

@ -21,7 +21,6 @@ import socket
import requests
import logging
import warnings
import functools
try:
from simplejson import loads as json_loads
except ImportError:
@ -233,10 +232,22 @@ class HTTPConnection(object):
def getheader(k, v=None):
return old_getheader(k.lower(), v)
def releasing_read(*args, **kwargs):
kwargs['decode_content'] = True
chunk = self.resp.raw.read(*args, **kwargs)
if not chunk:
# NOTE(sigmavirus24): Release the connection back to the
# urllib3's connection pool. This will reduce the number of
# log messages seen in bug #1341777. This does not actually
# close a socket. It will also prevent people from being
# mislead as to the cause of a bug as in bug #1424732.
self.resp.close()
return chunk
self.resp.getheaders = getheaders
self.resp.getheader = getheader
self.resp.read = functools.partial(self.resp.raw.read,
decode_content=True)
self.resp.read = releasing_read
return self.resp