Skip to content
Myr

connection.py

This file

Path

httpcore/connection.py

88 lines · 3.1 KiB · Python

Last changed by

AdaptersTom Christiebefore adoptionfab6fcd

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–6why, one step back
2import typing
34import h2.connection5import h116

AdaptersTom Christiebefore adoptionfab6fcd

line 7why, one step back
7from .adapters import Adapter

First pass at HTTP/2 supportTom Christiebefore adoption53f3dc4

lines 8–11why, one step back
8from .config import DEFAULT_SSL_CONFIG, DEFAULT_TIMEOUT_CONFIG, SSLConfig, TimeoutConfig
9from .exceptions import ConnectTimeout10from .http2 import HTTP2Connection11from .http11 import HTTP11Connection

AdaptersTom Christiebefore adoptionfab6fcd

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

Stream refactoring and HTTP/2 test caseTom Christiebefore adoptiona44190f

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

First pass at HTTP/2 supportTom Christiebefore adoption53f3dc4

line 14why, one step back
14

More specfic annotations on callbacksTom Christiebefore adoption06f6d50

lines 15–16why, one step back
15# Callback signature: async def callback(conn: HTTPConnection) -> None
16ReleaseCallback = typing.Callable[["HTTPConnection"], typing.Awaitable[None]]

First pass at HTTP/2 supportTom Christiebefore adoption53f3dc4

line 17why, one step back
17

More specfic annotations on callbacksTom Christiebefore adoption06f6d50

line 18why, one step back
18

AdaptersTom Christiebefore adoptionfab6fcd

line 19why, one step back
19class HTTPConnection(Adapter):

First pass at HTTP/2 supportTom Christiebefore adoption53f3dc4

lines 20–24why, one step back
20 def __init__(
21 self,22 origin: typing.Union[str, Origin],23 ssl: SSLConfig = DEFAULT_SSL_CONFIG,24 timeout: TimeoutConfig = DEFAULT_TIMEOUT_CONFIG,

More specfic annotations on callbacksTom Christiebefore adoption06f6d50

line 25why, one step back
25 release_func: typing.Optional[ReleaseCallback] = None,

First pass at HTTP/2 supportTom Christiebefore adoption53f3dc4

lines 26–29why, one step back
26 ):
27 self.origin = Origin(origin) if isinstance(origin, str) else origin28 self.ssl = ssl29 self.timeout = timeout

More specfic annotations on callbacksTom Christiebefore adoption06f6d50

line 30why, one step back
30 self.release_func = release_func

First pass at HTTP/2 supportTom Christiebefore adoption53f3dc4

lines 31–33why, one step back
31 self.h11_connection = None # type: typing.Optional[HTTP11Connection]
32 self.h2_connection = None # type: typing.Optional[HTTP2Connection]33

AdaptersTom Christiebefore adoptionfab6fcd

lines 34–37why, one step back
34 def prepare_request(self, request: Request) -> None:
35 pass3637 async def send(self, request: Request, **options: typing.Any) -> Response:

First pass at HTTP/2 supportTom Christiebefore adoption53f3dc4

line 38why, one step back
38 if self.h11_connection is None and self.h2_connection is None:

AdaptersTom Christiebefore adoptionfab6fcd

line 39why, one step back
39 await self.connect(**options)

First pass at HTTP/2 supportTom Christiebefore adoption53f3dc4

lines 40–41why, one step back
40
41 if self.h2_connection is not None:

AdaptersTom Christiebefore adoptionfab6fcd

line 42why, one step back
42 response = await self.h2_connection.send(request, **options)

First pass at HTTP/2 supportTom Christiebefore adoption53f3dc4

lines 43–44why, one step back
43 else:
44 assert self.h11_connection is not None

AdaptersTom Christiebefore adoptionfab6fcd

line 45why, one step back
45 response = await self.h11_connection.send(request, **options)

First pass at HTTP/2 supportTom Christiebefore adoption53f3dc4

lines 46–48why, one step back
46
47 return response48

AdaptersTom Christiebefore adoptionfab6fcd

lines 49–53why, one step back
49 async def connect(self, **options: typing.Any) -> None:
50 ssl = options.get("ssl", self.ssl)51 timeout = options.get("timeout", self.timeout)52 assert isinstance(ssl, SSLConfig)53 assert isinstance(timeout, TimeoutConfig)

Add http2 connection re-useTom Christiebefore adoptionf942eb9

lines 54–58why, one step back
54
55 hostname = self.origin.hostname56 port = self.origin.port57 ssl_context = await ssl.load_ssl_context() if self.origin.is_ssl else None58

More specfic annotations on callbacksTom Christiebefore adoption06f6d50

line 59why, one step back
59 if self.release_func is None:

Add http2 connection re-useTom Christiebefore adoptionf942eb9

lines 60–61why, one step back
60 on_release = None
61 else:

More specfic annotations on callbacksTom Christiebefore adoption06f6d50

line 62why, one step back
62 on_release = functools.partial(self.release_func, self)

Add http2 connection re-useTom Christiebefore adoptionf942eb9

lines 63–65why, one step back
63
64 reader, writer, protocol = await connect(hostname, port, ssl_context, timeout)65 if protocol == Protocol.HTTP_2:

AdaptersTom Christiebefore adoptionfab6fcd

line 66why, one step back
66 self.h2_connection = HTTP2Connection(reader, writer, on_release=on_release)

Add http2 connection re-useTom Christiebefore adoptionf942eb9

lines 67–68why, one step back
67 else:
68 self.h11_connection = HTTP11Connection(

AdaptersTom Christiebefore adoptionfab6fcd

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

Add http2 connection re-useTom Christiebefore adoptionf942eb9

lines 70–71why, one step back
70 )
71

First pass at HTTP/2 supportTom Christiebefore adoption53f3dc4

lines 72–74why, one step back
72 async def close(self) -> None:
73 if self.h2_connection is not None:74 await self.h2_connection.close()

Stream refactoring and HTTP/2 test caseTom Christiebefore adoptiona44190f

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

First pass at HTTP/2 supportTom Christiebefore adoption53f3dc4

line 76why, one step back
76 await self.h11_connection.close()

Add http2 connection re-useTom Christiebefore adoptionf942eb9

lines 77–80why, one step back
77
78 @property79 def is_http2(self) -> bool:80 return self.h2_connection is not None

First pass at HTTP/2 supportTom Christiebefore adoption53f3dc4

lines 81–88why, one step back
81
82 @property83 def is_closed(self) -> bool:84 if self.h2_connection is not None:85 return self.h2_connection.is_closed86 else:87 assert self.h11_connection is not None88 return self.h11_connection.is_closed