From 8756591b0ad24320a0b6fa3b2f7513e17ec46b6c Mon Sep 17 00:00:00 2001
From: Ian Cordasco <ian.cordasco@rackspace.com>
Date: Wed, 21 Jan 2015 14:38:02 -0600
Subject: [PATCH] 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
---
 swiftclient/client.py | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/swiftclient/client.py b/swiftclient/client.py
index 985cad89..0c6d8d34 100644
--- a/swiftclient/client.py
+++ b/swiftclient/client.py
@@ -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