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

Making release() and time() async #170

Merged
merged 3 commits into from
Aug 26, 2020
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
8 changes: 4 additions & 4 deletions httpcore/_async/connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def __init__(self) -> None:
async def acquire(self, timeout: float = None) -> None:
return

def release(self) -> None:
async def release(self) -> None:
return


Expand Down Expand Up @@ -265,7 +265,7 @@ async def _response_closed(self, connection: AsyncHTTPConnection) -> None:
remove_from_pool = True
close_connection = True
elif self._keepalive_expiry is not None:
now = self._backend.time()
now = await self._backend.time()
connection.expires_at = now + self._keepalive_expiry

if remove_from_pool:
Expand All @@ -281,7 +281,7 @@ async def _keepalive_sweep(self) -> None:
if self._keepalive_expiry is None:
return

now = self._backend.time()
now = await self._backend.time()
if now < self._next_keepalive_check:
return

Expand Down Expand Up @@ -315,7 +315,7 @@ async def _remove_from_pool(self, connection: AsyncHTTPConnection) -> None:
logger.trace("removing connection from pool=%r", connection)
async with self._thread_lock:
if connection in self._connections.get(connection.origin, set()):
self._connection_semaphore.release()
await self._connection_semaphore.release()
self._connections[connection.origin].remove(connection)
if not self._connections[connection.origin]:
del self._connections[connection.origin]
Expand Down
4 changes: 2 additions & 2 deletions httpcore/_async/http2.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ async def request(
self.events[stream_id] = []
return await h2_stream.request(method, url, headers, stream, timeout)
except Exception:
self.max_streams_semaphore.release()
await self.max_streams_semaphore.release()
raise

async def send_connection_init(self, timeout: TimeoutDict) -> None:
Expand Down Expand Up @@ -269,7 +269,7 @@ async def close_stream(self, stream_id: int) -> None:
elif self.state == ConnectionState.FULL:
await self.aclose()
finally:
self.max_streams_semaphore.release()
await self.max_streams_semaphore.release()


class AsyncHTTP2Stream:
Expand Down
6 changes: 3 additions & 3 deletions httpcore/_backends/asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ class Lock(AsyncLock):
def __init__(self) -> None:
self._lock = asyncio.Lock()

def release(self) -> None:
async def release(self) -> None:
self._lock.release()

async def acquire(self) -> None:
Expand All @@ -204,7 +204,7 @@ async def acquire(self, timeout: float = None) -> None:
except asyncio.TimeoutError:
raise self.exc_class()

def release(self) -> None:
async def release(self) -> None:
self.semaphore.release()


Expand Down Expand Up @@ -267,6 +267,6 @@ def create_lock(self) -> AsyncLock:
def create_semaphore(self, max_value: int, exc_class: type) -> AsyncSemaphore:
return Semaphore(max_value, exc_class=exc_class)

def time(self) -> float:
async def time(self) -> float:
loop = asyncio.get_event_loop()
return loop.time()
4 changes: 2 additions & 2 deletions httpcore/_backends/auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,5 @@ def create_lock(self) -> AsyncLock:
def create_semaphore(self, max_value: int, exc_class: type) -> AsyncSemaphore:
return self.backend.create_semaphore(max_value, exc_class=exc_class)

def time(self) -> float:
return self.backend.time()
async def time(self) -> float:
return await self.backend.time()
8 changes: 4 additions & 4 deletions httpcore/_backends/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ async def __aexit__(
exc_value: BaseException = None,
traceback: TracebackType = None,
) -> None:
self.release()
await self.release()

def release(self) -> None:
async def release(self) -> None:
raise NotImplementedError() # pragma: no cover

async def acquire(self) -> None:
Expand All @@ -65,7 +65,7 @@ class AsyncSemaphore:
async def acquire(self, timeout: float = None) -> None:
raise NotImplementedError() # pragma: no cover

def release(self) -> None:
async def release(self) -> None:
raise NotImplementedError() # pragma: no cover


Expand Down Expand Up @@ -96,5 +96,5 @@ def create_lock(self) -> AsyncLock:
def create_semaphore(self, max_value: int, exc_class: type) -> AsyncSemaphore:
raise NotImplementedError() # pragma: no cover

def time(self) -> float:
async def time(self) -> float:
raise NotImplementedError() # pragma: no cover
6 changes: 3 additions & 3 deletions httpcore/_backends/trio.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class Lock(AsyncLock):
def __init__(self) -> None:
self._lock = trio.Lock()

def release(self) -> None:
async def release(self) -> None:
self._lock.release()

async def acquire(self) -> None:
Expand All @@ -129,7 +129,7 @@ async def acquire(self, timeout: float = None) -> None:

raise self.exc_class()

def release(self) -> None:
async def release(self) -> None:
self.semaphore.release()


Expand Down Expand Up @@ -198,5 +198,5 @@ def create_lock(self) -> AsyncLock:
def create_semaphore(self, max_value: int, exc_class: type) -> AsyncSemaphore:
return Semaphore(max_value, exc_class=exc_class)

def time(self) -> float:
async def time(self) -> float:
return trio.current_time()