Skip to content

Commit 705bc74

Browse files
committed
Add automatic connection pool close for redis.sentinel
1 parent 8804ed2 commit 705bc74

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

redis/sentinel.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -349,9 +349,7 @@ def master_for(
349349
connection_kwargs = dict(self.connection_kwargs)
350350
connection_kwargs.update(kwargs)
351351
return redis_class(
352-
connection_pool=connection_pool_class(
353-
service_name, self, **connection_kwargs
354-
)
352+
from_pool=connection_pool_class(service_name, self, **connection_kwargs)
355353
)
356354

357355
def slave_for(
@@ -382,7 +380,5 @@ def slave_for(
382380
connection_kwargs = dict(self.connection_kwargs)
383381
connection_kwargs.update(kwargs)
384382
return redis_class(
385-
connection_pool=connection_pool_class(
386-
service_name, self, **connection_kwargs
387-
)
383+
from_pool=connection_pool_class(service_name, self, **connection_kwargs)
388384
)

tests/test_sentinel.py

+26
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import socket
2+
from unittest import mock
23

34
import pytest
45
import redis.sentinel
@@ -240,3 +241,28 @@ def test_flushconfig(cluster, sentinel):
240241
def test_reset(cluster, sentinel):
241242
cluster.master["is_odown"] = True
242243
assert sentinel.sentinel_reset("mymaster")
244+
245+
246+
@pytest.mark.onlynoncluster
247+
@pytest.mark.parametrize("method_name", ["master_for", "slave_for"])
248+
def test_auto_close_pool(cluster, sentinel, method_name):
249+
"""
250+
Check that the connection pool created by the sentinel client is
251+
automatically closed
252+
"""
253+
254+
method = getattr(sentinel, method_name)
255+
client = method("mymaster", db=9)
256+
pool = client.connection_pool
257+
assert client.auto_close_connection_pool is True
258+
calls = 0
259+
260+
def mock_disconnect():
261+
nonlocal calls
262+
calls += 1
263+
264+
with mock.patch.object(pool, "disconnect", mock_disconnect):
265+
client.close()
266+
267+
assert calls == 1
268+
pool.disconnect()

0 commit comments

Comments
 (0)