-
Notifications
You must be signed in to change notification settings - Fork 105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Local address support. #100
Changes from 12 commits
0faa59a
4b41aaa
731dcf8
a2faecb
96cb808
8fdc117
cc4963e
9cd0dee
6285d29
abb518f
36acad7
e634023
3790805
29070ad
04be66b
91f4104
e55fb6e
9d9def2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,6 +76,7 @@ class AsyncConnectionPool(AsyncHTTPTransport): | |
* **keepalive_expiry** - `Optional[float]` - The maximum time to allow | ||
before closing a keep-alive connection. | ||
* **http2** - `bool` - Enable HTTP/2 support. | ||
* **local_addr** - `Optional[bytes]` - Local address to connect from | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Miniature nitpick. Let's use a full stop to match the other cases. "Local address to connect from." 😃 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
""" | ||
|
||
def __init__( | ||
|
@@ -85,12 +86,14 @@ def __init__( | |
max_keepalive: int = None, | ||
keepalive_expiry: float = None, | ||
http2: bool = False, | ||
local_addr: bytes = None, | ||
): | ||
self._ssl_context = SSLContext() if ssl_context is None else ssl_context | ||
self._max_connections = max_connections | ||
self._max_keepalive = max_keepalive | ||
self._keepalive_expiry = keepalive_expiry | ||
self._http2 = http2 | ||
self._local_addr = local_addr | ||
self._connections: Dict[Origin, Set[AsyncHTTPConnection]] = {} | ||
self._thread_lock = ThreadLock() | ||
self._backend = AutoBackend() | ||
|
@@ -141,7 +144,10 @@ async def request( | |
|
||
if connection is None: | ||
connection = AsyncHTTPConnection( | ||
origin=origin, http2=self._http2, ssl_context=self._ssl_context, | ||
origin=origin, | ||
http2=self._http2, | ||
ssl_context=self._ssl_context, | ||
local_addr=self._local_addr, | ||
) | ||
logger.trace("created connection=%r", connection) | ||
await self._add_to_pool(connection, timeout=timeout) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -222,13 +222,20 @@ async def open_tcp_stream( | |
port: int, | ||
ssl_context: Optional[SSLContext], | ||
timeout: TimeoutDict, | ||
local_addr: Optional[bytes], | ||
) -> SocketStream: | ||
host = hostname.decode("ascii") | ||
connect_timeout = timeout.get("connect") | ||
exc_map = {asyncio.TimeoutError: ConnectTimeout, OSError: ConnectError} | ||
with map_exceptions(exc_map): | ||
local_addrport = None | ||
if local_addr: | ||
local_addrport = (local_addr, 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. local_addrport = None if local_addr is None else (local_addr, 0) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
stream_reader, stream_writer = await asyncio.wait_for( | ||
asyncio.open_connection(host, port, ssl=ssl_context), connect_timeout, | ||
asyncio.open_connection( | ||
host, port, ssl=ssl_context, local_addr=local_addrport | ||
), | ||
connect_timeout, | ||
) | ||
return SocketStream( | ||
stream_reader=stream_reader, stream_writer=stream_writer | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,8 +34,11 @@ async def open_tcp_stream( | |
port: int, | ||
ssl_context: Optional[SSLContext], | ||
timeout: TimeoutDict, | ||
local_addr: Optional[bytes], | ||
) -> AsyncSocketStream: | ||
return await self.backend.open_tcp_stream(hostname, port, ssl_context, timeout) | ||
return await self.backend.open_tcp_stream( | ||
hostname, port, ssl_context, timeout, local_addr | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I reckon we ought to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
) | ||
|
||
def create_lock(self) -> AsyncLock: | ||
return self.backend.create_lock() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -140,7 +140,10 @@ async def open_tcp_stream( | |
port: int, | ||
ssl_context: Optional[SSLContext], | ||
timeout: TimeoutDict, | ||
local_addr: Optional[bytes], | ||
) -> AsyncSocketStream: | ||
if local_addr: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe a comment that it is currently supported in Also, what will our implementation change look like once trio support does land here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm adding a comment. I believe the code would just add something like |
||
raise NotImplementedError() | ||
connect_timeout = none_as_inf(timeout.get("connect")) | ||
exc_map = { | ||
trio.TooSlowError: ConnectTimeout, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to check - Do we want
local_addr
orsource_addr
here?What are trio, asyncio, and the sync stdlib using for their naming?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
asyncio uses
local_addr
, trio useslocal_address
, and the stdlib socket module usessource_address
(and curio usessource_addr
). There's really no consistency.