| 30 | |
| 31 | |
| 32 | class H2ConnectionPool: |
| 33 | def __init__(self, reactor: ReactorBase, settings: Settings) -> None: |
| 34 | self._reactor = reactor |
| 35 | self.settings = settings |
| 36 | |
| 37 | # Store a dictionary which is used to get the respective |
| 38 | # H2ClientProtocolInstance using the key as Tuple(scheme, hostname, port) |
| 39 | self._connections: dict[ConnectionKeyT, H2ClientProtocol] = {} |
| 40 | |
| 41 | # Save all requests that arrive before the connection is established |
| 42 | self._pending_requests: dict[ |
| 43 | ConnectionKeyT, deque[Deferred[H2ClientProtocol]] |
| 44 | ] = {} |
| 45 | |
| 46 | self._tls_verbose_logging: bool = settings.getbool( |
| 47 | "DOWNLOADER_CLIENT_TLS_VERBOSE_LOGGING" |
| 48 | ) |
| 49 | |
| 50 | def get_connection( |
| 51 | self, key: ConnectionKeyT, uri: URI, endpoint: HostnameEndpoint |
| 52 | ) -> Deferred[H2ClientProtocol]: |
| 53 | if key in self._pending_requests: |
| 54 | # Received a request while connecting to remote |
| 55 | # Create a deferred which will fire with the H2ClientProtocol |
| 56 | # instance |
| 57 | d: Deferred[H2ClientProtocol] = Deferred() |
| 58 | self._pending_requests[key].append(d) |
| 59 | return d |
| 60 | |
| 61 | # Check if we already have a connection to the remote |
| 62 | conn = self._connections.get(key, None) |
| 63 | if conn: |
| 64 | # Return this connection instance wrapped inside a deferred |
| 65 | return defer.succeed(conn) |
| 66 | |
| 67 | # No connection is established for the given URI |
| 68 | return self._new_connection(key, uri, endpoint) |
| 69 | |
| 70 | def _new_connection( |
| 71 | self, key: ConnectionKeyT, uri: URI, endpoint: HostnameEndpoint |
| 72 | ) -> Deferred[H2ClientProtocol]: |
| 73 | self._pending_requests[key] = deque() |
| 74 | |
| 75 | conn_lost_deferred: Deferred[list[BaseException]] = Deferred() |
| 76 | conn_lost_deferred.addCallback(self._remove_connection, key) |
| 77 | |
| 78 | factory = H2ClientFactory( |
| 79 | uri, |
| 80 | self.settings, |
| 81 | conn_lost_deferred, |
| 82 | tls_verbose_logging=self._tls_verbose_logging, |
| 83 | ) |
| 84 | conn_d = endpoint.connect(factory) |
| 85 | conn_d.addCallback(self.put_connection, key) |
| 86 | |
| 87 | d: Deferred[H2ClientProtocol] = Deferred() |
| 88 | self._pending_requests[key].append(d) |
| 89 | return d |