Repository: gmr/tornado-aws

Issue:
AsyncHTTPClient(force_instance=True) could cause memory leaks
What's the reason of forcing a new instance of the `AsyncHTTPClient`? The Tornado documentation states
> close() is generally only necessary when either the IOLoop is also being closed, or the force_instance=True argument was used when creating the AsyncHTTPClient

Not closing an `AsyncHTTPClient ` instance is potentially causing memory leaks.
And I see an evidence of this within my running Tornado application that uses `AsyncAWSClient` with `AsyncHTTPClient.configure('tornado.curl_httpclient.CurlAsyncHTTPClient')`

```
>>> import objgraph
>>> objgraph.show_most_common_types()
Curl                       393010
list                       33683
function                   23579
```
With 393010 `Curl` instances the proccess eats about 14GB of memory which is insane!

At the very least the `close` method of the `AsyncHTTPClient` must be exposed so that one could close it explicitly. Additionally `force_instance=True` could be removed if there was no particular reason for it.

Discussion:

Patch:
diff --git a/docs/history.rst b/docs/history.rst
index 51124bb..5590628 100644
--- a/docs/history.rst
+++ b/docs/history.rst
@@ -3,6 +3,10 @@
 Version History
 ===============
 
+1.1.0 (2018-03-19)
+------------------
+ - Add ``close`` method to ``AWSClient`` (#9 from `31z4 <https://github.com/31z4>_`)
+
 1.0.0 (2018-01-19)
 ------------------
  - Add new exception type ``tornado_aws.exceptions.RequestException`` (#5 from `nvllsvm <https://github.com/nvllsvm>_`)
diff --git a/requires/installation.txt b/requires/installation.txt
index c98ba2b..78ed743 100644
--- a/requires/installation.txt
+++ b/requires/installation.txt
@@ -1,1 +1,1 @@
-tornado>=4.0
+tornado>=4.0,<5.0
diff --git a/setup.py b/setup.py
index d2addcf..daebb99 100644
--- a/setup.py
+++ b/setup.py
@@ -39,7 +39,7 @@ def read_requirements(name):
 
 setuptools.setup(
     name='tornado-aws',
-    version='1.0.0',
+    version='1.1.0',
     description=DESC,
     long_description=open('README.rst').read(),
     author='Gavin M. Roy',
diff --git a/tornado_aws/__init__.py b/tornado_aws/__init__.py
index 81ee2f7..4fb5d99 100644
--- a/tornado_aws/__init__.py
+++ b/tornado_aws/__init__.py
@@ -6,6 +6,6 @@ from tornado_aws.client import AWSClient
 from tornado_aws.client import AsyncAWSClient
 from tornado_aws.client import exceptions
 
-__version__ = '1.0.0'
+__version__ = '1.1.0'
 
 __all__ = ['AWSClient', 'AsyncAWSClient', 'exceptions']
diff --git a/tornado_aws/client.py b/tornado_aws/client.py
index 95c8e90..775c40a 100644
--- a/tornado_aws/client.py
+++ b/tornado_aws/client.py
@@ -181,6 +181,10 @@ class AWSClient(object):
                                       headers, body, True)
             raise aws_error if aws_error else error
 
+    def close(self):
+        """Closes the underlying HTTP client, freeing any resources used."""
+        self._client.close()
+
     def _process_error(self, error):
         """Attempt to process the error coming from AWS. Returns ``True``
         if the client should attempt to fetch credentials and the AWSError


Test patch:
diff --git a/tests/client_tests.py b/tests/client_tests.py
index 0d02db9..42eed16 100644
--- a/tests/client_tests.py
+++ b/tests/client_tests.py
@@ -125,6 +125,20 @@ class AsyncClientConfigTestCase(ClientConfigTestCase):
     CLIENT = client.AsyncAWSClient
 
 
+class ClientCloseTestCase(TestCase):
+
+    def test_close_is_called(self):
+        with self.client_with_default_creds('s3') as obj:
+            with mock.patch.object(obj._client, 'close') as close:
+                obj.close()
+                close.assert_called_once()
+
+
+class AsyncClientCloseTestCase(ClientCloseTestCase):
+
+    CLIENT = client.AsyncAWSClient
+
+
 class AMZErrorTestCase(TestCase):
 
     def test_awz_error(self):