From 4cb632a92581002f784e546e5393efa72853112c Mon Sep 17 00:00:00 2001 From: Itamar Haber Date: Fri, 5 Oct 2018 15:31:56 +0300 Subject: Adds support for CLIENT ID Signed-off-by: Itamar Haber --- redis/client.py | 5 +++++ tests/test_commands.py | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/redis/client.py b/redis/client.py index 79e94d0..53b7cc2 100755 --- a/redis/client.py +++ b/redis/client.py @@ -398,6 +398,7 @@ class StrictRedis(object): string_keys_to_dict('BGREWRITEAOF BGSAVE', lambda r: True), { 'CLIENT GETNAME': lambda r: r and nativestr(r), + 'CLIENT ID': int, 'CLIENT KILL': bool_ok, 'CLIENT LIST': parse_client_list, 'CLIENT SETNAME': bool_ok, @@ -707,6 +708,10 @@ class StrictRedis(object): "Returns the current connection name" return self.execute_command('CLIENT GETNAME') + def client_id(self): + "Returns the current connection id" + return self.execute_command('CLIENT ID') + def client_setname(self, name): "Sets the current connection name" return self.execute_command('CLIENT SETNAME', name) diff --git a/tests/test_commands.py b/tests/test_commands.py index b9b9b66..b8deee6 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -59,6 +59,10 @@ class TestRedisCommands(object): assert isinstance(clients[0], dict) assert 'addr' in clients[0] + @skip_if_server_version_lt('5.0.0') + def test_client_id(self, r): + assert r.client_id() > 0 + @skip_if_server_version_lt('2.6.9') def test_client_getname(self, r): assert r.client_getname() is None -- cgit v1.2.1 From 80beebbd22a9a12da5347acbd6184f2e722ab8fa Mon Sep 17 00:00:00 2001 From: Itamar Haber Date: Fri, 5 Oct 2018 15:46:05 +0300 Subject: Adds CLIENT UNBLOCK Signed-off-by: Itamar Haber --- redis/client.py | 5 +++++ tests/test_commands.py | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/redis/client.py b/redis/client.py index 53b7cc2..2ea28c3 100755 --- a/redis/client.py +++ b/redis/client.py @@ -402,6 +402,7 @@ class StrictRedis(object): 'CLIENT KILL': bool_ok, 'CLIENT LIST': parse_client_list, 'CLIENT SETNAME': bool_ok, + 'CLIENT UNBLOCK': int, 'CONFIG GET': parse_config_get, 'CONFIG RESETSTAT': bool_ok, 'CONFIG SET': bool_ok, @@ -716,6 +717,10 @@ class StrictRedis(object): "Sets the current connection name" return self.execute_command('CLIENT SETNAME', name) + def client_unblock(self, client_id): + "Unblocks a connection by its client id" + return self.execute_command('CLIENT UNBLOCK', client_id) + def config_get(self, pattern="*"): "Return a dictionary of configuration based on the ``pattern``" return self.execute_command('CONFIG GET', pattern) diff --git a/tests/test_commands.py b/tests/test_commands.py index b8deee6..01dbd79 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -63,6 +63,11 @@ class TestRedisCommands(object): def test_client_id(self, r): assert r.client_id() > 0 + @skip_if_server_version_lt('5.0.0') + def test_client_unblock(self, r): + myid = r.client_id() + assert r.client_unblock(myid) == 0 + @skip_if_server_version_lt('2.6.9') def test_client_getname(self, r): assert r.client_getname() is None -- cgit v1.2.1 From 7a8a85245d2e55c2c97290175bd6d6f98976ff5f Mon Sep 17 00:00:00 2001 From: Itamar Haber Date: Fri, 5 Oct 2018 20:20:55 +0300 Subject: Adds unblock reason support Signed-off-by: Itamar Haber --- redis/client.py | 17 +++++++++++++---- tests/test_commands.py | 6 +++++- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/redis/client.py b/redis/client.py index 2ea28c3..416d34b 100755 --- a/redis/client.py +++ b/redis/client.py @@ -402,7 +402,7 @@ class StrictRedis(object): 'CLIENT KILL': bool_ok, 'CLIENT LIST': parse_client_list, 'CLIENT SETNAME': bool_ok, - 'CLIENT UNBLOCK': int, + 'CLIENT UNBLOCK': lambda r: r and int(r) == 1 or False, 'CONFIG GET': parse_config_get, 'CONFIG RESETSTAT': bool_ok, 'CONFIG SET': bool_ok, @@ -717,9 +717,18 @@ class StrictRedis(object): "Sets the current connection name" return self.execute_command('CLIENT SETNAME', name) - def client_unblock(self, client_id): - "Unblocks a connection by its client id" - return self.execute_command('CLIENT UNBLOCK', client_id) + def client_unblock(self, client_id, reason=None): + """ + Unblocks a connection by its client id + + The ``reason`` argument, if set to ``'error'``, unblocks the client + with a special error message. When set to ``'timeout'`` or if not set, + the client is unblocked using the regular timeout mechanism. + """ + args = ['CLIENT UNBLOCK', int(client_id)] + if reason is not None and isinstance(reason, str): + args.append(reason) + return self.execute_command(*args) def config_get(self, pattern="*"): "Return a dictionary of configuration based on the ``pattern``" diff --git a/tests/test_commands.py b/tests/test_commands.py index 01dbd79..724928f 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -66,7 +66,11 @@ class TestRedisCommands(object): @skip_if_server_version_lt('5.0.0') def test_client_unblock(self, r): myid = r.client_id() - assert r.client_unblock(myid) == 0 + assert not r.client_unblock(myid) + assert not r.client_unblock(myid, reason='TIMEOUT') + assert not r.client_unblock(myid, reason='ERROR') + with pytest.raises(exceptions.ResponseError): + r.client_unblock(myid, reason='foobar') @skip_if_server_version_lt('2.6.9') def test_client_getname(self, r): -- cgit v1.2.1 From bf15bd6bb6de06d2da8727308bd9d91ff584b514 Mon Sep 17 00:00:00 2001 From: Roey Prat Date: Sun, 4 Nov 2018 10:26:27 +0200 Subject: Remove reason arg from client_unblock. Use boolean 'error' arg instead. --- redis/client.py | 15 +++++++-------- tests/test_commands.py | 6 ++---- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/redis/client.py b/redis/client.py index 416d34b..9a16c8e 100755 --- a/redis/client.py +++ b/redis/client.py @@ -717,17 +717,16 @@ class StrictRedis(object): "Sets the current connection name" return self.execute_command('CLIENT SETNAME', name) - def client_unblock(self, client_id, reason=None): + def client_unblock(self, client_id, error=False): """ - Unblocks a connection by its client id - - The ``reason`` argument, if set to ``'error'``, unblocks the client - with a special error message. When set to ``'timeout'`` or if not set, - the client is unblocked using the regular timeout mechanism. + Unblocks a connection by its client id. + If ``error`` is True, unblocks the client with a special error message. + If ``error`` is False (default), the client is unblocked using the + regular timeout mechanism. """ args = ['CLIENT UNBLOCK', int(client_id)] - if reason is not None and isinstance(reason, str): - args.append(reason) + if error: + args.append(Token.get_token('ERROR')) return self.execute_command(*args) def config_get(self, pattern="*"): diff --git a/tests/test_commands.py b/tests/test_commands.py index 724928f..9e77a93 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -67,10 +67,8 @@ class TestRedisCommands(object): def test_client_unblock(self, r): myid = r.client_id() assert not r.client_unblock(myid) - assert not r.client_unblock(myid, reason='TIMEOUT') - assert not r.client_unblock(myid, reason='ERROR') - with pytest.raises(exceptions.ResponseError): - r.client_unblock(myid, reason='foobar') + assert not r.client_unblock(myid, error=True) + assert not r.client_unblock(myid, error=False) @skip_if_server_version_lt('2.6.9') def test_client_getname(self, r): -- cgit v1.2.1