4949
5050
5151PROTOCOL_VERSION = 1
52+ MESSAGE_LENGTH_FORMAT = '@I'
53+ MESSAGE_LENGTH_SIZE = struct .calcsize (MESSAGE_LENGTH_FORMAT )
54+
55+ # Protocol types
56+ PROTOCOL_TCP = 1
57+ PROTOCOL_SSL = 2
58+
59+ DEFAULT_PORT = 55555
60+ # aperturedb's param ADB_MAX_CONNECTION_MESSAGE_SIZE_MB = 2048 by default
61+ DEFAULT_MAX_MESSAGE_SIZE_MB = 2048
62+
63+ # Query retry constants
64+ DEFAULT_RETRY_INTERVAL_SECONDS = 1
65+ DEFAULT_RETRY_MAX_ATTEMPTS = 3
66+ DEFAULT_SESSION_EXPIRY_OFFSET_SEC = 10
67+ DEFAULT_QUERY_CONNECTION_ERROR_SUPPRESSION_DELTA_SEC = 30
68+
69+ # Session renewal constants
70+ RENEW_SESSION_MAX_ATTEMPTS = 3
71+ RENEW_SESSION_RETRY_INTERVAL_SEC = 1
72+
73+ # Status codes
74+ STATUS_OK = 0
75+ STATUS_ERROR_DEFAULT = - 2
5276
5377
5478class UnauthorizedException (Exception ):
@@ -73,7 +97,7 @@ def valid(self) -> bool:
7397
7498 # This triggers refresh if the session is about to expire.
7599 if session_age > self .session_token_ttl - \
76- int (os .getenv ("SESSION_EXPIRY_OFFSET_SEC" , 10 )):
100+ int (os .getenv ("SESSION_EXPIRY_OFFSET_SEC" , DEFAULT_SESSION_EXPIRY_OFFSET_SEC )):
77101 return False
78102
79103 return True
@@ -106,14 +130,14 @@ class Connector(object):
106130 str (key): Apeture Key, configuration as a deflated compressed string
107131 """
108132
109- def __init__ (self , host = "localhost" , port = 55555 ,
133+ def __init__ (self , host = "localhost" , port = DEFAULT_PORT ,
110134 user = "" , password = "" , token = "" ,
111135 use_ssl = True ,
112136 shared_data = None ,
113137 authenticate = True ,
114138 use_keepalive = True ,
115- retry_interval_seconds = 1 ,
116- retry_max_attempts = 3 ,
139+ retry_interval_seconds = DEFAULT_RETRY_INTERVAL_SECONDS ,
140+ retry_max_attempts = DEFAULT_RETRY_MAX_ATTEMPTS ,
117141 config : Optional [Configuration ] = None ,
118142 key : Optional [str ] = None ):
119143 """
@@ -126,7 +150,8 @@ def __init__(self, host="localhost", port=55555,
126150 self .last_query_timestamp = None
127151 # suppress connection warnings which occur more than this time
128152 # after the last query
129- self .query_connection_error_suppression_delta = timedelta (seconds = 30 )
153+ self .query_connection_error_suppression_delta = timedelta (
154+ seconds = DEFAULT_QUERY_CONNECTION_ERROR_SUPPRESSION_DELTA_SEC )
130155
131156 if key is not None :
132157 self .config = Configuration .reinflate (key )
@@ -196,20 +221,20 @@ def __del__(self):
196221 self .connected = False
197222
198223 def _send_msg (self , data ):
199- # aperturedb's param ADB_MAX_CONNECTION_MESSAGE_SIZE_MB = 256 by default
200- if len (data ) > (256 * 2 ** 20 ):
224+ if len (data ) > (DEFAULT_MAX_MESSAGE_SIZE_MB * 2 ** 20 ):
201225 logger .warning (
202226 "Message sent is larger than default for ApertureDB Server. Server may disconnect." )
203227
204- sent_len = struct .pack ('@I' , len (data )) # send size first
228+ sent_len = struct .pack (MESSAGE_LENGTH_FORMAT ,
229+ len (data )) # send size first
205230 x = self .conn .send (sent_len + data )
206- return x == len (data ) + 4
231+ return x == len (data ) + MESSAGE_LENGTH_SIZE
207232
208233 def _recv_msg (self ):
209- recv_len = self .conn .recv (4 ) # get message size
234+ recv_len = self .conn .recv (MESSAGE_LENGTH_SIZE ) # get message size
210235 if recv_len == b'' :
211236 return None
212- recv_len = struct .unpack ('@I' , recv_len )[0 ]
237+ recv_len = struct .unpack (MESSAGE_LENGTH_FORMAT , recv_len )[0 ]
213238 response = bytearray (recv_len )
214239 read = 0
215240 while read < recv_len :
@@ -250,7 +275,7 @@ def _authenticate(self, user, password="", token=""):
250275 "Unexpected response from server upon authenticate request: " +
251276 str (response ))
252277 session_info = response [0 ]["Authenticate" ]
253- if session_info ["status" ] != 0 :
278+ if session_info ["status" ] != STATUS_OK :
254279 raise Exception (session_info ["info" ])
255280
256281 self .shared_data .session = Session (
@@ -280,7 +305,7 @@ def _refresh_token(self):
280305 logger .info (f"Refresh token response: \r \n { response } " )
281306 if isinstance (response , list ):
282307 session_info = response [0 ]["RefreshToken" ]
283- if session_info ["status" ] != 0 :
308+ if session_info ["status" ] != STATUS_OK :
284309 # Refresh token failed, we need to re-authenticate
285310 # This is possible with a long lived connector, where
286311 # the session token and the refresh token have expired.
@@ -320,7 +345,7 @@ def _connect(self):
320345
321346 # Handshake with server to negotiate protocol
322347
323- protocol = 2 if self .use_ssl else 1
348+ protocol = PROTOCOL_SSL if self .use_ssl else PROTOCOL_TCP
324349
325350 hello_msg = struct .pack ('@II' , PROTOCOL_VERSION , protocol )
326351
@@ -513,16 +538,16 @@ def query(self, q, blobs=[]):
513538
514539 def _renew_session (self ):
515540 count = 0
516- while count < 3 :
541+ while count < RENEW_SESSION_MAX_ATTEMPTS :
517542 try :
518543 self ._check_session_status ()
519544 break
520545 except UnauthorizedException as e :
521546 logger .warning (
522- f"[Attempt { count + 1 } of 3 ] Failed to refresh token." ,
547+ f"[Attempt { count + 1 } of { RENEW_SESSION_MAX_ATTEMPTS } ] Failed to refresh token." ,
523548 exc_info = True ,
524549 stack_info = True )
525- time .sleep (1 )
550+ time .sleep (RENEW_SESSION_RETRY_INTERVAL_SEC )
526551 count += 1
527552
528553 def clone (self ) -> Connector :
@@ -583,7 +608,7 @@ def check_status(self, json_res: CommandResponses) -> int:
583608 int: The value recieved from the server, or -2 if not found.
584609 """
585610 # Default status is -2, which is an error, but not a server error.
586- status = - 2
611+ status = STATUS_ERROR_DEFAULT
587612 if (isinstance (json_res , dict )):
588613 if ("status" not in json_res ):
589614 status = self .check_status (json_res [list (json_res .keys ())[0 ]])
0 commit comments