Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: automatically close connection pool for async Sentinel #2900

Merged
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
24 changes: 16 additions & 8 deletions redis/asyncio/sentinel.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,11 +335,15 @@ def master_for(
kwargs["is_master"] = True
connection_kwargs = dict(self.connection_kwargs)
connection_kwargs.update(kwargs)
return redis_class(
connection_pool=connection_pool_class(
service_name, self, **connection_kwargs
)

connection_pool = connection_pool_class(service_name, self, **connection_kwargs)
# The Redis object "owns" the pool
auto_close_connection_pool = True
client = redis_class(
connection_pool=connection_pool,
)
client.auto_close_connection_pool = auto_close_connection_pool
return client

def slave_for(
self,
Expand Down Expand Up @@ -368,8 +372,12 @@ def slave_for(
kwargs["is_master"] = False
connection_kwargs = dict(self.connection_kwargs)
connection_kwargs.update(kwargs)
return redis_class(
connection_pool=connection_pool_class(
service_name, self, **connection_kwargs
)

connection_pool = connection_pool_class(service_name, self, **connection_kwargs)
# The Redis object "owns" the pool
auto_close_connection_pool = True
client = redis_class(
connection_pool=connection_pool,
)
client.auto_close_connection_pool = auto_close_connection_pool
return client
26 changes: 26 additions & 0 deletions tests/test_asyncio/test_sentinel.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import socket
from unittest import mock

import pytest
import pytest_asyncio
Expand Down Expand Up @@ -239,3 +240,28 @@ async def test_flushconfig(cluster, sentinel):
async def test_reset(cluster, sentinel):
cluster.master["is_odown"] = True
assert await sentinel.sentinel_reset("mymaster")


@pytest.mark.onlynoncluster
@pytest.mark.parametrize("method_name", ["master_for", "slave_for"])
async def test_auto_close_pool(cluster, sentinel, method_name):
"""
Check that the connection pool created by the sentinel client is
automatically closed
"""

method = getattr(sentinel, method_name)
client = method("mymaster", db=9)
pool = client.connection_pool
assert client.auto_close_connection_pool is True
calls = 0

async def mock_disconnect():
nonlocal calls
calls += 1

with mock.patch.object(pool, "disconnect", mock_disconnect):
await client.close()

assert calls == 1
await pool.disconnect()