Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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