Skip to content

Commit 284174a

Browse files
support client pass through lib_name on connection
1 parent 114fc0c commit 284174a

File tree

2 files changed

+47
-16
lines changed

2 files changed

+47
-16
lines changed

redisvl/index/index.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,25 +84,27 @@ def _process(doc: "Document") -> Dict[str, Any]:
8484
return [_process(doc) for doc in results.docs]
8585

8686

87-
def check_modules_present():
87+
def setup_redis():
8888
def decorator(func):
8989
@wraps(func)
9090
def wrapper(self, *args, **kwargs):
9191
result = func(self, *args, **kwargs)
92-
RedisConnectionFactory.validate_redis_modules(self._redis_client)
92+
RedisConnectionFactory.validate_redis(self._redis_client, self._lib_name)
9393
return result
9494

9595
return wrapper
9696

9797
return decorator
9898

9999

100-
def check_async_modules_present():
100+
def setup_async_redis():
101101
def decorator(func):
102102
@wraps(func)
103103
def wrapper(self, *args, **kwargs):
104104
result = func(self, *args, **kwargs)
105-
RedisConnectionFactory.validate_async_redis_modules(self._redis_client)
105+
RedisConnectionFactory.validate_async_redis(
106+
self._redis_client, self._lib_name
107+
)
106108
return result
107109

108110
return wrapper
@@ -175,6 +177,9 @@ def __init__(
175177

176178
self.schema = schema
177179

180+
# set custom lib name
181+
self._lib_name: Optional[str] = kwargs.pop("lib_name", None)
182+
178183
# set up redis connection
179184
self._redis_client: Optional[Union[redis.Redis, aredis.Redis]] = None
180185
if redis_client is not None:
@@ -350,11 +355,13 @@ def connect(self, redis_url: Optional[str] = None, **kwargs):
350355
index.connect(redis_url="redis://localhost:6379")
351356
352357
"""
353-
client = RedisConnectionFactory.connect(redis_url, use_async=False, **kwargs)
358+
client = RedisConnectionFactory.connect(
359+
redis_url=redis_url, use_async=False, **kwargs
360+
)
354361
return self.set_client(client)
355362

356-
@check_modules_present()
357-
def set_client(self, client: redis.Redis):
363+
@setup_redis()
364+
def set_client(self, client: redis.Redis, **kwargs):
358365
"""Manually set the Redis client to use with the search index.
359366
360367
This method configures the search index to use a specific Redis or
@@ -729,10 +736,12 @@ def connect(self, redis_url: Optional[str] = None, **kwargs):
729736
index.connect(redis_url="redis://localhost:6379")
730737
731738
"""
732-
client = RedisConnectionFactory.connect(redis_url, use_async=True, **kwargs)
739+
client = RedisConnectionFactory.connect(
740+
redis_url=redis_url, use_async=True, **kwargs
741+
)
733742
return self.set_client(client)
734743

735-
@check_async_modules_present()
744+
@setup_async_redis()
736745
def set_client(self, client: aredis.Redis):
737746
"""Manually set the Redis client to use with the search index.
738747

redisvl/redis/connection.py

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
from redisvl.redis.constants import REDIS_REQUIRED_MODULES
1515
from redisvl.redis.utils import convert_bytes
16+
from redisvl.version import __version__
1617

1718

1819
def get_address_from_env() -> str:
@@ -26,6 +27,20 @@ def get_address_from_env() -> str:
2627
return os.environ["REDIS_URL"]
2728

2829

30+
def make_lib_name(*args) -> str:
31+
"""Build the lib name to be reported through the Redis client setinfo
32+
command.
33+
34+
Returns:
35+
str: Redis client library name
36+
"""
37+
custom_libs = f"redisvl_v{__version__}"
38+
for arg in args:
39+
if arg:
40+
custom_libs += f";{arg}"
41+
return f"redis-py({custom_libs})"
42+
43+
2944
class RedisConnectionFactory:
3045
"""Builds connections to a Redis database, supporting both synchronous and
3146
asynchronous clients.
@@ -108,8 +123,10 @@ def get_async_redis_connection(url: Optional[str] = None, **kwargs) -> AsyncRedi
108123
return AsyncRedis.from_url(get_address_from_env(), **kwargs)
109124

110125
@staticmethod
111-
def validate_redis_modules(
112-
client: Redis, redis_required_modules: Optional[List[Dict[str, Any]]] = None
126+
def validate_redis(
127+
client: Redis,
128+
lib_name: Optional[str] = None,
129+
redis_required_modules: Optional[List[Dict[str, Any]]] = None,
113130
) -> None:
114131
"""Validates if the required Redis modules are installed.
115132
@@ -119,13 +136,18 @@ def validate_redis_modules(
119136
Raises:
120137
ValueError: If required Redis modules are not installed.
121138
"""
122-
RedisConnectionFactory._validate_redis_modules(
139+
# set client library name
140+
client.client_setinfo("LIB-NAME", make_lib_name(lib_name))
141+
142+
# validate available modules
143+
RedisConnectionFactory._validate_modules(
123144
convert_bytes(client.module_list()), redis_required_modules
124145
)
125146

126147
@staticmethod
127-
def validate_async_redis_modules(
148+
def validate_async_redis(
128149
client: AsyncRedis,
150+
lib_name: Optional[str] = None,
129151
redis_required_modules: Optional[List[Dict[str, Any]]] = None,
130152
) -> None:
131153
"""
@@ -150,12 +172,12 @@ def validate_async_redis_modules(
150172
**client.connection_pool.connection_kwargs,
151173
)
152174
)
153-
RedisConnectionFactory.validate_redis_modules(
154-
temp_client, redis_required_modules
175+
RedisConnectionFactory.validate_redis(
176+
temp_client, lib_name, redis_required_modules
155177
)
156178

157179
@staticmethod
158-
def _validate_redis_modules(
180+
def _validate_modules(
159181
installed_modules, redis_required_modules: Optional[List[Dict[str, Any]]] = None
160182
) -> None:
161183
"""

0 commit comments

Comments
 (0)