pool.py
This file
Path
httpcore/pool.py
126 lines · 4.0 KiB · Python
Last changed by
Add SyncConnectionPoolTom Christiebefore adoption4a36ec7
History of this fileTake it
Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
lines 1–13why, one step back1import asyncio2import typing34from .config import (5 DEFAULT_CA_BUNDLE_PATH,6 DEFAULT_POOL_LIMITS,7 DEFAULT_SSL_CONFIG,8 DEFAULT_TIMEOUT_CONFIG,9 PoolLimits,10 SSLConfig,11 TimeoutConfig,12)13from .connections import ConnectionMerge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b
line 14why, one step back14from .datastructures import Client, Origin, Request, ResponseMerge pull request #7 from encode/timeout-testsTom Christiebefore adoption34c03d7
line 15why, one step back15from .exceptions import PoolTimeoutMerge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
lines 16–17why, one step back1617Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b
line 18why, one step back18class ConnectionPool(Client):Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
lines 19–25why, one step back19 def __init__(20 self,21 *,22 ssl: SSLConfig = DEFAULT_SSL_CONFIG,23 timeout: TimeoutConfig = DEFAULT_TIMEOUT_CONFIG,24 limits: PoolLimits = DEFAULT_POOL_LIMITS,25 ):Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b
line 26why, one step back26 self.ssl = sslMerge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
lines 27–29why, one step back27 self.timeout = timeout28 self.limits = limits29 self.is_closed = FalseMerge pull request #4 from encode/connection-poolingTom Christiebefore adoption68c468a
lines 30–31why, one step back30 self.num_active_connections = 031 self.num_keepalive_connections = 0Merge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde
line 32why, one step back32 self._keepalive_connections = (Merge pull request #4 from encode/connection-poolingTom Christiebefore adoption68c468a
line 33why, one step back33 {}Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b
line 34why, one step back34 ) # type: typing.Dict[Origin, typing.List[Connection]]Merge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde
lines 35–36why, one step back35 self._max_connections = ConnectionSemaphore(36 max_connections=self.limits.hard_limitMerge pull request #4 from encode/connection-poolingTom Christiebefore adoption68c468a
line 37why, one step back37 )Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
line 38why, one step back38Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b
line 39why, one step back39 async def send(Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
line 40why, one step back40 self,Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b
line 41why, one step back41 request: Request,Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
line 42why, one step back42 *,Merge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde
lines 43–44why, one step back43 ssl: typing.Optional[SSLConfig] = None,44 timeout: typing.Optional[TimeoutConfig] = None,Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
line 45why, one step back45 ) -> Response:Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b
lines 46–47why, one step back46 connection = await self.acquire_connection(request.url.origin, timeout=timeout)47 response = await connection.send(request, ssl=ssl, timeout=timeout)Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
lines 48–49why, one step back48 return response49Merge pull request #4 from encode/connection-poolingTom Christiebefore adoption68c468a
lines 50–53why, one step back50 @property51 def num_connections(self) -> int:52 return self.num_active_connections + self.num_keepalive_connections53Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
line 54why, one step back54 async def acquire_connection(Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b
line 55why, one step back55 self, origin: Origin, timeout: typing.Optional[TimeoutConfig] = NoneMerge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
line 56why, one step back56 ) -> Connection:Merge pull request #4 from encode/connection-poolingTom Christiebefore adoption68c468a
line 57why, one step back57 try:Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b
lines 58–60why, one step back58 connection = self._keepalive_connections[origin].pop()59 if not self._keepalive_connections[origin]:60 del self._keepalive_connections[origin]Merge pull request #4 from encode/connection-poolingTom Christiebefore adoption68c468a
lines 61–64why, one step back61 self.num_keepalive_connections -= 162 self.num_active_connections += 16364 except (KeyError, IndexError):Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b
lines 65–69why, one step back65 if timeout is None:66 pool_timeout = self.timeout.pool_timeout67 else:68 pool_timeout = timeout.pool_timeout69Merge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde
line 70why, one step back70 try:Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b
line 71why, one step back71 await asyncio.wait_for(self._max_connections.acquire(), pool_timeout)Merge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde
lines 72–73why, one step back72 except asyncio.TimeoutError:73 raise PoolTimeout()Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b
lines 74–79why, one step back74 connection = Connection(75 origin,76 ssl=self.ssl,77 timeout=self.timeout,78 on_release=self.release_connection,79 )Merge pull request #4 from encode/connection-poolingTom Christiebefore adoption68c468a
lines 80–81why, one step back80 self.num_active_connections += 181Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
lines 82–83why, one step back82 return connection83Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b
line 84why, one step back84 async def release_connection(self, connection: Connection) -> None:Merge pull request #4 from encode/connection-poolingTom Christiebefore adoption68c468a
line 85why, one step back85 if connection.is_closed:Merge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde
line 86why, one step back86 self._max_connections.release()Merge pull request #4 from encode/connection-poolingTom Christiebefore adoption68c468a
lines 87–91why, one step back87 self.num_active_connections -= 188 elif (89 self.limits.soft_limit is not None90 and self.num_connections > self.limits.soft_limit91 ):Merge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde
line 92why, one step back92 self._max_connections.release()Merge pull request #4 from encode/connection-poolingTom Christiebefore adoption68c468a
line 93why, one step back93 self.num_active_connections -= 1Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b
line 94why, one step back94 await connection.close()Merge pull request #4 from encode/connection-poolingTom Christiebefore adoption68c468a
lines 95–98why, one step back95 else:96 self.num_active_connections -= 197 self.num_keepalive_connections += 198 try:Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b
line 99why, one step back99 self._keepalive_connections[connection.origin].append(connection)Merge pull request #4 from encode/connection-poolingTom Christiebefore adoption68c468a
line 100why, one step back100 except KeyError:Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b
line 101why, one step back101 self._keepalive_connections[connection.origin] = [connection]Merge pull request #4 from encode/connection-poolingTom Christiebefore adoption68c468a
line 102why, one step back102Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
lines 103–106why, one step back103 async def close(self) -> None:104 self.is_closed = True105106Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b
lines 107–108why, one step back107class ConnectionSemaphore:108 def __init__(self, max_connections: int = None):Add SyncConnectionPoolTom Christiebefore adoption4a36ec7
line 109why, one step back109 self.max_connections = max_connectionsMerge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b
line 110why, one step back110Add SyncConnectionPoolTom Christiebefore adoption4a36ec7
lines 111–119why, one step back111 @property112 def semaphore(self) -> typing.Optional[asyncio.BoundedSemaphore]:113 if not hasattr(self, "_semaphore"):114 if self.max_connections is None:115 self._semaphore = None116 else:117 self._semaphore = asyncio.BoundedSemaphore(value=self.max_connections)118 return self._semaphore119Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b
line 120why, one step back120 async def acquire(self) -> None:Add SyncConnectionPoolTom Christiebefore adoption4a36ec7
line 121why, one step back121 if self.semaphore is not None:Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b
lines 122–124why, one step back122 await self.semaphore.acquire()123124 def release(self) -> None:Add SyncConnectionPoolTom Christiebefore adoption4a36ec7
line 125why, one step back125 if self.semaphore is not None:Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b
line 126why, one step back126 self.semaphore.release()