77import math
88import socket
99import typing as ty
10- import warnings
1110
11+ import httpcore
1212import httpx
1313
1414from . import encoding
2626 import typing_extensions
2727
2828 # By using the precise types from HTTPx we'll also get type errors if our
29- # types become somehow incompatible with the one from that library
29+ # types become somehow incompatible with the ones from that library
3030 RequestArgs = typing_extensions .TypedDict ("RequestArgs" , {
3131 "auth" : "httpx._types.AuthTypes" ,
3232 "cookies" : "httpx._types.CookieTypes" ,
@@ -75,9 +75,10 @@ def map_args_to_httpx(
7575
7676
7777class ClientSync (ClientSyncBase [httpx .Client ]):
78- __slots__ = ("_session_base" , "_session_kwargs" )
78+ __slots__ = ("_session_base" , "_session_kwargs" , "_session_laddr" )
7979 _session_base : "httpx._types.URLTypes"
8080 _session_kwargs : RequestArgs
81+ _session_laddr : ty .Optional [str ]
8182
8283 def _init (self , addr : addr_t , base : str , * , # type: ignore[no-any-unimported]
8384 auth : auth_t ,
@@ -90,12 +91,15 @@ def _init(self, addr: addr_t, base: str, *, # type: ignore[no-any-unimported]
9091 host_numeric : bool
9192 base_url , family , host_numeric = multiaddr_to_url_data (addr , base )
9293
93- #FIXME once https://github.com/encode/httpcore/pull/100 is released
94- if family != socket .AF_UNSPEC and not host_numeric :
95- warnings .warn (
96- "Restricting the address family on name lookups is not yet supported by HTTPx" ,
97- UserWarning
98- )
94+ self ._session_laddr = None
95+ if family != socket .AF_UNSPEC :
96+ if family == socket .AF_INET :
97+ self ._session_laddr = "0.0.0.0"
98+ elif family == socket .AF_INET6 :
99+ self ._session_laddr = "::"
100+ else :
101+ assert False , ("multiaddr_to_url_data should only return a socket "
102+ "address family of AF_INET, AF_INET6 or AF_UNSPEC" )
99103
100104 self ._session_base = base_url
101105 self ._session_kwargs = map_args_to_httpx (
@@ -107,7 +111,18 @@ def _init(self, addr: addr_t, base: str, *, # type: ignore[no-any-unimported]
107111 )
108112
109113 def _make_session (self ) -> httpx .Client :
110- return httpx .Client (** self ._session_kwargs , base_url = self ._session_base )
114+ connection_pool = httpcore .SyncConnectionPool (
115+ local_address = self ._session_laddr ,
116+
117+ #XXX: Argument values duplicated from httpx._client.Client._init_transport:
118+ keepalive_expiry = 5.0 , #XXX: Value duplicated from httpx._client.KEEPALIVE_EXPIRY
119+ max_connections = 100 , #XXX: Value duplicated from httpx._config.DEFAULT_LIMITS
120+ max_keepalive_connections = 20 , #XXX: Value duplicated from httpx._config.DEFAULT_LIMITS
121+ ssl_context = httpx .create_ssl_context (trust_env = True ),
122+ )
123+ return httpx .Client (** self ._session_kwargs ,
124+ base_url = self ._session_base ,
125+ transport = connection_pool )
111126
112127 def _do_raise_for_status (self , response : httpx .Response ) -> None :
113128 try :
0 commit comments