11import ssl
22import sys
33import http .client
4+ import socket
45
56import v3io .dataplane .response
67import v3io .dataplane .request
@@ -25,12 +26,12 @@ def __init__(self, logger, endpoint=None, max_connections=None, timeout=None, ve
2526
2627 # python 2 and 3 have different exceptions
2728 if sys .version_info [0 ] >= 3 :
28- self ._remote_disconnect_exception = http .client .RemoteDisconnected
29- self ._disconnection_exceptions = (BrokenPipeError , http .client .CannotSendRequest , http .client .RemoteDisconnected )
29+ self ._wait_response_exceptions = ( http .client .RemoteDisconnected , ConnectionResetError , ConnectionRefusedError )
30+ self ._send_request_exceptions = (BrokenPipeError , http .client .CannotSendRequest , http .client .RemoteDisconnected )
3031 self ._get_status_and_headers = self ._get_status_and_headers_py3
3132 else :
32- self ._remote_disconnect_exception = http .client .BadStatusLine
33- self ._disconnection_exceptions = (http .client .CannotSendRequest , http .client .BadStatusLine )
33+ self ._wait_response_exceptions = ( http .client .BadStatusLine , socket . error )
34+ self ._send_request_exceptions = (http .client .CannotSendRequest , http .client .BadStatusLine )
3435 self ._get_status_and_headers = self ._get_status_and_headers_py2
3536
3637 def restart (self ):
@@ -86,17 +87,30 @@ def wait_response(self, request, raise_for_status=None, num_retries=1):
8687 # return the response
8788 return response
8889
89- except self ._remote_disconnect_exception as e :
90+ except self ._wait_response_exceptions as e :
9091 if num_retries == 0 :
92+ self ._logger .warn_with ('Remote disconnected while waiting for response and ran out of retries' ,
93+ e = type (e ),
94+ connection_idx = connection_idx )
95+
9196 raise e
9297
98+ self ._logger .info_with ('Remote disconnected while waiting for response' ,
99+ retries_left = num_retries ,
100+ connection_idx = connection_idx )
101+
93102 num_retries -= 1
94103
95104 # create a connection
96105 connection = self ._recreate_connection_at_index (connection_idx )
97106
98107 # re-send the request on the connection
99108 request = self ._send_request_on_connection (request , connection_idx )
109+ except BaseException as e :
110+ self ._logger .warn_with ('Unhandled exception while waiting for response' ,
111+ e = type (e ),
112+ connection_idx = connection_idx )
113+ raise e
100114
101115 def _send_request_on_connection (self , request , connection_idx ):
102116 self .log ('Tx' ,
@@ -110,11 +124,16 @@ def _send_request_on_connection(self, request, connection_idx):
110124
111125 try :
112126 connection .request (request .method , request .path , request .body , request .headers )
113- except self ._disconnection_exceptions :
127+ except self ._send_request_exceptions as e :
128+ self ._logger .info_with ('Disconnected while attempting to send. Recreating connection' , e = type (e ))
129+
114130 connection = self ._recreate_connection_at_index (connection_idx )
115131
116132 # re-request
117133 connection .request (request .method , request .path , request .body , request .headers )
134+ except BaseException as e :
135+ self ._logger .warn_with ('Unhandled exception while sending request' , e = type (e ))
136+ raise e
118137
119138 return request
120139
0 commit comments