1919from typing import Union
2020
2121from pyignite .constants import PROTOCOLS , IGNITE_DEFAULT_HOST , IGNITE_DEFAULT_PORT , PROTOCOL_BYTE_ORDER
22- from pyignite .exceptions import HandshakeError , SocketError , connection_errors , AuthenticationError
22+ from pyignite .exceptions import HandshakeError , SocketError , connection_errors , AuthenticationError , ParameterError
2323from .bitmask_feature import BitmaskFeature
2424
2525from .handshake import HandshakeRequest , HandshakeResponse
3434
3535class BaseConnection :
3636 def __init__ (self , client , host : str = None , port : int = None , username : str = None , password : str = None ,
37- ** ssl_params ):
37+ handshake_timeout : float = 10.0 , ** ssl_params ):
3838 self .client = client
39+ self .handshake_timeout = handshake_timeout
3940 self .host = host if host else IGNITE_DEFAULT_HOST
4041 self .port = port if port else IGNITE_DEFAULT_PORT
4142 self .username = username
4243 self .password = password
4344 self .uuid = None
4445
46+ if handshake_timeout <= 0.0 :
47+ raise ParameterError ("handshake_timeout should be positive" )
48+
4549 check_ssl_params (ssl_params )
4650
4751 if self .username and self .password and 'use_ssl' not in ssl_params :
@@ -162,8 +166,9 @@ class Connection(BaseConnection):
162166 * binary protocol connector. Encapsulates handshake and failover reconnection.
163167 """
164168
165- def __init__ (self , client : 'Client' , host : str , port : int , timeout : float = None ,
166- username : str = None , password : str = None , ** ssl_params ):
169+ def __init__ (self , client : 'Client' , host : str , port : int , username : str = None , password : str = None ,
170+ timeout : float = None , handshake_timeout : float = 10.0 ,
171+ ** ssl_params ):
167172 """
168173 Initialize connection.
169174
@@ -177,11 +182,13 @@ def __init__(self, client: 'Client', host: str, port: int, timeout: float = None
177182 operation including `connect`. 0 means non-blocking mode, which is
178183 virtually guaranteed to fail. Can accept integer or float value.
179184 Default is None (blocking mode),
185+ :param handshake_timeout: (optional) sets timeout (in seconds) for performing handshake (connection)
186+ with node. Default is 10.0.
180187 :param use_ssl: (optional) set to True if Ignite server uses SSL
181188 on its binary connector. Defaults to use SSL when username
182189 and password has been supplied, not to use SSL otherwise,
183190 :param ssl_version: (optional) SSL version constant from standard
184- `ssl` module. Defaults to TLS v1.1, as in Ignite 2.5 ,
191+ `ssl` module. Defaults to TLS v1.2 ,
185192 :param ssl_ciphers: (optional) ciphers to use. If not provided,
186193 `ssl` default ciphers are used,
187194 :param ssl_cert_reqs: (optional) determines how the remote side
@@ -206,7 +213,7 @@ def __init__(self, client: 'Client', host: str, port: int, timeout: float = None
206213 cluster,
207214 :param password: (optional) password to authenticate to Ignite cluster.
208215 """
209- super ().__init__ (client , host , port , username , password , ** ssl_params )
216+ super ().__init__ (client , host , port , username , password , handshake_timeout , ** ssl_params )
210217 self .timeout = timeout
211218 self ._socket = None
212219
@@ -225,27 +232,29 @@ def connect(self):
225232 detecting_protocol = True
226233 self .client .protocol_context = ProtocolContext (max (PROTOCOLS ), BitmaskFeature .all_supported ())
227234
228- try :
229- self ._on_handshake_start ()
230- result = self ._connect_version ()
231- except HandshakeError as e :
232- if e .expected_version in PROTOCOLS :
233- self .client .protocol_context .version = e .expected_version
235+ while True :
236+ try :
237+ self ._on_handshake_start ()
234238 result = self ._connect_version ()
235- else :
239+ self ._socket .settimeout (self .timeout )
240+ self ._on_handshake_success (result )
241+ return
242+ except HandshakeError as e :
243+ if e .expected_version in PROTOCOLS :
244+ self .client .protocol_context .version = e .expected_version
245+ continue
246+ else :
247+ self ._on_handshake_fail (e )
248+ raise e
249+ except AuthenticationError as e :
236250 self ._on_handshake_fail (e )
237251 raise e
238- except AuthenticationError as e :
239- self ._on_handshake_fail (e )
240- raise e
241- except Exception as e :
242- self ._on_handshake_fail (e )
243- # restore undefined protocol version
244- if detecting_protocol :
245- self .client .protocol_context = None
246- raise e
247-
248- self ._on_handshake_success (result )
252+ except Exception as e :
253+ self ._on_handshake_fail (e )
254+ # restore undefined protocol version
255+ if detecting_protocol :
256+ self .client .protocol_context = None
257+ raise e
249258
250259 def _connect_version (self ) -> Union [dict , OrderedDict ]:
251260 """
@@ -254,7 +263,7 @@ def _connect_version(self) -> Union[dict, OrderedDict]:
254263 """
255264
256265 self ._socket = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
257- self ._socket .settimeout (self .timeout )
266+ self ._socket .settimeout (self .handshake_timeout )
258267 self ._socket = wrap (self ._socket , self .ssl_params )
259268 self ._socket .connect ((self .host , self .port ))
260269
0 commit comments