Skip to content

Commit

Permalink
Python: Add configuration for periodic checks interval
Browse files Browse the repository at this point in the history
  • Loading branch information
barshaul committed Mar 7, 2024
1 parent 068e114 commit 8944ffb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
19 changes: 17 additions & 2 deletions python/python/glide/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ class RedisClientConfiguration(BaseClientConfiguration):
reconnect_strategy (Optional[BackoffStrategy]): Strategy used to determine how and when to reconnect, in case of
connection failures.
If not set, a default backoff strategy will be used.
database_id (Optional[Int]): index of the logical database to connect to.
database_id (Optional[int]): index of the logical database to connect to.
client_name (Optional[str]): Client name to be used for the client. Will be used with CLIENT SETNAME command during connection establishment.
protocol (ProtocolVersion): The version of the Redis RESP protocol to communicate with the server.
"""
Expand Down Expand Up @@ -223,7 +223,6 @@ def __init__(
def _create_a_protobuf_conn_request(
self, cluster_mode: bool = False
) -> ConnectionRequest:
assert cluster_mode is False
request = super()._create_a_protobuf_conn_request(False)
if self.reconnect_strategy:
request.connection_retry_strategy.number_of_retries = (
Expand Down Expand Up @@ -259,6 +258,11 @@ class ClusterClientConfiguration(BaseClientConfiguration):
If the specified timeout is exceeded for a pending request, it will result in a timeout error. If not set, a default value will be used.
client_name (Optional[str]): Client name to be used for the client. Will be used with CLIENT SETNAME command during connection establishment.
protocol (ProtocolVersion): The version of the Redis RESP protocol to communicate with the server.
periodic_checks_interval (Optional[int]): Define the interval in seconds for periodic topology checks.
These checks evaluate changes in the cluster's topology, triggering a slot refresh when detected.
Periodic checks ensure a quick and efficient process by querying a limited number of nodes.
If no interval is set or the interval is 0, a default interval value will be used.
To disable periodic checks, pass -1 as the interval.
Notes:
Currently, the reconnection strategy in cluster mode is not configurable, and exponential backoff
Expand All @@ -274,6 +278,7 @@ def __init__(
request_timeout: Optional[int] = None,
client_name: Optional[str] = None,
protocol: ProtocolVersion = ProtocolVersion.RESP3,
periodic_checks_interval: Optional[int] = None,
):
super().__init__(
addresses=addresses,
Expand All @@ -284,3 +289,13 @@ def __init__(
client_name=client_name,
protocol=protocol,
)
self.periodic_checks_interval = periodic_checks_interval

def _create_a_protobuf_conn_request(
self, cluster_mode: bool = False
) -> ConnectionRequest:
request = super()._create_a_protobuf_conn_request(True)
if self.periodic_checks_interval:
request.periodic_checks_interval = self.periodic_checks_interval

return request
19 changes: 18 additions & 1 deletion python/python/tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Copyright GLIDE-for-Redis Project Contributors - SPDX Identifier: Apache-2.0

from glide.config import BaseClientConfiguration, NodeAddress, ReadFrom
from glide.config import (
BaseClientConfiguration,
ClusterClientConfiguration,
NodeAddress,
ReadFrom,
)
from glide.protobuf.connection_request_pb2 import ConnectionRequest
from glide.protobuf.connection_request_pb2 import ReadFrom as ProtobufReadFrom
from glide.protobuf.connection_request_pb2 import TlsMode
Expand Down Expand Up @@ -28,3 +33,15 @@ def test_convert_to_protobuf():
assert request.tls_mode is TlsMode.SecureTls
assert request.read_from == ProtobufReadFrom.PreferReplica
assert request.client_name == "TEST_CLIENT_NAME"


def test_periodic_checks_interval_to_protobuf():
config = ClusterClientConfiguration(
[NodeAddress("127.0.0.1")],
)
request = config._create_a_protobuf_conn_request()
assert request.periodic_checks_interval == 0

config.periodic_checks_interval = 30
request = config._create_a_protobuf_conn_request()
assert request.periodic_checks_interval == 30

0 comments on commit 8944ffb

Please sign in to comment.