Skip to content
Myr

pool.py

This file

Path

httpcore/pool.py

126 lines · 4.0 KiB · Python

Last changed by

Add SyncConnectionPoolTom Christiebefore adoption4a36ec7

History of this file

Take it

Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7

lines 1–13why, one step back
1import asyncio
2import 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 Connection

Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b

line 14why, one step back
14from .datastructures import Client, Origin, Request, Response

Merge pull request #7 from encode/timeout-testsTom Christiebefore adoption34c03d7

line 15why, one step back
15from .exceptions import PoolTimeout

Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7

lines 16–17why, one step back
16
17

Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b

line 18why, one step back
18class ConnectionPool(Client):

Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7

lines 19–25why, one step back
19 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 back
26 self.ssl = ssl

Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7

lines 27–29why, one step back
27 self.timeout = timeout
28 self.limits = limits29 self.is_closed = False

Merge pull request #4 from encode/connection-poolingTom Christiebefore adoption68c468a

lines 30–31why, one step back
30 self.num_active_connections = 0
31 self.num_keepalive_connections = 0

Merge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde

line 32why, one step back
32 self._keepalive_connections = (

Merge pull request #4 from encode/connection-poolingTom Christiebefore adoption68c468a

line 33why, one step back
33 {}

Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b

line 34why, one step back
34 ) # 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 back
35 self._max_connections = ConnectionSemaphore(
36 max_connections=self.limits.hard_limit

Merge pull request #4 from encode/connection-poolingTom Christiebefore adoption68c468a

line 37why, one step back
37 )

Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7

line 38why, one step back
38

Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b

line 39why, one step back
39 async def send(

Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7

line 40why, one step back
40 self,

Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b

line 41why, one step back
41 request: Request,

Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7

line 42why, one step back
42 *,

Merge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde

lines 43–44why, one step back
43 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 back
45 ) -> Response:

Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b

lines 46–47why, one step back
46 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 back
48 return response
49

Merge pull request #4 from encode/connection-poolingTom Christiebefore adoption68c468a

lines 50–53why, one step back
50 @property
51 def num_connections(self) -> int:52 return self.num_active_connections + self.num_keepalive_connections53

Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7

line 54why, one step back
54 async def acquire_connection(

Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b

line 55why, one step back
55 self, origin: Origin, timeout: typing.Optional[TimeoutConfig] = None

Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7

line 56why, one step back
56 ) -> Connection:

Merge pull request #4 from encode/connection-poolingTom Christiebefore adoption68c468a

line 57why, one step back
57 try:

Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b

lines 58–60why, one step back
58 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 back
61 self.num_keepalive_connections -= 1
62 self.num_active_connections += 16364 except (KeyError, IndexError):

Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b

lines 65–69why, one step back
65 if timeout is None:
66 pool_timeout = self.timeout.pool_timeout67 else:68 pool_timeout = timeout.pool_timeout69

Merge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde

line 70why, one step back
70 try:

Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b

line 71why, one step back
71 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 back
72 except asyncio.TimeoutError:
73 raise PoolTimeout()

Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b

lines 74–79why, one step back
74 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 back
80 self.num_active_connections += 1
81

Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7

lines 82–83why, one step back
82 return connection
83

Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b

line 84why, one step back
84 async def release_connection(self, connection: Connection) -> None:

Merge pull request #4 from encode/connection-poolingTom Christiebefore adoption68c468a

line 85why, one step back
85 if connection.is_closed:

Merge pull request #9 from encode/allow_per_request_timeout_and_sslTom Christiebefore adoptiondf84cde

line 86why, one step back
86 self._max_connections.release()

Merge pull request #4 from encode/connection-poolingTom Christiebefore adoption68c468a

lines 87–91why, one step back
87 self.num_active_connections -= 1
88 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 back
92 self._max_connections.release()

Merge pull request #4 from encode/connection-poolingTom Christiebefore adoption68c468a

line 93why, one step back
93 self.num_active_connections -= 1

Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b

line 94why, one step back
94 await connection.close()

Merge pull request #4 from encode/connection-poolingTom Christiebefore adoption68c468a

lines 95–98why, one step back
95 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 back
99 self._keepalive_connections[connection.origin].append(connection)

Merge pull request #4 from encode/connection-poolingTom Christiebefore adoption68c468a

line 100why, one step back
100 except KeyError:

Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b

line 101why, one step back
101 self._keepalive_connections[connection.origin] = [connection]

Merge pull request #4 from encode/connection-poolingTom Christiebefore adoption68c468a

line 102why, one step back
102

Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7

lines 103–106why, one step back
103 async def close(self) -> None:
104 self.is_closed = True105106

Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b

lines 107–108why, one step back
107class ConnectionSemaphore:
108 def __init__(self, max_connections: int = None):

Add SyncConnectionPoolTom Christiebefore adoption4a36ec7

line 109why, one step back
109 self.max_connections = max_connections

Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b

line 110why, one step back
110

Add SyncConnectionPoolTom Christiebefore adoption4a36ec7

lines 111–119why, one step back
111 @property
112 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._semaphore119

Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b

line 120why, one step back
120 async def acquire(self) -> None:

Add SyncConnectionPoolTom Christiebefore adoption4a36ec7

line 121why, one step back
121 if self.semaphore is not None:

Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b

lines 122–124why, one step back
122 await self.semaphore.acquire()
123124 def release(self) -> None:

Add SyncConnectionPoolTom Christiebefore adoption4a36ec7

line 125why, one step back
125 if self.semaphore is not None:

Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b

line 126why, one step back
126 self.semaphore.release()