Skip to content
Myr

connection.py

This file

Path

httpcore/connection.py

92 lines · 3.1 KiB · Python

Last changed by

Tighten up connection acquiry/releaseTom Christiebefore adoption28c505a

History of this file

Take it

Tighten up connection acquiry/releaseTom Christiebefore adoption28c505a

line 1why, one step back
1import functools

First pass at HTTP/2 supportTom Christiebefore adoption53f3dc4

lines 2–10why, one step back
2import typing
34import h2.connection5import h1167from .config import DEFAULT_SSL_CONFIG, DEFAULT_TIMEOUT_CONFIG, SSLConfig, TimeoutConfig8from .exceptions import ConnectTimeout9from .http2 import HTTP2Connection10from .http11 import HTTP11Connection

Add stream ID to HTTP2ConnectionTom Christiebefore adoption0bdcbae

line 11why, one step back
11from .models import Client, Origin, Request, Response

Stream refactoring and HTTP/2 test caseTom Christiebefore adoptiona44190f

line 12why, one step back
12from .streams import Protocol, connect

First pass at HTTP/2 supportTom Christiebefore adoption53f3dc4

lines 13–20why, one step back
13
1415class HTTPConnection(Client):16 def __init__(17 self,18 origin: typing.Union[str, Origin],19 ssl: SSLConfig = DEFAULT_SSL_CONFIG,20 timeout: TimeoutConfig = DEFAULT_TIMEOUT_CONFIG,

Tighten up connection acquiry/releaseTom Christiebefore adoption28c505a

line 21why, one step back
21 pool_release_func: typing.Callable = None,

First pass at HTTP/2 supportTom Christiebefore adoption53f3dc4

lines 22–25why, one step back
22 ):
23 self.origin = Origin(origin) if isinstance(origin, str) else origin24 self.ssl = ssl25 self.timeout = timeout

Tighten up connection acquiry/releaseTom Christiebefore adoption28c505a

line 26why, one step back
26 self.pool_release_func = pool_release_func

First pass at HTTP/2 supportTom Christiebefore adoption53f3dc4

lines 27–42why, one step back
27 self.h11_connection = None # type: typing.Optional[HTTP11Connection]
28 self.h2_connection = None # type: typing.Optional[HTTP2Connection]2930 async def send(31 self,32 request: Request,33 *,34 ssl: typing.Optional[SSLConfig] = None,35 timeout: typing.Optional[TimeoutConfig] = None,36 ) -> Response:37 if self.h11_connection is None and self.h2_connection is None:38 if ssl is None:39 ssl = self.ssl40 if timeout is None:41 timeout = self.timeout42

Stream refactoring and HTTP/2 test caseTom Christiebefore adoptiona44190f

lines 43–46why, one step back
43 hostname = self.origin.hostname
44 port = self.origin.port45 ssl_context = await ssl.load_ssl_context() if self.origin.is_ssl else None46

Tighten up connection acquiry/releaseTom Christiebefore adoption28c505a

lines 47–51why, one step back
47 if self.pool_release_func is None:
48 on_release = None49 else:50 on_release = functools.partial(self.pool_release_func, self)51

Stream refactoring and HTTP/2 test caseTom Christiebefore adoptiona44190f

lines 52–55why, one step back
52 reader, writer, protocol = await connect(
53 hostname, port, ssl_context, timeout54 )55 if protocol == Protocol.HTTP_2:

First pass at HTTP/2 supportTom Christiebefore adoption53f3dc4

lines 56–60why, one step back
56 self.h2_connection = HTTP2Connection(
57 reader,58 writer,59 origin=self.origin,60 timeout=self.timeout,

Tighten up connection acquiry/releaseTom Christiebefore adoption28c505a

line 61why, one step back
61 on_release=on_release,

First pass at HTTP/2 supportTom Christiebefore adoption53f3dc4

lines 62–68why, one step back
62 )
63 else:64 self.h11_connection = HTTP11Connection(65 reader,66 writer,67 origin=self.origin,68 timeout=self.timeout,

Tighten up connection acquiry/releaseTom Christiebefore adoption28c505a

line 69why, one step back
69 on_release=on_release,

First pass at HTTP/2 supportTom Christiebefore adoption53f3dc4

lines 70–82why, one step back
70 )
7172 if self.h2_connection is not None:73 response = await self.h2_connection.send(request, ssl=ssl, timeout=timeout)74 else:75 assert self.h11_connection is not None76 response = await self.h11_connection.send(request, ssl=ssl, timeout=timeout)7778 return response7980 async def close(self) -> None:81 if self.h2_connection is not None:82 await self.h2_connection.close()

Stream refactoring and HTTP/2 test caseTom Christiebefore adoptiona44190f

line 83why, one step back
83 elif self.h11_connection is not None:

First pass at HTTP/2 supportTom Christiebefore adoption53f3dc4

lines 84–92why, one step back
84 await self.h11_connection.close()
8586 @property87 def is_closed(self) -> bool:88 if self.h2_connection is not None:89 return self.h2_connection.is_closed90 else:91 assert self.h11_connection is not None92 return self.h11_connection.is_closed