Skip to content

Commit

Permalink
Deprecate the "auto_close_connection_pool" argument to Redis() and Re…
Browse files Browse the repository at this point in the history
…dis.from_url()
  • Loading branch information
kristjanvalur committed Sep 17, 2023
1 parent fb64a65 commit 32963b9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
33 changes: 25 additions & 8 deletions redis/asyncio/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def from_url(
cls,
url: str,
single_connection_client: bool = False,
auto_close_connection_pool: bool = True,
auto_close_connection_pool: Optional[bool] = None,
**kwargs,
):
"""
Expand Down Expand Up @@ -164,12 +164,17 @@ class initializer. In the case of conflicting arguments, querystring
connection_pool=connection_pool,
single_connection_client=single_connection_client,
)
# We should probably deprecate the `auto_close_connection_pool`
# argument.
# If the caller doesn't want the pool auto-closed, a better
# pattern is to create the pool manually (maybe using from_url()),
# pass it in using the `connection_pool`, and hold on to it to close
# it later.
if auto_close_connection_pool is not None:
warnings.warn(
DeprecationWarning(
'"auto_close_connection_pool" is deprecated '
"since version 5.0.0. "
"Please create a ConnectionPool explicitly and "
"provide to the Redis() constructor instead."
)
)
else:
auto_close_connection_pool = True
client.auto_close_connection_pool = auto_close_connection_pool
return client

Expand Down Expand Up @@ -223,7 +228,7 @@ def __init__(
username: Optional[str] = None,
retry: Optional[Retry] = None,
# deprecated. create a pool and use connection_pool instead
auto_close_connection_pool: bool = True,
auto_close_connection_pool: Optional[bool] = None,
redis_connect_func=None,
credential_provider: Optional[CredentialProvider] = None,
protocol: Optional[int] = 2,
Expand All @@ -237,6 +242,18 @@ def __init__(
"""
kwargs: Dict[str, Any]

if auto_close_connection_pool is not None:
warnings.warn(
DeprecationWarning(
'"auto_close_connection_pool" is deprecated '
"since version 5.0.0. "
"Please create a ConnectionPool explicitly and "
"provide to the Redis() constructor instead."
)
)
else:
auto_close_connection_pool = True

if not connection_pool:
# Create internal connection pool, expected to be closed by Redis instance
if not retry_on_error:
Expand Down
16 changes: 13 additions & 3 deletions tests/test_asyncio/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,15 +305,24 @@ async def get_redis_connection():
await r1.close()


async def test_pool_from_url_deprecation(request):
url: str = request.config.getoption("--redis-url")
url_args = parse_url(url)

with pytest.deprecated_call():
return Redis.from_url(url, auto_close_connection_pool=False)


async def test_pool_auto_close_disable(request):
"""Verify that auto_close_connection_pool can be disabled"""
"""Verify that auto_close_connection_pool can be disabled (deprecated)"""

url: str = request.config.getoption("--redis-url")
url_args = parse_url(url)

async def get_redis_connection():
url_args["auto_close_connection_pool"] = False
return Redis(**url_args)
with pytest.deprecated_call():
return Redis(**url_args)

r1 = await get_redis_connection()
assert r1.auto_close_connection_pool is False
Expand Down Expand Up @@ -394,7 +403,8 @@ async def test_redis_pool_auto_close_arg(request, auto_close):
pool = ConnectionPool.from_url(url)

async def get_redis_connection():
client = Redis(connection_pool=pool, auto_close_connection_pool=auto_close)
with pytest.deprecated_call():
client = Redis(connection_pool=pool, auto_close_connection_pool=auto_close)
return client

called = 0
Expand Down

0 comments on commit 32963b9

Please sign in to comment.