@@ -249,6 +249,8 @@ class BaseClientConfiguration:
249249 reconnect_strategy (Optional[BackoffStrategy]): Strategy used to determine how and when to reconnect, in case of
250250 connection failures.
251251 If not set, a default backoff strategy will be used.
252+ database_id (Optional[int]): Index of the logical database to connect to.
253+ If not set, the client will connect to database 0.
252254 client_name (Optional[str]): Client name to be used for the client. Will be used with CLIENT SETNAME command
253255 during connection establishment.
254256 protocol (ProtocolVersion): Serialization protocol to be used. If not set, `RESP3` will be used.
@@ -292,6 +294,7 @@ def __init__(
292294 read_from : ReadFrom = ReadFrom .PRIMARY ,
293295 request_timeout : Optional [int ] = None ,
294296 reconnect_strategy : Optional [BackoffStrategy ] = None ,
297+ database_id : Optional [int ] = None ,
295298 client_name : Optional [str ] = None ,
296299 protocol : ProtocolVersion = ProtocolVersion .RESP3 ,
297300 inflight_requests_limit : Optional [int ] = None ,
@@ -305,13 +308,23 @@ def __init__(
305308 self .read_from = read_from
306309 self .request_timeout = request_timeout
307310 self .reconnect_strategy = reconnect_strategy
311+ self .database_id = database_id
308312 self .client_name = client_name
309313 self .protocol = protocol
310314 self .inflight_requests_limit = inflight_requests_limit
311315 self .client_az = client_az
312316 self .advanced_config = advanced_config
313317 self .lazy_connect = lazy_connect
314318
319+ # Validate database_id parameter
320+ if database_id is not None :
321+ if not isinstance (database_id , int ):
322+ raise ValueError ("database_id must be an integer" )
323+ if database_id < 0 :
324+ raise ValueError ("database_id must be non-negative" )
325+ if database_id > 15 :
326+ raise ValueError ("database_id must be less than or equal to 15" )
327+
315328 if read_from == ReadFrom .AZ_AFFINITY and not client_az :
316329 raise ValueError (
317330 "client_az must be set when read_from is set to AZ_AFFINITY"
@@ -322,6 +335,39 @@ def __init__(
322335 "client_az must be set when read_from is set to AZ_AFFINITY_REPLICAS_AND_PRIMARY"
323336 )
324337
338+ def _set_addresses_in_request (self , request : ConnectionRequest ) -> None :
339+ """Set addresses in the protobuf request."""
340+ for address in self .addresses :
341+ address_info = request .addresses .add ()
342+ address_info .host = address .host
343+ address_info .port = address .port
344+
345+ def _set_reconnect_strategy_in_request (self , request : ConnectionRequest ) -> None :
346+ """Set reconnect strategy in the protobuf request."""
347+ if not self .reconnect_strategy :
348+ return
349+
350+ request .connection_retry_strategy .number_of_retries = (
351+ self .reconnect_strategy .num_of_retries
352+ )
353+ request .connection_retry_strategy .factor = self .reconnect_strategy .factor
354+ request .connection_retry_strategy .exponent_base = (
355+ self .reconnect_strategy .exponent_base
356+ )
357+ if self .reconnect_strategy .jitter_percent is not None :
358+ request .connection_retry_strategy .jitter_percent = (
359+ self .reconnect_strategy .jitter_percent
360+ )
361+
362+ def _set_credentials_in_request (self , request : ConnectionRequest ) -> None :
363+ """Set credentials in the protobuf request."""
364+ if not self .credentials :
365+ return
366+
367+ if self .credentials .username :
368+ request .authentication_info .username = self .credentials .username
369+ request .authentication_info .password = self .credentials .password
370+
325371 def _create_a_protobuf_conn_request (
326372 self , cluster_mode : bool = False
327373 ) -> ConnectionRequest :
@@ -335,44 +381,34 @@ def _create_a_protobuf_conn_request(
335381 ConnectionRequest: Protobuf ConnectionRequest.
336382 """
337383 request = ConnectionRequest ()
338- for address in self .addresses :
339- address_info = request .addresses .add ()
340- address_info .host = address .host
341- address_info .port = address .port
384+
385+ # Set basic configuration
386+ self ._set_addresses_in_request (request )
342387 request .tls_mode = TlsMode .SecureTls if self .use_tls else TlsMode .NoTls
343388 request .read_from = self .read_from .value
389+ request .cluster_mode_enabled = cluster_mode
390+ request .protocol = self .protocol .value
391+
392+ # Set optional configuration
344393 if self .request_timeout :
345394 request .request_timeout = self .request_timeout
346- if self .reconnect_strategy :
347- request .connection_retry_strategy .number_of_retries = (
348- self .reconnect_strategy .num_of_retries
349- )
350- request .connection_retry_strategy .factor = self .reconnect_strategy .factor
351- request .connection_retry_strategy .exponent_base = (
352- self .reconnect_strategy .exponent_base
353- )
354- if self .reconnect_strategy .jitter_percent is not None :
355- request .connection_retry_strategy .jitter_percent = (
356- self .reconnect_strategy .jitter_percent
357- )
358395
359- request .cluster_mode_enabled = True if cluster_mode else False
360- if self .credentials :
361- if self .credentials .username :
362- request .authentication_info .username = self .credentials .username
363- request .authentication_info .password = self .credentials .password
396+ self ._set_reconnect_strategy_in_request (request )
397+ self ._set_credentials_in_request (request )
398+
364399 if self .client_name :
365400 request .client_name = self .client_name
366- request .protocol = self .protocol .value
367401 if self .inflight_requests_limit :
368402 request .inflight_requests_limit = self .inflight_requests_limit
369403 if self .client_az :
370404 request .client_az = self .client_az
405+ if self .database_id is not None :
406+ request .database_id = self .database_id
371407 if self .advanced_config :
372408 self .advanced_config ._create_a_protobuf_conn_request (request )
373-
374409 if self .lazy_connect is not None :
375410 request .lazy_connect = self .lazy_connect
411+
376412 return request
377413
378414 def _is_pubsub_configured (self ) -> bool :
@@ -425,7 +461,8 @@ class GlideClientConfiguration(BaseClientConfiguration):
425461 reconnect_strategy (Optional[BackoffStrategy]): Strategy used to determine how and when to reconnect, in case of
426462 connection failures.
427463 If not set, a default backoff strategy will be used.
428- database_id (Optional[int]): index of the logical database to connect to.
464+ database_id (Optional[int]): Index of the logical database to connect to.
465+ If not set, the client will connect to database 0.
429466 client_name (Optional[str]): Client name to be used for the client. Will be used with CLIENT SETNAME command during
430467 connection establishment.
431468 protocol (ProtocolVersion): The version of the RESP protocol to communicate with the server.
@@ -500,23 +537,21 @@ def __init__(
500537 read_from = read_from ,
501538 request_timeout = request_timeout ,
502539 reconnect_strategy = reconnect_strategy ,
540+ database_id = database_id ,
503541 client_name = client_name ,
504542 protocol = protocol ,
505543 inflight_requests_limit = inflight_requests_limit ,
506544 client_az = client_az ,
507545 advanced_config = advanced_config ,
508546 lazy_connect = lazy_connect ,
509547 )
510- self .database_id = database_id
511548 self .pubsub_subscriptions = pubsub_subscriptions
512549
513550 def _create_a_protobuf_conn_request (
514551 self , cluster_mode : bool = False
515552 ) -> ConnectionRequest :
516553 assert cluster_mode is False
517554 request = super ()._create_a_protobuf_conn_request (cluster_mode )
518- if self .database_id :
519- request .database_id = self .database_id
520555
521556 if self .pubsub_subscriptions :
522557 if self .protocol == ProtocolVersion .RESP2 :
@@ -592,6 +627,8 @@ class GlideClusterClientConfiguration(BaseClientConfiguration):
592627 reconnect_strategy (Optional[BackoffStrategy]): Strategy used to determine how and when to reconnect, in case of
593628 connection failures.
594629 If not set, a default backoff strategy will be used.
630+ database_id (Optional[int]): Index of the logical database to connect to.
631+ If not set, the client will connect to database 0.
595632 client_name (Optional[str]): Client name to be used for the client. Will be used with CLIENT SETNAME command during
596633 connection establishment.
597634 protocol (ProtocolVersion): The version of the RESP protocol to communicate with the server.
@@ -661,6 +698,7 @@ def __init__(
661698 read_from : ReadFrom = ReadFrom .PRIMARY ,
662699 request_timeout : Optional [int ] = None ,
663700 reconnect_strategy : Optional [BackoffStrategy ] = None ,
701+ database_id : Optional [int ] = None ,
664702 client_name : Optional [str ] = None ,
665703 protocol : ProtocolVersion = ProtocolVersion .RESP3 ,
666704 periodic_checks : Union [
@@ -679,6 +717,7 @@ def __init__(
679717 read_from = read_from ,
680718 request_timeout = request_timeout ,
681719 reconnect_strategy = reconnect_strategy ,
720+ database_id = database_id ,
682721 client_name = client_name ,
683722 protocol = protocol ,
684723 inflight_requests_limit = inflight_requests_limit ,
0 commit comments