2525
2626
2727class Transport (abstract .Transport ):
28- _connection_timeout_seconds = 20
29- _request_max_retries = 2
30-
3128 def __init__ (self , logger , endpoint = None , max_connections = None , timeout = None , verbosity = None ):
3229 super (Transport , self ).__init__ (logger , endpoint , max_connections , timeout , verbosity )
3330
@@ -39,19 +36,24 @@ def __init__(self, logger, endpoint=None, max_connections=None, timeout=None, ve
3936 # create the pool connection
4037 self ._create_connections (self .max_connections , self ._host , self ._ssl_context )
4138
42- self ._wait_response_exceptions = (
43- http .client .RemoteDisconnected ,
44- ConnectionResetError ,
45- ConnectionRefusedError ,
46- http .client .ResponseNotReady ,
47- )
48- self ._send_request_exceptions = (
49- BrokenPipeError ,
50- http .client .CannotSendRequest ,
51- http .client .RemoteDisconnected ,
52- socket .timeout ,
53- )
54- self ._get_status_and_headers = self ._get_status_and_headers_py3
39+ # python 2 and 3 have different exceptions
40+ if sys .version_info [0 ] >= 3 :
41+ self ._wait_response_exceptions = (
42+ http .client .RemoteDisconnected ,
43+ ConnectionResetError ,
44+ ConnectionRefusedError ,
45+ http .client .ResponseNotReady ,
46+ )
47+ self ._send_request_exceptions = (
48+ BrokenPipeError ,
49+ http .client .CannotSendRequest ,
50+ http .client .RemoteDisconnected ,
51+ )
52+ self ._get_status_and_headers = self ._get_status_and_headers_py3
53+ else :
54+ self ._wait_response_exceptions = (http .client .BadStatusLine , socket .error )
55+ self ._send_request_exceptions = (http .client .CannotSendRequest , http .client .BadStatusLine )
56+ self ._get_status_and_headers = self ._get_status_and_headers_py2
5557
5658 def close (self ):
5759 # Ignore redundant calls to close
@@ -152,27 +154,20 @@ def _send_request_on_connection(self, request, connection):
152154 self .log (
153155 "Tx" , connection = connection , method = request .method , path = path , headers = request .headers , body = request .body
154156 )
155-
156157 starting_offset = 0
157158 is_body_seekable = request .body and hasattr (request .body , "seek" ) and hasattr (request .body , "tell" )
158159 if is_body_seekable :
159160 starting_offset = request .body .tell ()
160-
161- retries_left = self ._request_max_retries
162- while True :
161+ try :
163162 try :
164163 connection .request (request .method , path , request .body , request .headers )
165- break
166164 except self ._send_request_exceptions as e :
167165 self ._logger .debug_with (
168- f"Disconnected while attempting to send request – "
169- f"{ retries_left } out of { self ._request_max_retries } retries left." ,
166+ "Disconnected while attempting to send. Recreating connection and retrying" ,
170167 e = type (e ),
171168 e_msg = e ,
169+ connection = connection ,
172170 )
173- if retries_left == 0 :
174- raise
175- retries_left -= 1
176171 connection .close ()
177172 if is_body_seekable :
178173 # If the first connection fails, the pointer of the body might move at the size
@@ -181,11 +176,12 @@ def _send_request_on_connection(self, request, connection):
181176 request .body .seek (starting_offset )
182177 connection = self ._create_connection (self ._host , self ._ssl_context )
183178 request .transport .connection_used = connection
184- except BaseException as e :
185- self ._logger .error_with (
186- "Unhandled exception while sending request" , e = type (e ), e_msg = e , connection = connection
187- )
188- raise e
179+ connection .request (request .method , path , request .body , request .headers )
180+ except BaseException as e :
181+ self ._logger .error_with (
182+ "Unhandled exception while sending request" , e = type (e ), e_msg = e , connection = connection
183+ )
184+ raise e
189185
190186 return request
191187
@@ -196,9 +192,9 @@ def _create_connections(self, num_connections, host, ssl_context):
196192
197193 def _create_connection (self , host , ssl_context ):
198194 if ssl_context is None :
199- return http .client .HTTPConnection (host , timeout = self . _connection_timeout_seconds )
195+ return http .client .HTTPConnection (host )
200196
201- return http .client .HTTPSConnection (host , timeout = self . _connection_timeout_seconds , context = ssl_context )
197+ return http .client .HTTPSConnection (host , context = ssl_context )
202198
203199 def _parse_endpoint (self , endpoint ):
204200 if endpoint .startswith ("http://" ):
0 commit comments