More specfic annotations on callbacks
This commit is contained in:
parent
f942eb94f1
commit
06f6d50084
@ -11,6 +11,9 @@ from .http11 import HTTP11Connection
|
||||
from .models import Client, Origin, Request, Response
|
||||
from .streams import Protocol, connect
|
||||
|
||||
# Callback signature: async def callback(conn: HTTPConnection) -> None
|
||||
ReleaseCallback = typing.Callable[["HTTPConnection"], typing.Awaitable[None]]
|
||||
|
||||
|
||||
class HTTPConnection(Client):
|
||||
def __init__(
|
||||
@ -18,12 +21,12 @@ class HTTPConnection(Client):
|
||||
origin: typing.Union[str, Origin],
|
||||
ssl: SSLConfig = DEFAULT_SSL_CONFIG,
|
||||
timeout: TimeoutConfig = DEFAULT_TIMEOUT_CONFIG,
|
||||
pool_release_func: typing.Callable = None,
|
||||
release_func: typing.Optional[ReleaseCallback] = None,
|
||||
):
|
||||
self.origin = Origin(origin) if isinstance(origin, str) else origin
|
||||
self.ssl = ssl
|
||||
self.timeout = timeout
|
||||
self.pool_release_func = pool_release_func
|
||||
self.release_func = release_func
|
||||
self.h11_connection = None # type: typing.Optional[HTTP11Connection]
|
||||
self.h2_connection = None # type: typing.Optional[HTTP2Connection]
|
||||
|
||||
@ -59,10 +62,10 @@ class HTTPConnection(Client):
|
||||
port = self.origin.port
|
||||
ssl_context = await ssl.load_ssl_context() if self.origin.is_ssl else None
|
||||
|
||||
if self.pool_release_func is None:
|
||||
if self.release_func is None:
|
||||
on_release = None
|
||||
else:
|
||||
on_release = functools.partial(self.pool_release_func, self)
|
||||
on_release = functools.partial(self.release_func, self)
|
||||
|
||||
reader, writer, protocol = await connect(hostname, port, ssl_context, timeout)
|
||||
if protocol == Protocol.HTTP_2:
|
||||
|
||||
@ -126,7 +126,7 @@ class ConnectionPool(Client):
|
||||
origin,
|
||||
ssl=self.ssl,
|
||||
timeout=self.timeout,
|
||||
pool_release_func=self.release_connection,
|
||||
release_func=self.release_connection,
|
||||
)
|
||||
|
||||
self.active_connections.add(connection)
|
||||
|
||||
@ -19,6 +19,11 @@ H11Event = typing.Union[
|
||||
|
||||
OptionalTimeout = typing.Optional[TimeoutConfig]
|
||||
|
||||
# Callback signature: async def callback() -> None
|
||||
# In practice the callback will be a functools partial, which binds
|
||||
# the `ConnectionPool.release_connection(conn: HTTPConnection)` method.
|
||||
OnReleaseCallback = typing.Callable[[], typing.Awaitable[None]]
|
||||
|
||||
|
||||
class HTTP11Connection(Client):
|
||||
READ_NUM_BYTES = 4096
|
||||
@ -29,11 +34,11 @@ class HTTP11Connection(Client):
|
||||
writer: BaseWriter,
|
||||
origin: Origin,
|
||||
timeout: TimeoutConfig = DEFAULT_TIMEOUT_CONFIG,
|
||||
on_release: typing.Callable = None,
|
||||
on_release: typing.Optional[OnReleaseCallback] = None,
|
||||
):
|
||||
self.origin = origin
|
||||
self.reader = reader
|
||||
self.writer = writer
|
||||
self.origin = origin
|
||||
self.timeout = timeout
|
||||
self.on_release = on_release
|
||||
self.h11_state = h11.Connection(our_role=h11.CLIENT)
|
||||
|
||||
@ -23,9 +23,9 @@ class HTTP2Connection(Client):
|
||||
timeout: TimeoutConfig = DEFAULT_TIMEOUT_CONFIG,
|
||||
on_release: typing.Callable = None,
|
||||
):
|
||||
self.origin = origin
|
||||
self.reader = reader
|
||||
self.writer = writer
|
||||
self.origin = origin
|
||||
self.timeout = timeout
|
||||
self.on_release = on_release
|
||||
self.h2_state = h2.connection.H2Connection()
|
||||
|
||||
Loading…
Reference in New Issue
Block a user