Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements CLIENT KILL laddr filter #1506

Merged
merged 2 commits into from
Jul 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion redis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1209,14 +1209,16 @@ def client_kill(self, address):
"Disconnects the client at ``address`` (ip:port)"
return self.execute_command('CLIENT KILL', address)

def client_kill_filter(self, _id=None, _type=None, addr=None, skipme=None):
def client_kill_filter(self, _id=None, _type=None, addr=None,
skipme=None, laddr=None):
"""
Disconnects client(s) using a variety of filter options
:param id: Kills a client by its unique ID field
:param type: Kills a client by type where type is one of 'normal',
'master', 'slave' or 'pubsub'
:param addr: Kills a client by its 'address:port'
:param skipme: If True, then the client calling the command
:param laddr: Kills a cient by its 'local (bind) address:port'
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
"""
Expand All @@ -1238,6 +1240,8 @@ def client_kill_filter(self, _id=None, _type=None, addr=None, skipme=None):
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 <filter> <value> ... ... <filter> "
"<value> must specify at least one filter")
Expand Down
20 changes: 20 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,26 @@ def test_client_list_after_client_setname(self, r):
# 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, r2):
r.client_setname('redis-py-c1')
r2.client_setname('redis-py-c2')
clients = [client for client in r.client_list()
if client.get('name') in ['redis-py-c1', 'redis-py-c2']]
assert len(clients) == 2

clients_by_name = dict([(client.get('name'), client)
for client in clients])

client_2_addr = clients_by_name['redis-py-c2'].get('laddr')
resp = r.client_kill_filter(laddr=client_2_addr)
assert resp == 1

clients = [client for client in 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')
def test_client_pause(self, r):
assert r.client_pause(1)
Expand Down