Skip to content

Commit

Permalink
fix(redis-cluster): add lower socket timeout (#3401)
Browse files Browse the repository at this point in the history
  • Loading branch information
gagantrivedi authored Feb 12, 2024
1 parent 8002d81 commit 37b89b3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
9 changes: 5 additions & 4 deletions api/core/redis_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
from django_redis.client.default import DefaultClient
from django_redis.exceptions import ConnectionInterrupted
from django_redis.pool import ConnectionFactory
from redis.backoff import DecorrelatedJitterBackoff
from redis.cluster import RedisCluster
from redis.exceptions import RedisClusterException
from redis.retry import Retry

SOCKET_TIMEOUT = 0.2


class SafeRedisClusterClient(DefaultClient):
Expand Down Expand Up @@ -118,8 +118,9 @@ def get_connection(self, connection_params: dict) -> RedisCluster:
)
client_cls_kwargs[key] = value

# Add explicit retry
client_cls_kwargs["retry"] = Retry(DecorrelatedJitterBackoff(), 3)
# Add explicit socket timeout
client_cls_kwargs["socket_timeout"] = SOCKET_TIMEOUT
client_cls_kwargs["socket_keepalive"] = True
# ... and then build and return the client
return RedisCluster(**client_cls_kwargs)
except Exception as e:
Expand Down
6 changes: 2 additions & 4 deletions api/tests/unit/core/test_redis_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ def test_cluster_connection_factory__get_connection_with_non_conflicting_params(
):
# Given
mockRedisCluster = mocker.patch("core.redis_cluster.RedisCluster")
mockedRetry = mocker.patch("core.redis_cluster.Retry")
mockedBackoff = mocker.patch("core.redis_cluster.DecorrelatedJitterBackoff")
connection_factory = ClusterConnectionFactory(
options={"REDIS_CLIENT_KWARGS": {"decode_responses": False}}
)
Expand All @@ -57,9 +55,9 @@ def test_cluster_connection_factory__get_connection_with_non_conflicting_params(
decode_responses=False,
host="localhost",
port=6379,
retry=mockedRetry.return_value,
socket_keepalive=True,
socket_timeout=0.2,
)
mockedRetry.assert_called_once_with(mockedBackoff(), 3)


def test_cluster_connection_factory__get_connection_with_conflicting_params(
Expand Down

0 comments on commit 37b89b3

Please sign in to comment.