From d1bed29470725b460b4f4dd7ae3c9b6b165f18ac Mon Sep 17 00:00:00 2001 From: Andrew-Chen-Wang Date: Mon, 4 Oct 2021 17:44:26 -0400 Subject: [PATCH] Add andymccurdy/redis-py@b021f5a15e61ac4217b533443eba0a1c56c03ef3 Implements CLIENT KILL laddr filter (andymccurdy/redis-py#1506) Signed-off-by: Andrew-Chen-Wang --- aioredis/client.py | 4 ++++ tests/test_commands.py | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/aioredis/client.py b/aioredis/client.py index e7e74a23c..660438200 100644 --- a/aioredis/client.py +++ b/aioredis/client.py @@ -1359,6 +1359,7 @@ def client_kill_filter( _type: Optional[str] = None, addr: Optional[str] = None, skipme: Optional[bool] = None, + laddr: Optional[bool] = None, ) -> Awaitable: """ Disconnects client(s) using a variety of filter options @@ -1369,6 +1370,7 @@ def client_kill_filter( :param skipme: If True, then the client calling the command will not get killed even if it is identified by one of the filter options. If skipme is not provided, the server defaults to skipme=True + :param laddr: Kills a client by its 'local (bind) address:port' """ args = [] if _type is not None: @@ -1387,6 +1389,8 @@ def client_kill_filter( args.extend((b"ID", _id)) if addr is not None: args.extend((b"ADDR", addr)) + if laddr is not None: + args.extend((b"LADDR", laddr)) if not args: raise DataError( "CLIENT KILL ... ... " diff --git a/tests/test_commands.py b/tests/test_commands.py index d8cf185b9..ac63b71fd 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -474,6 +474,31 @@ async def test_client_list_after_client_setname(self, r: aioredis.Redis): # we don't know which client ours will be assert "redis_py_test" in [c["name"] for c in clients] + @skip_if_server_version_lt("6.2.0") + def test_client_kill_filter_by_laddr(self, r: aioredis.Redis, r2: aioredis.Redis): + await r.client_setname("redis-py-c1") + await r2.client_setname("redis-py-c2") + clients = [ + client + for client in await r.client_list() + if client.get("name") in ["redis-py-c1", "redis-py-c2"] + ] + assert len(clients) == 2 + + clients_by_name = {client.get("name"): client for client in clients} + + client_2_addr = clients_by_name["redis-py-c2"].get("laddr") + resp = await r.client_kill_filter(laddr=client_2_addr) + assert resp == 1 + + clients = [ + client + for client in await r.client_list() + if client.get("name") in ["redis-py-c1", "redis-py-c2"] + ] + assert len(clients) == 1 + assert clients[0].get("name") == "redis-py-c1" + @skip_if_server_version_lt("2.9.50") async def test_client_pause(self, r: aioredis.Redis): assert await r.client_pause(1)