Skip to content

Commit

Permalink
feat: Implemented SentinelBlockingConnectionPool.
Browse files Browse the repository at this point in the history
  • Loading branch information
DABND19 committed Jul 17, 2024
1 parent b7f9a4c commit c8eecd5
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
* Add SentinelBlockingConnectionPool class
* Move doctests (doc code examples) to main branch
* Update `ResponseT` type hint
* Allow to control the minimum SSL version
Expand Down
5 changes: 5 additions & 0 deletions docs/connections.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ SentinelConnectionPool
.. autoclass:: redis.sentinel.SentinelConnectionPool
:members:

SentinelBlockingConnectionPool
======================
.. autoclass:: redis.sentinel.SentinelBlockingConnectionPool
:members:


Cluster Client
**************
Expand Down
2 changes: 2 additions & 0 deletions redis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
)
from redis.sentinel import (
Sentinel,
SentinelBlockingConnectionPool,
SentinelConnectionPool,
SentinelManagedConnection,
SentinelManagedSSLConnection,
Expand Down Expand Up @@ -77,6 +78,7 @@ def int_or_str(value):
"RedisError",
"ResponseError",
"Sentinel",
"SentinelBlockingConnectionPool",
"SentinelConnectionPool",
"SentinelManagedConnection",
"SentinelManagedSSLConnection",
Expand Down
2 changes: 2 additions & 0 deletions redis/asyncio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
)
from redis.asyncio.sentinel import (
Sentinel,
SentinelBlockingConnectionPool,
SentinelConnectionPool,
SentinelManagedConnection,
SentinelManagedSSLConnection,
Expand Down Expand Up @@ -53,6 +54,7 @@
"RedisError",
"ResponseError",
"Sentinel",
"SentinelBlockingConnectionPool",
"SentinelConnectionPool",
"SentinelManagedConnection",
"SentinelManagedSSLConnection",
Expand Down
31 changes: 23 additions & 8 deletions redis/asyncio/sentinel.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from redis.asyncio.client import Redis
from redis.asyncio.connection import (
BlockingConnectionPool,
Connection,
ConnectionPool,
EncodableT,
Expand Down Expand Up @@ -97,14 +98,7 @@ class SentinelManagedSSLConnection(SentinelManagedConnection, SSLConnection):
pass


class SentinelConnectionPool(ConnectionPool):
"""
Sentinel backed connection pool.
If ``check_connection`` flag is set to True, SentinelManagedConnection
sends a PING command right after establishing the connection.
"""

class _SentinelConnectionPoolMixin:
def __init__(self, service_name, sentinel_manager, **kwargs):
kwargs["connection_class"] = kwargs.get(
"connection_class",
Expand Down Expand Up @@ -168,6 +162,27 @@ async def rotate_slaves(self) -> AsyncIterator:
raise SlaveNotFoundError(f"No slave found for {self.service_name!r}")


class SentinelConnectionPool(_SentinelConnectionPoolMixin, ConnectionPool):
"""
Sentinel backed connection pool.
If ``check_connection`` flag is set to True, SentinelManagedConnection
sends a PING command right after establishing the connection.
"""


class SentinelBlockingConnectionPool(
_SentinelConnectionPoolMixin,
BlockingConnectionPool,
):
"""
Sentinel blocking connection pool.
If ``check_connection`` flag is set to True, SentinelManagedConnection
sends a PING command right after establishing the connection.
"""


class Sentinel(AsyncSentinelCommands):
"""
Redis Sentinel cluster client
Expand Down
37 changes: 28 additions & 9 deletions redis/sentinel.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@

from redis.client import Redis
from redis.commands import SentinelCommands
from redis.connection import Connection, ConnectionPool, SSLConnection
from redis.connection import (
BlockingConnectionPool,
Connection,
ConnectionPool,
SSLConnection,
)
from redis.exceptions import ConnectionError, ReadOnlyError, ResponseError, TimeoutError
from redis.utils import str_if_bytes

Expand Down Expand Up @@ -134,14 +139,7 @@ def rotate_slaves(self):
raise SlaveNotFoundError(f"No slave found for {self.service_name!r}")


class SentinelConnectionPool(ConnectionPool):
"""
Sentinel backed connection pool.
If ``check_connection`` flag is set to True, SentinelManagedConnection
sends a PING command right after establishing the connection.
"""

class _SentinelConnectionPoolMixin:
def __init__(self, service_name, sentinel_manager, **kwargs):
kwargs["connection_class"] = kwargs.get(
"connection_class",
Expand Down Expand Up @@ -195,6 +193,27 @@ def rotate_slaves(self):
return self.proxy.rotate_slaves()


class SentinelConnectionPool(_SentinelConnectionPoolMixin, ConnectionPool):
"""
Sentinel backed connection pool.
If ``check_connection`` flag is set to True, SentinelManagedConnection
sends a PING command right after establishing the connection.
"""


class SentinelBlockingConnectionPool(
_SentinelConnectionPoolMixin,
BlockingConnectionPool,
):
"""
Sentinel blocking connection pool.
If ``check_connection`` flag is set to True, SentinelManagedConnection
sends a PING command right after establishing the connection.
"""


class Sentinel(SentinelCommands):
"""
Redis Sentinel cluster client
Expand Down

0 comments on commit c8eecd5

Please sign in to comment.