Skip to content

Commit

Permalink
private conn and remove shutdown method
Browse files Browse the repository at this point in the history
  • Loading branch information
sonic182 committed Feb 6, 2024
1 parent 4fa0433 commit c20df01
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 18 deletions.
25 changes: 10 additions & 15 deletions aiosonic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def __init__(self):
self.raw_headers = []
self.body = b""
self.response_initial = {}
self.connection = None
self._connection = None
self.chunked = False
self.compressed = b""
self.chunks_readed = False
Expand Down Expand Up @@ -139,7 +139,7 @@ def _update_cookies(self, header_tuple):

def _set_connection(self, connection: Connection):
"""Set header to response."""
self.connection = connection
self._connection = connection

@property
def status_code(self) -> int:
Expand Down Expand Up @@ -208,27 +208,27 @@ async def json(self, json_decoder=loads) -> dict:

async def read_chunks(self) -> AsyncIterator[bytes]:
"""Read chunks from chunked response."""
if not self.connection:
if not self._connection:
raise ConnectionError("missing connection, possible already read response.")
try:
while True and not self.chunks_readed:
chunk_size = int((await self.connection.readline()).rstrip(), 16)
chunk_size = int((await self._connection.readline()).rstrip(), 16)
if not chunk_size:
# read last CRLF
await self.connection.readline()
await self._connection.readline()
break
chunk = await self.connection.readexactly(chunk_size + 2)
chunk = await self._connection.readexactly(chunk_size + 2)
yield chunk[:-2]
self.chunks_readed = True
finally:
# Ensure the conn get's released
self.connection.release()
self.connection = None
self._connection.release()
self._connection = None

def __del__(self):
# clean it
if self.connection:
self.connection.ensure_released()
if self._connection:
self._connection.ensure_released()

def _set_request_meta(self, urlparsed: ParseResult):
self.request_meta = {"from_path": urlparsed.path or "/"}
Expand Down Expand Up @@ -512,11 +512,6 @@ async def __aenter__(self):
async def __aexit__(self, _exc_type, exc, _tb): # type: ignore
if exc:
raise exc
await self.shutdown()

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

async def _request_with_body(
self,
Expand Down
2 changes: 1 addition & 1 deletion aiosonic/pools.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async def cleanup(self):


class SmartPool:
"""Pool which utilizes alive connections."""
"""Pool which priorizes the reusage of connections."""

def __init__(self, connector, pool_size, connection_cls):
self.pool_size = pool_size
Expand Down
4 changes: 2 additions & 2 deletions tests/test_aiosonic.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ async def test_get_chunked_response(app, aiohttp_server):

async with aiosonic.HTTPClient() as client:
res = await client.get(url)
assert res.connection
assert res._connection
assert res.status_code == 200

chunks = [b"foo", b"bar"]
Expand Down Expand Up @@ -526,7 +526,7 @@ async def test_read_chunks_by_text_method(app, aiohttp_server):

async with aiosonic.HTTPClient() as client:
res = await client.get(url)
assert res.connection
assert res._connection
assert res.status_code == 200
assert await res.text() == "foobar"
await server.close()
Expand Down

0 comments on commit c20df01

Please sign in to comment.