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

Add support for CLIENT SETINFO #2857

Merged
merged 23 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion redis/_parsers/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ def parse_client_info(value):
"key1=value1 key2=value2 key3=value3"
"""
client_info = {}
infos = str_if_bytes(value).split(" ")
infos = str_if_bytes(value[:-1]).split(" ")
for info in infos:
chayim marked this conversation as resolved.
Show resolved Hide resolved
key, value = info.split("=")
client_info[key] = value
Expand Down Expand Up @@ -700,6 +700,7 @@ def string_keys_to_dict(key_string, callback):
"CLIENT KILL": parse_client_kill,
"CLIENT LIST": parse_client_list,
"CLIENT PAUSE": bool_ok,
"CLIENT SETINFO": bool_ok,
"CLIENT SETNAME": bool_ok,
"CLIENT UNBLOCK": bool,
"CLUSTER ADDSLOTS": bool_ok,
Expand Down
11 changes: 10 additions & 1 deletion redis/asyncio/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
TimeoutError,
)
from redis.typing import EncodableT
from redis.utils import HIREDIS_AVAILABLE, str_if_bytes
from redis.utils import HIREDIS_AVAILABLE, get_lib_version, str_if_bytes

from .._parsers import (
BaseParser,
Expand Down Expand Up @@ -347,6 +347,15 @@ async def on_connect(self) -> None:
if str_if_bytes(await self.read_response()) != "OK":
raise ConnectionError("Error setting client name")

try:
# set the library name and version
await self.send_command("CLIENT", "SETINFO", "LIB-NAME", "redis-py")
await self.read_response()
dvora-h marked this conversation as resolved.
Show resolved Hide resolved
await self.send_command("CLIENT", "SETINFO", "LIB-VER", get_lib_version())
await self.read_response()
except ResponseError:
pass

# if a database is specified, switch to it
if self.db:
chayim marked this conversation as resolved.
Show resolved Hide resolved
await self.send_command("SELECT", self.db)
Expand Down
1 change: 1 addition & 0 deletions redis/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ class AbstractRedisCluster:
"ACL WHOAMI",
"AUTH",
"CLIENT LIST",
"CLIENT SETINFO",
"CLIENT SETNAME",
"CLIENT GETNAME",
"CONFIG SET",
Expand Down
7 changes: 7 additions & 0 deletions redis/commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,13 @@ def client_setname(self, name: str, **kwargs) -> ResponseT:
"""
return self.execute_command("CLIENT SETNAME", name, **kwargs)

def client_setinfo(self, attr: str, value: str, **kwargs) -> ResponseT:
"""
Sets the current connection library name or version
For mor information see https://redis.io/commands/client-setinfo
"""
return self.execute_command("CLIENT SETINFO", attr, value, **kwargs)

def client_unblock(
self, client_id: int, error: bool = False, **kwargs
) -> ResponseT:
Expand Down
10 changes: 10 additions & 0 deletions redis/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
HIREDIS_AVAILABLE,
HIREDIS_PACK_AVAILABLE,
SSL_AVAILABLE,
get_lib_version,
str_if_bytes,
)

Expand Down Expand Up @@ -360,6 +361,15 @@ def on_connect(self):
if str_if_bytes(self.read_response()) != "OK":
raise ConnectionError("Error setting client name")

try:
# set the library name and version
self.send_command("CLIENT", "SETINFO", "LIB-NAME", "redis-py")
dvora-h marked this conversation as resolved.
Show resolved Hide resolved
self.read_response()
self.send_command("CLIENT", "SETINFO", "LIB-VER", get_lib_version())
self.read_response()
except ResponseError:
pass

# if a database is specified, switch to it
if self.db:
self.send_command("SELECT", self.db)
Expand Down
14 changes: 14 additions & 0 deletions redis/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import sys
from contextlib import contextmanager
from functools import wraps
from typing import Any, Dict, Mapping, Union
Expand Down Expand Up @@ -27,6 +28,11 @@
except ImportError:
CRYPTOGRAPHY_AVAILABLE = False

if sys.version_info >= (3, 8):
dvora-h marked this conversation as resolved.
Show resolved Hide resolved
from importlib import metadata
else:
import importlib_metadata as metadata


def from_url(url, **kwargs):
"""
Expand Down Expand Up @@ -131,3 +137,11 @@ def _set_info_logger():
handler = logging.StreamHandler()
handler.setLevel(logging.INFO)
logger.addHandler(handler)


def get_lib_version():
try:
libver = metadata.version("redis")
except metadata.PackageNotFoundError:
libver = "99.99.99"
return libver
8 changes: 8 additions & 0 deletions tests/test_asyncio/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,14 @@ async def test_client_setname(self, r: redis.Redis):
r, await r.client_getname(), "redis_py_test", b"redis_py_test"
)

@skip_if_server_version_lt("7.2.0")
async def test_client_setinfo(self, r: redis.Redis):
assert await r.client_setinfo("lib-name", "test")
assert await r.client_setinfo("lib-ver", "123")
dvora-h marked this conversation as resolved.
Show resolved Hide resolved
info = await r.client_info()
assert info["lib-name"] == "test"
assert info["lib-ver"] == "123"

@skip_if_server_version_lt("2.6.9")
@pytest.mark.onlynoncluster
async def test_client_kill(self, r: redis.Redis, r2):
Expand Down
8 changes: 8 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,14 @@ def test_client_setname(self, r):
assert r.client_setname("redis_py_test")
assert_resp_response(r, r.client_getname(), "redis_py_test", b"redis_py_test")

@skip_if_server_version_lt("7.2.0")
def test_client_setinfo(self, r: redis.Redis):
assert r.client_setinfo("lib-name", "test")
chayim marked this conversation as resolved.
Show resolved Hide resolved
assert r.client_setinfo("lib-ver", "123")
info = r.client_info()
assert info["lib-name"] == "test"
assert info["lib-ver"] == "123"

@pytest.mark.onlynoncluster
@skip_if_server_version_lt("2.6.9")
def test_client_kill(self, r, r2):
Expand Down