4949 TimeoutError ,
5050)
5151from redis .typing import EncodableT
52- from redis .utils import HIREDIS_AVAILABLE , str_if_bytes
52+ from redis .utils import HIREDIS_AVAILABLE , get_lib_version , str_if_bytes
5353
5454from .._parsers import (
5555 BaseParser ,
@@ -101,6 +101,8 @@ class AbstractConnection:
101101 "db" ,
102102 "username" ,
103103 "client_name" ,
104+ "lib_name" ,
105+ "lib_version" ,
104106 "credential_provider" ,
105107 "password" ,
106108 "socket_timeout" ,
@@ -140,6 +142,8 @@ def __init__(
140142 socket_read_size : int = 65536 ,
141143 health_check_interval : float = 0 ,
142144 client_name : Optional [str ] = None ,
145+ lib_name : Optional [str ] = "redis-py" ,
146+ lib_version : Optional [str ] = get_lib_version (),
143147 username : Optional [str ] = None ,
144148 retry : Optional [Retry ] = None ,
145149 redis_connect_func : Optional [ConnectCallbackT ] = None ,
@@ -157,6 +161,8 @@ def __init__(
157161 self .pid = os .getpid ()
158162 self .db = db
159163 self .client_name = client_name
164+ self .lib_name = lib_name
165+ self .lib_version = lib_version
160166 self .credential_provider = credential_provider
161167 self .password = password
162168 self .username = username
@@ -347,9 +353,23 @@ async def on_connect(self) -> None:
347353 if str_if_bytes (await self .read_response ()) != "OK" :
348354 raise ConnectionError ("Error setting client name" )
349355
350- # if a database is specified, switch to it
356+ # set the library name and version, pipeline for lower startup latency
357+ if self .lib_name :
358+ await self .send_command ("CLIENT" , "SETINFO" , "LIB-NAME" , self .lib_name )
359+ if self .lib_version :
360+ await self .send_command ("CLIENT" , "SETINFO" , "LIB-VER" , self .lib_version )
361+ # if a database is specified, switch to it. Also pipeline this
351362 if self .db :
352363 await self .send_command ("SELECT" , self .db )
364+
365+ # read responses from pipeline
366+ for _ in (sent for sent in (self .lib_name , self .lib_version ) if sent ):
367+ try :
368+ await self .read_response ()
369+ except ResponseError :
370+ pass
371+
372+ if self .db :
353373 if str_if_bytes (await self .read_response ()) != "OK" :
354374 raise ConnectionError ("Invalid Database" )
355375
0 commit comments