Skip to content

Commit

Permalink
safe shutdown client
Browse files Browse the repository at this point in the history
  • Loading branch information
sonic182 committed Nov 22, 2020
1 parent e145ecf commit 2cd70d3
Show file tree
Hide file tree
Showing 5 changed files with 271 additions and 244 deletions.
11 changes: 11 additions & 0 deletions aiosonic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,17 @@ def __init__(self, connector: TCPConnector = None):
"""
self.connector = connector or TCPConnector()

async def __aenter__(self):
return self

async def __aexit__(self, *_args, **_kwargs):
await self.shutdown()

async def shutdown(self):
"""Cleanup connections, this method makes client unusable."""
await self.connector.cleanup()


async def _request_with_body(self,
url: str,
method: str,
Expand Down
11 changes: 7 additions & 4 deletions aiosonic/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ def is_closing():
return True # noqa

if not (self.key and key == self.key and not is_closing()):
if self.writer:
self.writer.close()
self.close()

if urlparsed.scheme == 'https':
ssl_context = ssl_context or ssl.create_default_context(
Expand Down Expand Up @@ -125,7 +124,7 @@ async def __aexit__(self, exc_type, exc, tb):
self.key = None
self.h2conn = None
if self.writer:
self.writer.close()
self.close()

if not self.blocked:
await self.release()
Expand All @@ -142,10 +141,14 @@ def timeouts(self) -> Timeouts:

def __del__(self):
"""Cleanup."""
self.close(True)

def close(self, check_closing=False):
"""Close connection if opened."""
if self.writer:
is_closing = getattr(self.writer, 'is_closing',
self.writer._transport.is_closing)
if is_closing():
if not check_closing or is_closing():
self.writer.close()

async def http2_request(self, headers: Dict[str, str],
Expand Down
4 changes: 4 additions & 0 deletions aiosonic/connectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,7 @@ async def wait_free_pool(self):
if self.pool.is_all_free():
return True
asyncio_sleep(0.02)

async def cleanup(self):
"""Cleanup connector connections."""
await self.pool.cleanup()
12 changes: 12 additions & 0 deletions aiosonic/pools.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ def is_all_free(self):
"""Indicates if all pool is free."""
return self.pool_size == self.pool.qsize()

async def cleanup(self):
"""Get all conn and close them, this method let this pool unusable."""
for _ in range(self.pool_size):
conn = self.pool.get()
conn.close()


class SmartPool:
"""Pool which utilizes alive connections."""
Expand Down Expand Up @@ -55,3 +61,9 @@ def release(self, conn):
def is_all_free(self):
"""Indicates if all pool is free."""
return self.pool_size == self.sem._value

async def cleanup(self):
"""Get all conn and close them, this method let this pool unusable."""
for count in range(self.pool_size):
conn = await self.acquire()
conn.close()

0 comments on commit 2cd70d3

Please sign in to comment.