Skip to content

Commit d7827d8

Browse files
authored
Merge pull request #1036 from itamarhaber/v5-client-subcommands
Adds v5 new client subcommands
2 parents 320afd3 + c34e8e8 commit d7827d8

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

redis/client.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,9 +461,11 @@ class StrictRedis(object):
461461
string_keys_to_dict('BGREWRITEAOF BGSAVE', lambda r: True),
462462
{
463463
'CLIENT GETNAME': lambda r: r and nativestr(r),
464+
'CLIENT ID': int,
464465
'CLIENT KILL': bool_ok,
465466
'CLIENT LIST': parse_client_list,
466467
'CLIENT SETNAME': bool_ok,
468+
'CLIENT UNBLOCK': lambda r: r and int(r) == 1 or False,
467469
'CLUSTER ADDSLOTS': bool_ok,
468470
'CLUSTER COUNT-FAILURE-REPORTS': lambda x: int(x),
469471
'CLUSTER COUNTKEYSINSLOT': lambda x: int(x),
@@ -792,10 +794,26 @@ def client_getname(self):
792794
"Returns the current connection name"
793795
return self.execute_command('CLIENT GETNAME')
794796

797+
def client_id(self):
798+
"Returns the current connection id"
799+
return self.execute_command('CLIENT ID')
800+
795801
def client_setname(self, name):
796802
"Sets the current connection name"
797803
return self.execute_command('CLIENT SETNAME', name)
798804

805+
def client_unblock(self, client_id, error=False):
806+
"""
807+
Unblocks a connection by its client id.
808+
If ``error`` is True, unblocks the client with a special error message.
809+
If ``error`` is False (default), the client is unblocked using the
810+
regular timeout mechanism.
811+
"""
812+
args = ['CLIENT UNBLOCK', int(client_id)]
813+
if error:
814+
args.append(Token.get_token('ERROR'))
815+
return self.execute_command(*args)
816+
799817
def config_get(self, pattern="*"):
800818
"Return a dictionary of configuration based on the ``pattern``"
801819
return self.execute_command('CONFIG GET', pattern)

tests/test_commands.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,17 @@ def test_client_list(self, r):
6767
assert isinstance(clients[0], dict)
6868
assert 'addr' in clients[0]
6969

70+
@skip_if_server_version_lt('5.0.0')
71+
def test_client_id(self, r):
72+
assert r.client_id() > 0
73+
74+
@skip_if_server_version_lt('5.0.0')
75+
def test_client_unblock(self, r):
76+
myid = r.client_id()
77+
assert not r.client_unblock(myid)
78+
assert not r.client_unblock(myid, error=True)
79+
assert not r.client_unblock(myid, error=False)
80+
7081
@skip_if_server_version_lt('2.6.9')
7182
def test_client_getname(self, r):
7283
assert r.client_getname() is None

0 commit comments

Comments
 (0)