2121from tzlocal import get_localzone
2222
2323from pygridgain .constants import PROTOCOLS , DEFAULT_HOST , DEFAULT_PORT , PROTOCOL_BYTE_ORDER
24- from pygridgain .exceptions import HandshakeError , SocketError , connection_errors , AuthenticationError
24+ from pygridgain .exceptions import HandshakeError , SocketError , connection_errors , AuthenticationError , ParameterError
2525from .bitmask_feature import BitmaskFeature
2626
2727from .handshake import HandshakeRequest , HandshakeResponse
3636
3737class BaseConnection :
3838 def __init__ (self , client , host : str = None , port : int = None , username : str = None , password : str = None ,
39- ** ssl_params ):
39+ handshake_timeout : float = 10.0 , ** ssl_params ):
4040 self .client = client
41+ self .handshake_timeout = handshake_timeout
4142 self .host = host if host else DEFAULT_HOST
4243 self .port = port if port else DEFAULT_PORT
4344 self .username = username
@@ -46,6 +47,9 @@ def __init__(self, client, host: str = None, port: int = None, username: str = N
4647
4748 self .timezone = get_localzone ().tzname (None )
4849
50+ if handshake_timeout <= 0.0 :
51+ raise ParameterError ("handshake_timeout should be positive" )
52+
4953 check_ssl_params (ssl_params )
5054
5155 if self .username and self .password and 'use_ssl' not in ssl_params :
@@ -166,8 +170,9 @@ class Connection(BaseConnection):
166170 * binary protocol connector. Encapsulates handshake and failover reconnection.
167171 """
168172
169- def __init__ (self , client : 'Client' , host : str , port : int , timeout : float = None ,
170- username : str = None , password : str = None , ** ssl_params ):
173+ def __init__ (self , client : 'Client' , host : str , port : int , username : str = None , password : str = None ,
174+ timeout : float = None , handshake_timeout : float = 10.0 ,
175+ ** ssl_params ):
171176 """
172177 Initialize connection.
173178
@@ -181,11 +186,13 @@ def __init__(self, client: 'Client', host: str, port: int, timeout: float = None
181186 operation including `connect`. 0 means non-blocking mode, which is
182187 virtually guaranteed to fail. Can accept integer or float value.
183188 Default is None (blocking mode),
189+ :param handshake_timeout: (optional) sets timeout (in seconds) for performing handshake (connection)
190+ with node. Default is 10.0.
184191 :param use_ssl: (optional) set to True if GridGain server uses SSL
185192 on its binary connector. Defaults to use SSL when username
186193 and password has been supplied, not to use SSL otherwise,
187194 :param ssl_version: (optional) SSL version constant from standard
188- `ssl` module. Defaults to TLS v1.1, as in GridGain 8.5 ,
195+ `ssl` module. Defaults to TLS v1.2 ,
189196 :param ssl_ciphers: (optional) ciphers to use. If not provided,
190197 `ssl` default ciphers are used,
191198 :param ssl_cert_reqs: (optional) determines how the remote side
@@ -211,7 +218,7 @@ def __init__(self, client: 'Client', host: str, port: int, timeout: float = None
211218 :param password: (optional) password to authenticate to GridGain
212219 cluster.
213220 """
214- super ().__init__ (client , host , port , username , password , ** ssl_params )
221+ super ().__init__ (client , host , port , username , password , handshake_timeout , ** ssl_params )
215222 self .timeout = timeout
216223 self ._socket = None
217224
@@ -230,27 +237,29 @@ def connect(self):
230237 detecting_protocol = True
231238 self .client .protocol_context = ProtocolContext (max (PROTOCOLS ), BitmaskFeature .all_supported ())
232239
233- try :
234- self ._on_handshake_start ()
235- result = self ._connect_version ()
236- except HandshakeError as e :
237- if e .expected_version in PROTOCOLS :
238- self .client .protocol_context .version = e .expected_version
240+ while True :
241+ try :
242+ self ._on_handshake_start ()
239243 result = self ._connect_version ()
240- else :
244+ self ._socket .settimeout (self .timeout )
245+ self ._on_handshake_success (result )
246+ return
247+ except HandshakeError as e :
248+ if e .expected_version in PROTOCOLS :
249+ self .client .protocol_context .version = e .expected_version
250+ continue
251+ else :
252+ self ._on_handshake_fail (e )
253+ raise e
254+ except AuthenticationError as e :
241255 self ._on_handshake_fail (e )
242256 raise e
243- except AuthenticationError as e :
244- self ._on_handshake_fail (e )
245- raise e
246- except Exception as e :
247- self ._on_handshake_fail (e )
248- # restore undefined protocol version
249- if detecting_protocol :
250- self .client .protocol_context = None
251- raise e
252-
253- self ._on_handshake_success (result )
257+ except Exception as e :
258+ self ._on_handshake_fail (e )
259+ # restore undefined protocol version
260+ if detecting_protocol :
261+ self .client .protocol_context = None
262+ raise e
254263
255264 def _connect_version (self ) -> Union [dict , OrderedDict ]:
256265 """
@@ -259,7 +268,7 @@ def _connect_version(self) -> Union[dict, OrderedDict]:
259268 """
260269
261270 self ._socket = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
262- self ._socket .settimeout (self .timeout )
271+ self ._socket .settimeout (self .handshake_timeout )
263272 self ._socket = wrap (self ._socket , self .ssl_params )
264273 self ._socket .connect ((self .host , self .port ))
265274
0 commit comments