Skip to content
Myr

Refactor get_semaphore (#625)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Tom Christie · committed by GitHub · parent 66e0f4b · Browse files · 5 files+32 added−42 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+32 added−42 removed

  • other:+32 added−42 removed

5 files+32 added−42 removed

modified:httpx/concurrency/asyncio.py+7 added-17 removed
2 unmodified lines
3import ssl4import sys5import typing67from ..config import PoolLimits, Timeout7from ..config import Timeout8from ..exceptions import ConnectTimeout, PoolTimeout, ReadTimeout, WriteTimeout9from .base import BaseEvent, BasePoolSemaphore, BaseSocketStream, ConcurrencyBackend1011SSL_MONKEY_PATCH_APPLIED = False
155 unmodified lines
167 await self.stream_writer.wait_closed()168169170class PoolSemaphore(BasePoolSemaphore):171 def __init__(self, pool_limits: PoolLimits):172 self.pool_limits = pool_limits171 def __init__(self, max_value: int) -> None:172 self.max_value = max_value173174 @property175 def semaphore(self) -> typing.Optional[asyncio.BoundedSemaphore]:175 def semaphore(self) -> asyncio.BoundedSemaphore:176 if not hasattr(self, "_semaphore"):177 max_connections = self.pool_limits.hard_limit178 if max_connections is None:179 self._semaphore = None180 else:181 self._semaphore = asyncio.BoundedSemaphore(value=max_connections)177 self._semaphore = asyncio.BoundedSemaphore(value=self.max_value)178 return self._semaphore179180 async def acquire(self, timeout: float = None) -> None:185 if self.semaphore is None:186 return187181 try:182 await asyncio.wait_for(self.semaphore.acquire(), timeout)183 except asyncio.TimeoutError:184 raise PoolTimeout()185186 def release(self) -> None:194 if self.semaphore is None:195 return196187 self.semaphore.release()188189190class AsyncioBackend(ConcurrencyBackend):
69 unmodified lines
260 return self.loop.run_until_complete(coroutine(*args, **kwargs))261 finally:262 self._loop = loop263274 def get_semaphore(self, limits: PoolLimits) -> BasePoolSemaphore:275 return PoolSemaphore(limits)264 def get_semaphore(self, max_value: int) -> BasePoolSemaphore:265 return PoolSemaphore(max_value)266267 def create_event(self) -> BaseEvent:268 return typing.cast(BaseEvent, asyncio.Event())
modified:httpx/concurrency/auto.py+3 added-3 removed
1 unmodified line
2import typing34import sniffio56from ..config import PoolLimits, Timeout6from ..config import Timeout7from .base import (8 BaseEvent,9 BasePoolSemaphore,10 BaseSocketStream,
29 unmodified lines
40 timeout: Timeout,41 ) -> BaseSocketStream:42 return await self.backend.open_uds_stream(path, hostname, ssl_context, timeout)4344 def get_semaphore(self, limits: PoolLimits) -> BasePoolSemaphore:45 return self.backend.get_semaphore(limits)44 def get_semaphore(self, max_value: int) -> BasePoolSemaphore:45 return self.backend.get_semaphore(max_value)4647 async def run_in_threadpool(48 self, func: typing.Callable, *args: typing.Any, **kwargs: typing.Any49 ) -> typing.Any:
4 unmodified lines
modified:httpx/concurrency/base.py+2 added-2 removed
1import ssl2import typing34from ..config import PoolLimits, Timeout4from ..config import Timeout567def lookup_backend(8 backend: typing.Union[str, "ConcurrencyBackend"] = "auto"
95 unmodified lines
104 timeout: Timeout,105 ) -> BaseSocketStream:106 raise NotImplementedError() # pragma: no cover107108 def get_semaphore(self, limits: PoolLimits) -> BasePoolSemaphore:108 def get_semaphore(self, max_value: int) -> BasePoolSemaphore:109 raise NotImplementedError() # pragma: no cover110111 async def run_in_threadpool(112 self, func: typing.Callable, *args: typing.Any, **kwargs: typing.Any
10 unmodified lines
modified:httpx/concurrency/trio.py+7 added-19 removed
2 unmodified lines
3import typing45import trio67from ..config import PoolLimits, Timeout7from ..config import Timeout8from ..exceptions import ConnectTimeout, PoolTimeout, ReadTimeout, WriteTimeout9from .base import BaseEvent, BasePoolSemaphore, BaseSocketStream, ConcurrencyBackend1011
70 unmodified lines
82 await self.stream.aclose()838485class PoolSemaphore(BasePoolSemaphore):86 def __init__(self, pool_limits: PoolLimits):87 self.pool_limits = pool_limits86 def __init__(self, max_value: int):87 self.max_value = max_value8889 @property90 def semaphore(self) -> typing.Optional[trio.Semaphore]:90 def semaphore(self) -> trio.Semaphore:91 if not hasattr(self, "_semaphore"):92 max_connections = self.pool_limits.hard_limit93 if max_connections is None:94 self._semaphore = None95 else:96 self._semaphore = trio.Semaphore(97 max_connections, max_value=max_connections98 )92 self._semaphore = trio.Semaphore(self.max_value, max_value=self.max_value)93 return self._semaphore9495 async def acquire(self, timeout: float = None) -> None:102 if self.semaphore is None:103 return10496 timeout = none_as_inf(timeout)9798 with trio.move_on_after(timeout):99 await self.semaphore.acquire()
1 unmodified line
101102 raise PoolTimeout()103104 def release(self) -> None:114 if self.semaphore is None:115 return116105 self.semaphore.release()106107108class TrioBackend(ConcurrencyBackend):
46 unmodified lines
155 return trio.run(156 functools.partial(coroutine, **kwargs) if kwargs else coroutine, *args157 )158171 def get_semaphore(self, limits: PoolLimits) -> BasePoolSemaphore:172 return PoolSemaphore(limits)159 def get_semaphore(self, max_value: int) -> BasePoolSemaphore:160 return PoolSemaphore(max_value)161162 def create_event(self) -> BaseEvent:163 return Event()164
18 unmodified lines
modified:httpx/dispatch/connection_pool.py+13 added-1 removed
11 unmodified lines
1213logger = get_logger(__name__)141516class NullSemaphore(BasePoolSemaphore):17 async def acquire(self, timeout: float = None) -> None:18 return1920 def release(self) -> None:21 return222324class ConnectionStore:25 """26 We need to maintain collections of connections in a way that allows us to:27
78 unmodified lines
106 def max_connections(self) -> BasePoolSemaphore:107 # We do this lazily, to make sure backend autodetection always108 # runs within an async context.109 if not hasattr(self, "_max_connections"):110 limit = self.pool_limits.hard_limit111 if not limit:112 self._max_connections = NullSemaphore() # type: BasePoolSemaphore113 else:102 self._max_connections = self.backend.get_semaphore(self.pool_limits)114 self._max_connections = self.backend.get_semaphore(limit)115 return self._max_connections116117 @property118 def num_connections(self) -> int:
84 unmodified lines