Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always close implicit connector, even for keepalive responses. #79

Merged
merged 2 commits into from
Jun 22, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def request(method, url, *,
if request_class is None:
request_class = ClientRequest
if connector is None:
connector = aiohttp.TCPConnector(loop=loop)
connector = aiohttp.TCPConnector(force_close=True, loop=loop)

while True:
req = request_class(
Expand Down
6 changes: 5 additions & 1 deletion aiohttp/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ def release(self):
class BaseConnector(object):

def __init__(self, *, reuse_timeout=30, conn_timeout=None,
share_cookies=False, loop=None, **kwargs):
share_cookies=False, force_close=False, loop=None, **kwargs):
self._conns = {}
self._reuse_timeout = reuse_timeout
self._conn_timeout = conn_timeout
self._share_cookies = share_cookies
self._cleanup_handle = None
self._force_close = force_close

if loop is None:
loop = asyncio.get_event_loop()
Expand Down Expand Up @@ -165,6 +166,9 @@ def _release(self, key, req, transport, protocol):
if self._share_cookies and resp.cookies:
self.update_cookies(resp.cookies.items())

if self._force_close:
should_close = True

reader = protocol.reader
if should_close or (reader.output and not reader.output.at_eof()):
transport.close()
Expand Down
33 changes: 33 additions & 0 deletions tests/test_client_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,39 @@ def test_multidict_headers(self):
self.assertEqual(str(len(data)),
content['headers']['Content-Length'])

def test_close_implicit_connector(self):

@asyncio.coroutine
def go(url):
r = yield from client.request('GET', url, loop=self.loop)

connection = r.connection
self.assertIsNotNone(connection)
connector = connection._connector
self.assertIsNotNone(connector)
yield from r.read_and_close()
self.assertEqual(0, len(connector._conns))

with test_utils.run_server(self.loop, router=Functional) as httpd:
url = httpd.url('keepalive')
self.loop.run_until_complete(go(url))

def test_dont_close_explicit_connector(self):

@asyncio.coroutine
def go(url):
connector = aiohttp.TCPConnector(loop=self.loop)

r = yield from client.request('GET', url,
connector=connector,
loop=self.loop)
yield from r.read_and_close()
self.assertEqual(1, len(connector._conns))

with test_utils.run_server(self.loop, router=Functional) as httpd:
url = httpd.url('keepalive')
self.loop.run_until_complete(go(url))


class Functional(test_utils.Router):

Expand Down