Skip to content

Commit 97bd380

Browse files
committed
Adding better handling of configs and fixing tests
1 parent 2d52d45 commit 97bd380

File tree

6 files changed

+224
-163
lines changed

6 files changed

+224
-163
lines changed

redis/client.py

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,17 @@ def __init__(
278278
single_connection_client:
279279
if `True`, connection pool is not used. In that case `Redis`
280280
instance use is not thread safe.
281+
decode_responses:
282+
if `True`, the response will be decoded to utf-8.
283+
Argument is ignored when connection_pool is provided.
284+
maint_notifications_config:
285+
configuration the pool to support maintenance notifications - see
286+
`redis.maint_notifications.MaintNotificationsConfig` for details.
287+
Only supported with RESP3
288+
If not provided and protocol is RESP3, the maintenance notifications
289+
will be enabled by default (logic is included in the connection pool
290+
initialization).
291+
Argument is ignored when connection_pool is provided.
281292
"""
282293
if event_dispatcher is None:
283294
self._event_dispatcher = EventDispatcher()
@@ -354,6 +365,22 @@ def __init__(
354365
"cache_config": cache_config,
355366
}
356367
)
368+
maint_notifications_enabled = (
369+
maint_notifications_config and maint_notifications_config.enabled
370+
)
371+
if maint_notifications_enabled and protocol not in [
372+
3,
373+
"3",
374+
]:
375+
raise RedisError(
376+
"Maintenance notifications handlers on connection are only supported with RESP version 3"
377+
)
378+
if maint_notifications_config:
379+
kwargs.update(
380+
{
381+
"maint_notifications_config": maint_notifications_config,
382+
}
383+
)
357384
connection_pool = ConnectionPool(**kwargs)
358385
self._event_dispatcher.dispatch(
359386
AfterPooledConnectionsInstantiationEvent(
@@ -377,23 +404,6 @@ def __init__(
377404
]:
378405
raise RedisError("Client caching is only supported with RESP version 3")
379406

380-
if maint_notifications_config and self.connection_pool.get_protocol() not in [
381-
3,
382-
"3",
383-
]:
384-
raise RedisError(
385-
"Push handlers on connection are only supported with RESP version 3"
386-
)
387-
if maint_notifications_config and maint_notifications_config.enabled:
388-
self.maint_notifications_pool_handler = MaintNotificationsPoolHandler(
389-
self.connection_pool, maint_notifications_config
390-
)
391-
self.connection_pool.set_maint_notifications_pool_handler(
392-
self.maint_notifications_pool_handler
393-
)
394-
else:
395-
self.maint_notifications_pool_handler = None
396-
397407
self.single_connection_lock = threading.RLock()
398408
self.connection = None
399409
self._single_connection_client = single_connection_client
@@ -591,15 +601,9 @@ def monitor(self):
591601
return Monitor(self.connection_pool)
592602

593603
def client(self):
594-
maint_notifications_config = (
595-
None
596-
if self.maint_notifications_pool_handler is None
597-
else self.maint_notifications_pool_handler.config
598-
)
599604
return self.__class__(
600605
connection_pool=self.connection_pool,
601606
single_connection_client=True,
602-
maint_notifications_config=maint_notifications_config,
603607
)
604608

605609
def __enter__(self):

redis/cluster.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
WatchError,
5151
)
5252
from redis.lock import Lock
53+
from redis.maint_notifications import MaintNotificationsConfig
5354
from redis.retry import Retry
5455
from redis.utils import (
5556
deprecated_args,
@@ -1662,6 +1663,11 @@ def create_redis_node(self, host, port, **kwargs):
16621663
backoff=NoBackoff(), retries=0, supported_errors=(ConnectionError,)
16631664
)
16641665

1666+
protocol = kwargs.get("protocol", None)
1667+
if protocol in [3, "3"]:
1668+
kwargs.update(
1669+
{"maint_notifications_config": MaintNotificationsConfig(enabled=False)}
1670+
)
16651671
if self.from_url:
16661672
# Create a redis node with a costumed connection pool
16671673
kwargs.update({"host": host})

0 commit comments

Comments
 (0)