Skip to content

Commit df35f03

Browse files
author
Fran Garcia
committed
address comments (take 2)
1 parent 9570abf commit df35f03

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

baseplate/clients/redis_cluster.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
from typing import Any
44
from typing import Dict
55

6-
import rediscluster # type: ignore
6+
import rediscluster
77

8-
from rediscluster.pipeline import ClusterPipeline # type: ignore
8+
from rediscluster.pipeline import ClusterPipeline
99

1010
from baseplate import Span
1111
from baseplate.clients import ContextFactory
@@ -14,20 +14,23 @@
1414

1515

1616
# We want to be able to combine blocking behaviour with the ability to read from replicas
17-
# Unfortunately this is not provide as-is so we cmobine two connection pool classes to provide
17+
# Unfortunately this is not provide as-is so we combine two connection pool classes to provide
1818
# the desired behaviour.
1919
class ClusterWithReadReplicasBlockingConnectionPool(rediscluster.ClusterBlockingConnectionPool):
2020
# pylint: disable=arguments-differ
2121
def get_node_by_slot(self, slot: int, read_command: bool = False) -> Dict[str, Any]:
2222
"""
23-
Get a random node from the slot, including master
23+
Get a node from the slot.
24+
If the command is a read command we'll try to return a random replica.
25+
If there are no replicas or this isn't a read command we'll return the primary.
2426
"""
25-
nodes_in_slot = self.nodes.slots[slot]
26-
if read_command:
27-
random_index = random.randrange(1, len(nodes_in_slot))
28-
return nodes_in_slot[random_index]
27+
primary, *replicas = self.nodes.slots[slot]
2928

30-
return nodes_in_slot[0]
29+
if replicas and read_command:
30+
return random.choice(replicas)
31+
32+
# Either this isn't a read command or there aren't any replicas
33+
return primary
3134

3235

3336
def cluster_pool_from_config(
@@ -60,7 +63,7 @@ def cluster_pool_from_config(
6063
parser = config.SpecParser(
6164
{
6265
"url": config.String,
63-
"max_connections": config.Optional(config.Integer, default=None),
66+
"max_connections": config.Optional(config.Integer, default=50),
6467
"timeout": config.Optional(config.Timespan, default=100),
6568
"read_from_replicas": config.Optional(config.Boolean, default=False),
6669
"skip_full_coverage_check": config.Optional(config.Boolean, default=True),

setup.cfg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ ignore_missing_imports = True
102102
[mypy-pythonjsonlogger.*]
103103
ignore_missing_imports = True
104104

105+
[mypy-rediscluster.*]
106+
ignore_missing_imports = True
107+
105108
[mypy-sqlalchemy.*]
106109
ignore_missing_imports = True
107110

tests/integration/redis_tests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from baseplate.clients.redis import RedisClient
1010
from baseplate import Baseplate
11+
1112
from . import TestBaseplateObserver, get_endpoint_or_skip_container
1213

1314
from baseplate.clients.redis import MessageQueue

0 commit comments

Comments
 (0)