Skip to content

Commit 4a381e1

Browse files
committed
Add automatic connection pool close for redis.sentinel
1 parent 72ff761 commit 4a381e1

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

redis/sentinel.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -354,9 +354,7 @@ def master_for(
354354
connection_kwargs = dict(self.connection_kwargs)
355355
connection_kwargs.update(kwargs)
356356
return redis_class(
357-
connection_pool=connection_pool_class(
358-
service_name, self, **connection_kwargs
359-
)
357+
from_pool=connection_pool_class(service_name, self, **connection_kwargs)
360358
)
361359

362360
def slave_for(
@@ -387,7 +385,5 @@ def slave_for(
387385
connection_kwargs = dict(self.connection_kwargs)
388386
connection_kwargs.update(kwargs)
389387
return redis_class(
390-
connection_pool=connection_pool_class(
391-
service_name, self, **connection_kwargs
392-
)
388+
from_pool=connection_pool_class(service_name, self, **connection_kwargs)
393389
)

tests/test_sentinel.py

Lines changed: 26 additions & 0 deletions
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)