Skip to content

Commit

Permalink
Add close() method to redis.ConnectionPool
Browse files Browse the repository at this point in the history
  • Loading branch information
kristjanvalur committed Aug 17, 2023
1 parent abfb92e commit dd3e8c3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions redis/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -1151,6 +1151,10 @@ def disconnect(self, inuse_connections=True):
for connection in connections:
connection.disconnect()

def close(self) -> None:
"""Close the pool, disconnecting all connections"""
self.disconnect()

def set_retry(self, retry: "Retry") -> None:
self.connection_kwargs.update({"retry": retry})
for conn in self._available_connections:
Expand Down
11 changes: 11 additions & 0 deletions tests/test_connection_pool.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import re
import time
from contextlib import closing
from threading import Thread
from unittest import mock

Expand Down Expand Up @@ -51,6 +52,16 @@ def test_connection_creation(self):
assert isinstance(connection, DummyConnection)
assert connection.kwargs == connection_kwargs

def test_closing(self):
connection_kwargs = {"foo": "bar", "biz": "baz"}
pool = redis.ConnectionPool(
connection_class=DummyConnection,
max_connections=None,
**connection_kwargs,
)
with closing(pool):
pass

def test_multiple_connections(self, master_host):
connection_kwargs = {"host": master_host[0], "port": master_host[1]}
pool = self.get_pool(connection_kwargs=connection_kwargs)
Expand Down

0 comments on commit dd3e8c3

Please sign in to comment.