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

Keep-alive timeouts. #627

Merged
merged 7 commits into from
Dec 12, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions httpx/concurrency/asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ async def open_uds_stream(

return SocketStream(stream_reader=stream_reader, stream_writer=stream_writer)

def time(self) -> float:
loop = asyncio.get_event_loop()
return loop.time()

async def run_in_threadpool(
self, func: typing.Callable, *args: typing.Any, **kwargs: typing.Any
) -> typing.Any:
Expand Down
3 changes: 3 additions & 0 deletions httpx/concurrency/auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ async def open_uds_stream(
) -> BaseSocketStream:
return await self.backend.open_uds_stream(path, hostname, ssl_context, timeout)

def time(self) -> float:
return self.backend.time()

def get_semaphore(self, limits: PoolLimits) -> BasePoolSemaphore:
return self.backend.get_semaphore(limits)

Expand Down
3 changes: 3 additions & 0 deletions httpx/concurrency/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ async def open_uds_stream(
) -> BaseSocketStream:
raise NotImplementedError() # pragma: no cover

def time(self) -> float:
raise NotImplementedError() # pragma: no cover

def get_semaphore(self, limits: PoolLimits) -> BasePoolSemaphore:
raise NotImplementedError() # pragma: no cover

Expand Down
3 changes: 3 additions & 0 deletions httpx/concurrency/trio.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ def run(
functools.partial(coroutine, **kwargs) if kwargs else coroutine, *args
)

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

def get_semaphore(self, limits: PoolLimits) -> BasePoolSemaphore:
return PoolSemaphore(limits)

Expand Down
1 change: 1 addition & 0 deletions httpx/dispatch/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def __init__(
self.release_func = release_func
self.uds = uds
self.open_connection: typing.Optional[OpenConnection] = None
self.timeout_at: typing.Optional[float] = None

async def send(
self,
Expand Down
17 changes: 17 additions & 0 deletions httpx/dispatch/connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ def __len__(self) -> int:


class ConnectionPool(Dispatcher):
KEEP_ALIVE_TIMEOUT = 5.0
florimondmanca marked this conversation as resolved.
Show resolved Hide resolved

def __init__(
self,
*,
Expand All @@ -93,6 +95,7 @@ def __init__(
self.active_connections = ConnectionStore()

self.backend = lookup_backend(backend)
self.next_keepalive_check = 0.0

@property
def max_connections(self) -> BasePoolSemaphore:
Expand All @@ -106,13 +109,26 @@ def max_connections(self) -> BasePoolSemaphore:
def num_connections(self) -> int:
return len(self.keepalive_connections) + len(self.active_connections)

async def keepalive_timeouts(self) -> None:
florimondmanca marked this conversation as resolved.
Show resolved Hide resolved
now = self.backend.time()
if now < self.next_keepalive_check:
return
self.next_keepalive_check = now + 1.0
keepalives = list(self.keepalive_connections.all.keys())
florimondmanca marked this conversation as resolved.
Show resolved Hide resolved
for connection in keepalives:
if connection.timeout_at is not None and now > connection.timeout_at:
florimondmanca marked this conversation as resolved.
Show resolved Hide resolved
self.keepalive_connections.remove(connection)
self.max_connections.release()
await connection.close()

async def send(
self,
request: Request,
verify: VerifyTypes = None,
cert: CertTypes = None,
timeout: Timeout = None,
) -> Response:
await self.keepalive_timeouts()
connection = await self.acquire_connection(
origin=request.url.origin, timeout=timeout
)
Expand Down Expand Up @@ -168,6 +184,7 @@ async def release_connection(self, connection: HTTPConnection) -> None:
self.max_connections.release()
await connection.close()
else:
connection.timeout_at = self.backend.time() + self.KEEP_ALIVE_TIMEOUT
self.active_connections.remove(connection)
self.keepalive_connections.add(connection)

Expand Down