client.py 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001
  1. import copy
  2. import re
  3. import threading
  4. import time
  5. from itertools import chain
  6. from typing import (
  7. TYPE_CHECKING,
  8. Any,
  9. Callable,
  10. Dict,
  11. List,
  12. Mapping,
  13. Optional,
  14. Set,
  15. Type,
  16. Union,
  17. )
  18. from redis._parsers.encoders import Encoder
  19. from redis._parsers.helpers import (
  20. _RedisCallbacks,
  21. _RedisCallbacksRESP2,
  22. _RedisCallbacksRESP3,
  23. bool_ok,
  24. )
  25. from redis.backoff import ExponentialWithJitterBackoff
  26. from redis.cache import CacheConfig, CacheInterface
  27. from redis.commands import (
  28. CoreCommands,
  29. RedisModuleCommands,
  30. SentinelCommands,
  31. list_or_args,
  32. )
  33. from redis.commands.core import Script
  34. from redis.connection import (
  35. AbstractConnection,
  36. Connection,
  37. ConnectionPool,
  38. SSLConnection,
  39. UnixDomainSocketConnection,
  40. )
  41. from redis.credentials import CredentialProvider
  42. from redis.driver_info import DriverInfo, resolve_driver_info
  43. from redis.event import (
  44. AfterPooledConnectionsInstantiationEvent,
  45. AfterPubSubConnectionInstantiationEvent,
  46. AfterSingleConnectionInstantiationEvent,
  47. ClientType,
  48. EventDispatcher,
  49. )
  50. from redis.exceptions import (
  51. ConnectionError,
  52. ExecAbortError,
  53. PubSubError,
  54. RedisError,
  55. ResponseError,
  56. WatchError,
  57. )
  58. from redis.lock import Lock
  59. from redis.maint_notifications import (
  60. MaintNotificationsConfig,
  61. OSSMaintNotificationsHandler,
  62. )
  63. from redis.observability.attributes import PubSubDirection
  64. from redis.observability.recorder import (
  65. record_error_count,
  66. record_operation_duration,
  67. record_pubsub_message,
  68. )
  69. from redis.retry import Retry
  70. from redis.utils import (
  71. _set_info_logger,
  72. check_protocol_version,
  73. deprecated_args,
  74. safe_str,
  75. str_if_bytes,
  76. truncate_text,
  77. )
  78. if TYPE_CHECKING:
  79. import ssl
  80. import OpenSSL
  81. SYM_EMPTY = b""
  82. EMPTY_RESPONSE = "EMPTY_RESPONSE"
  83. # some responses (ie. dump) are binary, and just meant to never be decoded
  84. NEVER_DECODE = "NEVER_DECODE"
  85. class CaseInsensitiveDict(dict):
  86. "Case insensitive dict implementation. Assumes string keys only."
  87. def __init__(self, data: Dict[str, str]) -> None:
  88. for k, v in data.items():
  89. self[k.upper()] = v
  90. def __contains__(self, k):
  91. return super().__contains__(k.upper())
  92. def __delitem__(self, k):
  93. super().__delitem__(k.upper())
  94. def __getitem__(self, k):
  95. return super().__getitem__(k.upper())
  96. def get(self, k, default=None):
  97. return super().get(k.upper(), default)
  98. def __setitem__(self, k, v):
  99. super().__setitem__(k.upper(), v)
  100. def update(self, data):
  101. data = CaseInsensitiveDict(data)
  102. super().update(data)
  103. class AbstractRedis:
  104. pass
  105. class Redis(RedisModuleCommands, CoreCommands, SentinelCommands):
  106. """
  107. Implementation of the Redis protocol.
  108. This abstract class provides a Python interface to all Redis commands
  109. and an implementation of the Redis protocol.
  110. Pipelines derive from this, implementing how
  111. the commands are sent and received to the Redis server. Based on
  112. configuration, an instance will either use a ConnectionPool, or
  113. Connection object to talk to redis.
  114. It is not safe to pass PubSub or Pipeline objects between threads.
  115. """
  116. @classmethod
  117. def from_url(cls, url: str, **kwargs) -> "Redis":
  118. """
  119. Return a Redis client object configured from the given URL
  120. For example::
  121. redis://[[username]:[password]]@localhost:6379/0
  122. rediss://[[username]:[password]]@localhost:6379/0
  123. unix://[username@]/path/to/socket.sock?db=0[&password=password]
  124. Three URL schemes are supported:
  125. - `redis://` creates a TCP socket connection. See more at:
  126. <https://www.iana.org/assignments/uri-schemes/prov/redis>
  127. - `rediss://` creates a SSL wrapped TCP socket connection. See more at:
  128. <https://www.iana.org/assignments/uri-schemes/prov/rediss>
  129. - ``unix://``: creates a Unix Domain Socket connection.
  130. The username, password, hostname, path and all querystring values
  131. are passed through urllib.parse.unquote in order to replace any
  132. percent-encoded values with their corresponding characters.
  133. There are several ways to specify a database number. The first value
  134. found will be used:
  135. 1. A ``db`` querystring option, e.g. redis://localhost?db=0
  136. 2. If using the redis:// or rediss:// schemes, the path argument
  137. of the url, e.g. redis://localhost/0
  138. 3. A ``db`` keyword argument to this function.
  139. If none of these options are specified, the default db=0 is used.
  140. All querystring options are cast to their appropriate Python types.
  141. Boolean arguments can be specified with string values "True"/"False"
  142. or "Yes"/"No". Values that cannot be properly cast cause a
  143. ``ValueError`` to be raised. Once parsed, the querystring arguments
  144. and keyword arguments are passed to the ``ConnectionPool``'s
  145. class initializer. In the case of conflicting arguments, querystring
  146. arguments always win.
  147. """
  148. single_connection_client = kwargs.pop("single_connection_client", False)
  149. connection_pool = ConnectionPool.from_url(url, **kwargs)
  150. client = cls(
  151. connection_pool=connection_pool,
  152. single_connection_client=single_connection_client,
  153. )
  154. client.auto_close_connection_pool = True
  155. return client
  156. @classmethod
  157. def from_pool(
  158. cls: Type["Redis"],
  159. connection_pool: ConnectionPool,
  160. ) -> "Redis":
  161. """
  162. Return a Redis client from the given connection pool.
  163. The Redis client will take ownership of the connection pool and
  164. close it when the Redis client is closed.
  165. """
  166. client = cls(
  167. connection_pool=connection_pool,
  168. )
  169. client.auto_close_connection_pool = True
  170. return client
  171. @deprecated_args(
  172. args_to_warn=["retry_on_timeout"],
  173. reason="TimeoutError is included by default.",
  174. version="6.0.0",
  175. )
  176. @deprecated_args(
  177. args_to_warn=["lib_name", "lib_version"],
  178. reason="Use 'driver_info' parameter instead. "
  179. "lib_name and lib_version will be removed in a future version.",
  180. )
  181. def __init__(
  182. self,
  183. host: str = "localhost",
  184. port: int = 6379,
  185. db: int = 0,
  186. password: Optional[str] = None,
  187. socket_timeout: Optional[float] = None,
  188. socket_connect_timeout: Optional[float] = None,
  189. socket_keepalive: Optional[bool] = None,
  190. socket_keepalive_options: Optional[Mapping[int, Union[int, bytes]]] = None,
  191. connection_pool: Optional[ConnectionPool] = None,
  192. unix_socket_path: Optional[str] = None,
  193. encoding: str = "utf-8",
  194. encoding_errors: str = "strict",
  195. decode_responses: bool = False,
  196. retry_on_timeout: bool = False,
  197. retry: Retry = Retry(
  198. backoff=ExponentialWithJitterBackoff(base=1, cap=10), retries=3
  199. ),
  200. retry_on_error: Optional[List[Type[Exception]]] = None,
  201. ssl: bool = False,
  202. ssl_keyfile: Optional[str] = None,
  203. ssl_certfile: Optional[str] = None,
  204. ssl_cert_reqs: Union[str, "ssl.VerifyMode"] = "required",
  205. ssl_include_verify_flags: Optional[List["ssl.VerifyFlags"]] = None,
  206. ssl_exclude_verify_flags: Optional[List["ssl.VerifyFlags"]] = None,
  207. ssl_ca_certs: Optional[str] = None,
  208. ssl_ca_path: Optional[str] = None,
  209. ssl_ca_data: Optional[str] = None,
  210. ssl_check_hostname: bool = True,
  211. ssl_password: Optional[str] = None,
  212. ssl_validate_ocsp: bool = False,
  213. ssl_validate_ocsp_stapled: bool = False,
  214. ssl_ocsp_context: Optional["OpenSSL.SSL.Context"] = None,
  215. ssl_ocsp_expected_cert: Optional[str] = None,
  216. ssl_min_version: Optional["ssl.TLSVersion"] = None,
  217. ssl_ciphers: Optional[str] = None,
  218. max_connections: Optional[int] = None,
  219. single_connection_client: bool = False,
  220. health_check_interval: int = 0,
  221. client_name: Optional[str] = None,
  222. lib_name: Optional[str] = None,
  223. lib_version: Optional[str] = None,
  224. driver_info: Optional["DriverInfo"] = None,
  225. username: Optional[str] = None,
  226. redis_connect_func: Optional[Callable[[], None]] = None,
  227. credential_provider: Optional[CredentialProvider] = None,
  228. protocol: Optional[int] = 2,
  229. cache: Optional[CacheInterface] = None,
  230. cache_config: Optional[CacheConfig] = None,
  231. event_dispatcher: Optional[EventDispatcher] = None,
  232. maint_notifications_config: Optional[MaintNotificationsConfig] = None,
  233. oss_cluster_maint_notifications_handler: Optional[
  234. OSSMaintNotificationsHandler
  235. ] = None,
  236. ) -> None:
  237. """
  238. Initialize a new Redis client.
  239. To specify a retry policy for specific errors, you have two options:
  240. 1. Set the `retry_on_error` to a list of the error/s to retry on, and
  241. you can also set `retry` to a valid `Retry` object(in case the default
  242. one is not appropriate) - with this approach the retries will be triggered
  243. on the default errors specified in the Retry object enriched with the
  244. errors specified in `retry_on_error`.
  245. 2. Define a `Retry` object with configured 'supported_errors' and set
  246. it to the `retry` parameter - with this approach you completely redefine
  247. the errors on which retries will happen.
  248. `retry_on_timeout` is deprecated - please include the TimeoutError
  249. either in the Retry object or in the `retry_on_error` list.
  250. When 'connection_pool' is provided - the retry configuration of the
  251. provided pool will be used.
  252. Args:
  253. single_connection_client:
  254. if `True`, connection pool is not used. In that case `Redis`
  255. instance use is not thread safe.
  256. decode_responses:
  257. if `True`, the response will be decoded to utf-8.
  258. Argument is ignored when connection_pool is provided.
  259. driver_info:
  260. Optional DriverInfo object to identify upstream libraries.
  261. If provided, lib_name and lib_version are ignored.
  262. If not provided, a DriverInfo will be created from lib_name and lib_version.
  263. Argument is ignored when connection_pool is provided.
  264. lib_name:
  265. **Deprecated.** Use driver_info instead. Library name for CLIENT SETINFO.
  266. lib_version:
  267. **Deprecated.** Use driver_info instead. Library version for CLIENT SETINFO.
  268. maint_notifications_config:
  269. configures the pool to support maintenance notifications - see
  270. `redis.maint_notifications.MaintNotificationsConfig` for details.
  271. Only supported with RESP3
  272. If not provided and protocol is RESP3, the maintenance notifications
  273. will be enabled by default (logic is included in the connection pool
  274. initialization).
  275. Argument is ignored when connection_pool is provided.
  276. oss_cluster_maint_notifications_handler:
  277. handler for OSS cluster notifications - see
  278. `redis.maint_notifications.OSSMaintNotificationsHandler` for details.
  279. Only supported with RESP3
  280. Argument is ignored when connection_pool is provided.
  281. """
  282. if event_dispatcher is None:
  283. self._event_dispatcher = EventDispatcher()
  284. else:
  285. self._event_dispatcher = event_dispatcher
  286. if not connection_pool:
  287. if not retry_on_error:
  288. retry_on_error = []
  289. # Handle driver_info: if provided, use it; otherwise create from lib_name/lib_version
  290. computed_driver_info = resolve_driver_info(
  291. driver_info, lib_name, lib_version
  292. )
  293. kwargs = {
  294. "db": db,
  295. "username": username,
  296. "password": password,
  297. "socket_timeout": socket_timeout,
  298. "encoding": encoding,
  299. "encoding_errors": encoding_errors,
  300. "decode_responses": decode_responses,
  301. "retry_on_error": retry_on_error,
  302. "retry": copy.deepcopy(retry),
  303. "max_connections": max_connections,
  304. "health_check_interval": health_check_interval,
  305. "client_name": client_name,
  306. "driver_info": computed_driver_info,
  307. "redis_connect_func": redis_connect_func,
  308. "credential_provider": credential_provider,
  309. "protocol": protocol,
  310. }
  311. # based on input, setup appropriate connection args
  312. if unix_socket_path is not None:
  313. kwargs.update(
  314. {
  315. "path": unix_socket_path,
  316. "connection_class": UnixDomainSocketConnection,
  317. }
  318. )
  319. else:
  320. # TCP specific options
  321. kwargs.update(
  322. {
  323. "host": host,
  324. "port": port,
  325. "socket_connect_timeout": socket_connect_timeout,
  326. "socket_keepalive": socket_keepalive,
  327. "socket_keepalive_options": socket_keepalive_options,
  328. }
  329. )
  330. if ssl:
  331. kwargs.update(
  332. {
  333. "connection_class": SSLConnection,
  334. "ssl_keyfile": ssl_keyfile,
  335. "ssl_certfile": ssl_certfile,
  336. "ssl_cert_reqs": ssl_cert_reqs,
  337. "ssl_include_verify_flags": ssl_include_verify_flags,
  338. "ssl_exclude_verify_flags": ssl_exclude_verify_flags,
  339. "ssl_ca_certs": ssl_ca_certs,
  340. "ssl_ca_data": ssl_ca_data,
  341. "ssl_check_hostname": ssl_check_hostname,
  342. "ssl_password": ssl_password,
  343. "ssl_ca_path": ssl_ca_path,
  344. "ssl_validate_ocsp_stapled": ssl_validate_ocsp_stapled,
  345. "ssl_validate_ocsp": ssl_validate_ocsp,
  346. "ssl_ocsp_context": ssl_ocsp_context,
  347. "ssl_ocsp_expected_cert": ssl_ocsp_expected_cert,
  348. "ssl_min_version": ssl_min_version,
  349. "ssl_ciphers": ssl_ciphers,
  350. }
  351. )
  352. if (cache_config or cache) and check_protocol_version(protocol, 3):
  353. kwargs.update(
  354. {
  355. "cache": cache,
  356. "cache_config": cache_config,
  357. }
  358. )
  359. maint_notifications_enabled = (
  360. maint_notifications_config and maint_notifications_config.enabled
  361. )
  362. if maint_notifications_enabled and protocol not in [
  363. 3,
  364. "3",
  365. ]:
  366. raise RedisError(
  367. "Maintenance notifications handlers on connection are only supported with RESP version 3"
  368. )
  369. if maint_notifications_config:
  370. kwargs.update(
  371. {
  372. "maint_notifications_config": maint_notifications_config,
  373. }
  374. )
  375. if oss_cluster_maint_notifications_handler:
  376. kwargs.update(
  377. {
  378. "oss_cluster_maint_notifications_handler": oss_cluster_maint_notifications_handler,
  379. }
  380. )
  381. connection_pool = ConnectionPool(**kwargs)
  382. self._event_dispatcher.dispatch(
  383. AfterPooledConnectionsInstantiationEvent(
  384. [connection_pool], ClientType.SYNC, credential_provider
  385. )
  386. )
  387. self.auto_close_connection_pool = True
  388. else:
  389. self.auto_close_connection_pool = False
  390. self._event_dispatcher.dispatch(
  391. AfterPooledConnectionsInstantiationEvent(
  392. [connection_pool], ClientType.SYNC, credential_provider
  393. )
  394. )
  395. self.connection_pool = connection_pool
  396. if (cache_config or cache) and self.connection_pool.get_protocol() not in [
  397. 3,
  398. "3",
  399. ]:
  400. raise RedisError("Client caching is only supported with RESP version 3")
  401. self.single_connection_lock = threading.RLock()
  402. self.connection = None
  403. self._single_connection_client = single_connection_client
  404. if self._single_connection_client:
  405. self.connection = self.connection_pool.get_connection()
  406. self._event_dispatcher.dispatch(
  407. AfterSingleConnectionInstantiationEvent(
  408. self.connection, ClientType.SYNC, self.single_connection_lock
  409. )
  410. )
  411. self.response_callbacks = CaseInsensitiveDict(_RedisCallbacks)
  412. if self.connection_pool.connection_kwargs.get("protocol") in ["3", 3]:
  413. self.response_callbacks.update(_RedisCallbacksRESP3)
  414. else:
  415. self.response_callbacks.update(_RedisCallbacksRESP2)
  416. def __repr__(self) -> str:
  417. return (
  418. f"<{type(self).__module__}.{type(self).__name__}"
  419. f"({repr(self.connection_pool)})>"
  420. )
  421. def get_encoder(self) -> "Encoder":
  422. """Get the connection pool's encoder"""
  423. return self.connection_pool.get_encoder()
  424. def get_connection_kwargs(self) -> Dict:
  425. """Get the connection's key-word arguments"""
  426. return self.connection_pool.connection_kwargs
  427. def get_retry(self) -> Optional[Retry]:
  428. return self.get_connection_kwargs().get("retry")
  429. def set_retry(self, retry: Retry) -> None:
  430. self.get_connection_kwargs().update({"retry": retry})
  431. self.connection_pool.set_retry(retry)
  432. def set_response_callback(self, command: str, callback: Callable) -> None:
  433. """Set a custom Response Callback"""
  434. self.response_callbacks[command] = callback
  435. def load_external_module(self, funcname, func) -> None:
  436. """
  437. This function can be used to add externally defined redis modules,
  438. and their namespaces to the redis client.
  439. funcname - A string containing the name of the function to create
  440. func - The function, being added to this class.
  441. ex: Assume that one has a custom redis module named foomod that
  442. creates command named 'foo.dothing' and 'foo.anotherthing' in redis.
  443. To load function functions into this namespace:
  444. from redis import Redis
  445. from foomodule import F
  446. r = Redis()
  447. r.load_external_module("foo", F)
  448. r.foo().dothing('your', 'arguments')
  449. For a concrete example see the reimport of the redisjson module in
  450. tests/test_connection.py::test_loading_external_modules
  451. """
  452. setattr(self, funcname, func)
  453. def pipeline(self, transaction=True, shard_hint=None) -> "Pipeline":
  454. """
  455. Return a new pipeline object that can queue multiple commands for
  456. later execution. ``transaction`` indicates whether all commands
  457. should be executed atomically. Apart from making a group of operations
  458. atomic, pipelines are useful for reducing the back-and-forth overhead
  459. between the client and server.
  460. """
  461. return Pipeline(
  462. self.connection_pool, self.response_callbacks, transaction, shard_hint
  463. )
  464. def transaction(
  465. self, func: Callable[["Pipeline"], None], *watches, **kwargs
  466. ) -> Union[List[Any], Any, None]:
  467. """
  468. Convenience method for executing the callable `func` as a transaction
  469. while watching all keys specified in `watches`. The 'func' callable
  470. should expect a single argument which is a Pipeline object.
  471. """
  472. shard_hint = kwargs.pop("shard_hint", None)
  473. value_from_callable = kwargs.pop("value_from_callable", False)
  474. watch_delay = kwargs.pop("watch_delay", None)
  475. with self.pipeline(True, shard_hint) as pipe:
  476. while True:
  477. try:
  478. if watches:
  479. pipe.watch(*watches)
  480. func_value = func(pipe)
  481. exec_value = pipe.execute()
  482. return func_value if value_from_callable else exec_value
  483. except WatchError:
  484. if watch_delay is not None and watch_delay > 0:
  485. time.sleep(watch_delay)
  486. continue
  487. def lock(
  488. self,
  489. name: str,
  490. timeout: Optional[float] = None,
  491. sleep: float = 0.1,
  492. blocking: bool = True,
  493. blocking_timeout: Optional[float] = None,
  494. lock_class: Union[None, Any] = None,
  495. thread_local: bool = True,
  496. raise_on_release_error: bool = True,
  497. ):
  498. """
  499. Return a new Lock object using key ``name`` that mimics
  500. the behavior of threading.Lock.
  501. If specified, ``timeout`` indicates a maximum life for the lock.
  502. By default, it will remain locked until release() is called.
  503. ``sleep`` indicates the amount of time to sleep per loop iteration
  504. when the lock is in blocking mode and another client is currently
  505. holding the lock.
  506. ``blocking`` indicates whether calling ``acquire`` should block until
  507. the lock has been acquired or to fail immediately, causing ``acquire``
  508. to return False and the lock not being acquired. Defaults to True.
  509. Note this value can be overridden by passing a ``blocking``
  510. argument to ``acquire``.
  511. ``blocking_timeout`` indicates the maximum amount of time in seconds to
  512. spend trying to acquire the lock. A value of ``None`` indicates
  513. continue trying forever. ``blocking_timeout`` can be specified as a
  514. float or integer, both representing the number of seconds to wait.
  515. ``lock_class`` forces the specified lock implementation. Note that as
  516. of redis-py 3.0, the only lock class we implement is ``Lock`` (which is
  517. a Lua-based lock). So, it's unlikely you'll need this parameter, unless
  518. you have created your own custom lock class.
  519. ``thread_local`` indicates whether the lock token is placed in
  520. thread-local storage. By default, the token is placed in thread local
  521. storage so that a thread only sees its token, not a token set by
  522. another thread. Consider the following timeline:
  523. time: 0, thread-1 acquires `my-lock`, with a timeout of 5 seconds.
  524. thread-1 sets the token to "abc"
  525. time: 1, thread-2 blocks trying to acquire `my-lock` using the
  526. Lock instance.
  527. time: 5, thread-1 has not yet completed. redis expires the lock
  528. key.
  529. time: 5, thread-2 acquired `my-lock` now that it's available.
  530. thread-2 sets the token to "xyz"
  531. time: 6, thread-1 finishes its work and calls release(). if the
  532. token is *not* stored in thread local storage, then
  533. thread-1 would see the token value as "xyz" and would be
  534. able to successfully release the thread-2's lock.
  535. ``raise_on_release_error`` indicates whether to raise an exception when
  536. the lock is no longer owned when exiting the context manager. By default,
  537. this is True, meaning an exception will be raised. If False, the warning
  538. will be logged and the exception will be suppressed.
  539. In some use cases it's necessary to disable thread local storage. For
  540. example, if you have code where one thread acquires a lock and passes
  541. that lock instance to a worker thread to release later. If thread
  542. local storage isn't disabled in this case, the worker thread won't see
  543. the token set by the thread that acquired the lock. Our assumption
  544. is that these cases aren't common and as such default to using
  545. thread local storage."""
  546. if lock_class is None:
  547. lock_class = Lock
  548. return lock_class(
  549. self,
  550. name,
  551. timeout=timeout,
  552. sleep=sleep,
  553. blocking=blocking,
  554. blocking_timeout=blocking_timeout,
  555. thread_local=thread_local,
  556. raise_on_release_error=raise_on_release_error,
  557. )
  558. def pubsub(self, **kwargs):
  559. """
  560. Return a Publish/Subscribe object. With this object, you can
  561. subscribe to channels and listen for messages that get published to
  562. them.
  563. """
  564. return PubSub(
  565. self.connection_pool, event_dispatcher=self._event_dispatcher, **kwargs
  566. )
  567. def monitor(self):
  568. return Monitor(self.connection_pool)
  569. def client(self):
  570. return self.__class__(
  571. connection_pool=self.connection_pool,
  572. single_connection_client=True,
  573. )
  574. def __enter__(self):
  575. return self
  576. def __exit__(self, exc_type, exc_value, traceback):
  577. self.close()
  578. def __del__(self):
  579. try:
  580. self.close()
  581. except Exception:
  582. pass
  583. def close(self) -> None:
  584. # In case a connection property does not yet exist
  585. # (due to a crash earlier in the Redis() constructor), return
  586. # immediately as there is nothing to clean-up.
  587. if not hasattr(self, "connection"):
  588. return
  589. conn = self.connection
  590. if conn:
  591. self.connection = None
  592. self.connection_pool.release(conn)
  593. if self.auto_close_connection_pool:
  594. self.connection_pool.disconnect()
  595. def _send_command_parse_response(self, conn, command_name, *args, **options):
  596. """
  597. Send a command and parse the response
  598. """
  599. conn.send_command(*args, **options)
  600. return self.parse_response(conn, command_name, **options)
  601. def _close_connection(
  602. self,
  603. conn,
  604. error: Optional[Exception] = None,
  605. failure_count: Optional[int] = None,
  606. start_time: Optional[float] = None,
  607. command_name: Optional[str] = None,
  608. ) -> None:
  609. """
  610. Close the connection before retrying.
  611. The supported exceptions are already checked in the
  612. retry object so we don't need to do it here.
  613. After we disconnect the connection, it will try to reconnect and
  614. do a health check as part of the send_command logic(on connection level).
  615. """
  616. if error and failure_count <= conn.retry.get_retries():
  617. record_operation_duration(
  618. command_name=command_name,
  619. duration_seconds=time.monotonic() - start_time,
  620. server_address=getattr(conn, "host", None),
  621. server_port=getattr(conn, "port", None),
  622. db_namespace=str(conn.db),
  623. error=error,
  624. retry_attempts=failure_count,
  625. )
  626. conn.disconnect()
  627. # COMMAND EXECUTION AND PROTOCOL PARSING
  628. def execute_command(self, *args, **options):
  629. return self._execute_command(*args, **options)
  630. def _execute_command(self, *args, **options):
  631. """Execute a command and return a parsed response"""
  632. pool = self.connection_pool
  633. command_name = args[0]
  634. conn = self.connection or pool.get_connection()
  635. # Start timing for observability
  636. start_time = time.monotonic()
  637. # Track actual retry attempts for error reporting
  638. actual_retry_attempts = [0]
  639. def failure_callback(error, failure_count):
  640. actual_retry_attempts[0] = failure_count
  641. self._close_connection(conn, error, failure_count, start_time, command_name)
  642. if self._single_connection_client:
  643. self.single_connection_lock.acquire()
  644. try:
  645. result = conn.retry.call_with_retry(
  646. lambda: self._send_command_parse_response(
  647. conn, command_name, *args, **options
  648. ),
  649. failure_callback,
  650. with_failure_count=True,
  651. )
  652. record_operation_duration(
  653. command_name=command_name,
  654. duration_seconds=time.monotonic() - start_time,
  655. server_address=getattr(conn, "host", None),
  656. server_port=getattr(conn, "port", None),
  657. db_namespace=str(conn.db),
  658. )
  659. return result
  660. except Exception as e:
  661. record_error_count(
  662. server_address=getattr(conn, "host", None),
  663. server_port=getattr(conn, "port", None),
  664. network_peer_address=getattr(conn, "host", None),
  665. network_peer_port=getattr(conn, "port", None),
  666. error_type=e,
  667. retry_attempts=actual_retry_attempts[0],
  668. is_internal=False,
  669. )
  670. raise
  671. finally:
  672. if conn and conn.should_reconnect():
  673. self._close_connection(conn)
  674. conn.connect()
  675. if self._single_connection_client:
  676. self.single_connection_lock.release()
  677. if not self.connection:
  678. pool.release(conn)
  679. def parse_response(self, connection, command_name, **options):
  680. """Parses a response from the Redis server"""
  681. try:
  682. if NEVER_DECODE in options:
  683. response = connection.read_response(disable_decoding=True)
  684. options.pop(NEVER_DECODE)
  685. else:
  686. response = connection.read_response()
  687. except ResponseError:
  688. if EMPTY_RESPONSE in options:
  689. return options[EMPTY_RESPONSE]
  690. raise
  691. if EMPTY_RESPONSE in options:
  692. options.pop(EMPTY_RESPONSE)
  693. # Remove keys entry, it needs only for cache.
  694. options.pop("keys", None)
  695. if command_name in self.response_callbacks:
  696. return self.response_callbacks[command_name](response, **options)
  697. return response
  698. def get_cache(self) -> Optional[CacheInterface]:
  699. return self.connection_pool.cache
  700. StrictRedis = Redis
  701. class Monitor:
  702. """
  703. Monitor is useful for handling the MONITOR command to the redis server.
  704. next_command() method returns one command from monitor
  705. listen() method yields commands from monitor.
  706. """
  707. monitor_re = re.compile(r"\[(\d+) (.*?)\] (.*)")
  708. command_re = re.compile(r'"(.*?)(?<!\\)"')
  709. def __init__(self, connection_pool):
  710. self.connection_pool = connection_pool
  711. self.connection = self.connection_pool.get_connection()
  712. def __enter__(self):
  713. self._start_monitor()
  714. return self
  715. def __exit__(self, *args):
  716. self.connection.disconnect()
  717. self.connection_pool.release(self.connection)
  718. def next_command(self):
  719. """Parse the response from a monitor command"""
  720. response = self.connection.read_response()
  721. if response is None:
  722. return None
  723. if isinstance(response, bytes):
  724. response = self.connection.encoder.decode(response, force=True)
  725. command_time, command_data = response.split(" ", 1)
  726. m = self.monitor_re.match(command_data)
  727. db_id, client_info, command = m.groups()
  728. command = " ".join(self.command_re.findall(command))
  729. # Redis escapes double quotes because each piece of the command
  730. # string is surrounded by double quotes. We don't have that
  731. # requirement so remove the escaping and leave the quote.
  732. command = command.replace('\\"', '"')
  733. if client_info == "lua":
  734. client_address = "lua"
  735. client_port = ""
  736. client_type = "lua"
  737. elif client_info.startswith("unix"):
  738. client_address = "unix"
  739. client_port = client_info[5:]
  740. client_type = "unix"
  741. else:
  742. if client_info == "":
  743. client_address = ""
  744. client_port = ""
  745. client_type = "unknown"
  746. else:
  747. # use rsplit as ipv6 addresses contain colons
  748. client_address, client_port = client_info.rsplit(":", 1)
  749. client_type = "tcp"
  750. return {
  751. "time": float(command_time),
  752. "db": int(db_id),
  753. "client_address": client_address,
  754. "client_port": client_port,
  755. "client_type": client_type,
  756. "command": command,
  757. }
  758. def listen(self):
  759. """Listen for commands coming to the server."""
  760. while True:
  761. yield self.next_command()
  762. def _start_monitor(self):
  763. self.connection.send_command("MONITOR")
  764. # check that monitor returns 'OK', but don't return it to user
  765. response = self.connection.read_response()
  766. if not bool_ok(response):
  767. raise RedisError(f"MONITOR failed: {response}")
  768. class PubSub:
  769. """
  770. PubSub provides publish, subscribe and listen support to Redis channels.
  771. After subscribing to one or more channels, the listen() method will block
  772. until a message arrives on one of the subscribed channels. That message
  773. will be returned and it's safe to start listening again.
  774. """
  775. PUBLISH_MESSAGE_TYPES = ("message", "pmessage", "smessage")
  776. UNSUBSCRIBE_MESSAGE_TYPES = ("unsubscribe", "punsubscribe", "sunsubscribe")
  777. HEALTH_CHECK_MESSAGE = "redis-py-health-check"
  778. def __init__(
  779. self,
  780. connection_pool,
  781. shard_hint=None,
  782. ignore_subscribe_messages: bool = False,
  783. encoder: Optional["Encoder"] = None,
  784. push_handler_func: Union[None, Callable[[str], None]] = None,
  785. event_dispatcher: Optional["EventDispatcher"] = None,
  786. ):
  787. self.connection_pool = connection_pool
  788. self.shard_hint = shard_hint
  789. self.ignore_subscribe_messages = ignore_subscribe_messages
  790. self.connection = None
  791. self.subscribed_event = threading.Event()
  792. # we need to know the encoding options for this connection in order
  793. # to lookup channel and pattern names for callback handlers.
  794. self.encoder = encoder
  795. self.push_handler_func = push_handler_func
  796. if event_dispatcher is None:
  797. self._event_dispatcher = EventDispatcher()
  798. else:
  799. self._event_dispatcher = event_dispatcher
  800. self._lock = threading.RLock()
  801. if self.encoder is None:
  802. self.encoder = self.connection_pool.get_encoder()
  803. self.health_check_response_b = self.encoder.encode(self.HEALTH_CHECK_MESSAGE)
  804. if self.encoder.decode_responses:
  805. self.health_check_response = ["pong", self.HEALTH_CHECK_MESSAGE]
  806. else:
  807. self.health_check_response = [b"pong", self.health_check_response_b]
  808. if self.push_handler_func is None:
  809. _set_info_logger()
  810. self.reset()
  811. def __enter__(self) -> "PubSub":
  812. return self
  813. def __exit__(self, exc_type, exc_value, traceback) -> None:
  814. self.reset()
  815. def __del__(self) -> None:
  816. try:
  817. # if this object went out of scope prior to shutting down
  818. # subscriptions, close the connection manually before
  819. # returning it to the connection pool
  820. self.reset()
  821. except Exception:
  822. pass
  823. def reset(self) -> None:
  824. if self.connection:
  825. self.connection.disconnect()
  826. self.connection.deregister_connect_callback(self.on_connect)
  827. self.connection_pool.release(self.connection)
  828. self.connection = None
  829. self.health_check_response_counter = 0
  830. self.channels = {}
  831. self.pending_unsubscribe_channels = set()
  832. self.shard_channels = {}
  833. self.pending_unsubscribe_shard_channels = set()
  834. self.patterns = {}
  835. self.pending_unsubscribe_patterns = set()
  836. self.subscribed_event.clear()
  837. def close(self) -> None:
  838. self.reset()
  839. def on_connect(self, connection) -> None:
  840. "Re-subscribe to any channels and patterns previously subscribed to"
  841. # NOTE: for python3, we can't pass bytestrings as keyword arguments
  842. # so we need to decode channel/pattern names back to unicode strings
  843. # before passing them to [p]subscribe.
  844. #
  845. # However, channels subscribed without a callback (positional args) may
  846. # have binary names that are not valid in the current encoding (e.g.
  847. # arbitrary bytes that are not valid UTF-8). These channels are stored
  848. # with a ``None`` handler. We re-subscribe them as positional args so
  849. # that no decoding is required.
  850. self.pending_unsubscribe_channels.clear()
  851. self.pending_unsubscribe_patterns.clear()
  852. self.pending_unsubscribe_shard_channels.clear()
  853. if self.channels:
  854. channels_with_handlers = {}
  855. channels_without_handlers = []
  856. for k, v in self.channels.items():
  857. if v is not None:
  858. channels_with_handlers[self.encoder.decode(k, force=True)] = v
  859. else:
  860. channels_without_handlers.append(k)
  861. if channels_with_handlers or channels_without_handlers:
  862. self.subscribe(*channels_without_handlers, **channels_with_handlers)
  863. if self.patterns:
  864. patterns_with_handlers = {}
  865. patterns_without_handlers = []
  866. for k, v in self.patterns.items():
  867. if v is not None:
  868. patterns_with_handlers[self.encoder.decode(k, force=True)] = v
  869. else:
  870. patterns_without_handlers.append(k)
  871. if patterns_with_handlers or patterns_without_handlers:
  872. self.psubscribe(*patterns_without_handlers, **patterns_with_handlers)
  873. if self.shard_channels:
  874. shard_with_handlers = {}
  875. shard_without_handlers = []
  876. for k, v in self.shard_channels.items():
  877. if v is not None:
  878. shard_with_handlers[self.encoder.decode(k, force=True)] = v
  879. else:
  880. shard_without_handlers.append(k)
  881. if shard_with_handlers or shard_without_handlers:
  882. self.ssubscribe(*shard_without_handlers, **shard_with_handlers)
  883. @property
  884. def subscribed(self) -> bool:
  885. """Indicates if there are subscriptions to any channels or patterns"""
  886. return self.subscribed_event.is_set()
  887. def execute_command(self, *args):
  888. """Execute a publish/subscribe command"""
  889. # NOTE: don't parse the response in this function -- it could pull a
  890. # legitimate message off the stack if the connection is already
  891. # subscribed to one or more channels
  892. if self.connection is None:
  893. self.connection = self.connection_pool.get_connection()
  894. # register a callback that re-subscribes to any channels we
  895. # were listening to when we were disconnected
  896. self.connection.register_connect_callback(self.on_connect)
  897. if self.push_handler_func is not None:
  898. self.connection._parser.set_pubsub_push_handler(self.push_handler_func)
  899. self._event_dispatcher.dispatch(
  900. AfterPubSubConnectionInstantiationEvent(
  901. self.connection, self.connection_pool, ClientType.SYNC, self._lock
  902. )
  903. )
  904. connection = self.connection
  905. kwargs = {"check_health": not self.subscribed}
  906. if not self.subscribed:
  907. self.clean_health_check_responses()
  908. with self._lock:
  909. self._execute(connection, connection.send_command, *args, **kwargs)
  910. def clean_health_check_responses(self) -> None:
  911. """
  912. If any health check responses are present, clean them
  913. """
  914. ttl = 10
  915. conn = self.connection
  916. while conn and self.health_check_response_counter > 0 and ttl > 0:
  917. if self._execute(conn, conn.can_read, timeout=conn.socket_timeout):
  918. response = self._execute(conn, conn.read_response)
  919. if self.is_health_check_response(response):
  920. self.health_check_response_counter -= 1
  921. else:
  922. raise PubSubError(
  923. "A non health check response was cleaned by "
  924. "execute_command: {}".format(response)
  925. )
  926. ttl -= 1
  927. def _reconnect(
  928. self,
  929. conn,
  930. error: Optional[Exception] = None,
  931. failure_count: Optional[int] = None,
  932. start_time: Optional[float] = None,
  933. command_name: Optional[str] = None,
  934. ) -> None:
  935. """
  936. The supported exceptions are already checked in the
  937. retry object so we don't need to do it here.
  938. In this error handler we are trying to reconnect to the server.
  939. """
  940. if error and failure_count <= conn.retry.get_retries():
  941. if command_name:
  942. record_operation_duration(
  943. command_name=command_name,
  944. duration_seconds=time.monotonic() - start_time,
  945. server_address=getattr(conn, "host", None),
  946. server_port=getattr(conn, "port", None),
  947. db_namespace=str(conn.db),
  948. error=error,
  949. retry_attempts=failure_count,
  950. )
  951. conn.disconnect()
  952. conn.connect()
  953. def _execute(self, conn, command, *args, **kwargs):
  954. """
  955. Connect manually upon disconnection. If the Redis server is down,
  956. this will fail and raise a ConnectionError as desired.
  957. After reconnection, the ``on_connect`` callback should have been
  958. called by the # connection to resubscribe us to any channels and
  959. patterns we were previously listening to
  960. """
  961. if conn.should_reconnect():
  962. self._reconnect(conn)
  963. if not len(args) == 0:
  964. command_name = args[0]
  965. else:
  966. command_name = None
  967. # Start timing for observability
  968. start_time = time.monotonic()
  969. # Track actual retry attempts for error reporting
  970. actual_retry_attempts = [0]
  971. def failure_callback(error, failure_count):
  972. actual_retry_attempts[0] = failure_count
  973. self._reconnect(conn, error, failure_count, start_time, command_name)
  974. try:
  975. response = conn.retry.call_with_retry(
  976. lambda: command(*args, **kwargs),
  977. failure_callback,
  978. with_failure_count=True,
  979. )
  980. if command_name:
  981. record_operation_duration(
  982. command_name=command_name,
  983. duration_seconds=time.monotonic() - start_time,
  984. server_address=getattr(conn, "host", None),
  985. server_port=getattr(conn, "port", None),
  986. db_namespace=str(conn.db),
  987. )
  988. return response
  989. except Exception as e:
  990. record_error_count(
  991. server_address=getattr(conn, "host", None),
  992. server_port=getattr(conn, "port", None),
  993. network_peer_address=getattr(conn, "host", None),
  994. network_peer_port=getattr(conn, "port", None),
  995. error_type=e,
  996. retry_attempts=actual_retry_attempts[0],
  997. is_internal=False,
  998. )
  999. raise
  1000. def parse_response(self, block=True, timeout=0):
  1001. """Parse the response from a publish/subscribe command"""
  1002. conn = self.connection
  1003. if conn is None:
  1004. raise RuntimeError(
  1005. "pubsub connection not set: "
  1006. "did you forget to call subscribe() or psubscribe()?"
  1007. )
  1008. self.check_health()
  1009. def try_read():
  1010. if not block:
  1011. if not conn.can_read(timeout=timeout):
  1012. return None
  1013. else:
  1014. conn.connect()
  1015. return conn.read_response(disconnect_on_error=False, push_request=True)
  1016. response = self._execute(conn, try_read)
  1017. if self.is_health_check_response(response):
  1018. # ignore the health check message as user might not expect it
  1019. self.health_check_response_counter -= 1
  1020. return None
  1021. return response
  1022. def is_health_check_response(self, response) -> bool:
  1023. """
  1024. Check if the response is a health check response.
  1025. If there are no subscriptions redis responds to PING command with a
  1026. bulk response, instead of a multi-bulk with "pong" and the response.
  1027. """
  1028. if self.encoder.decode_responses:
  1029. return (
  1030. response
  1031. in [
  1032. self.health_check_response, # If there is a subscription
  1033. self.HEALTH_CHECK_MESSAGE, # If there are no subscriptions and decode_responses=True
  1034. ]
  1035. )
  1036. else:
  1037. return (
  1038. response
  1039. in [
  1040. self.health_check_response, # If there is a subscription
  1041. self.health_check_response_b, # If there isn't a subscription and decode_responses=False
  1042. ]
  1043. )
  1044. def check_health(self) -> None:
  1045. conn = self.connection
  1046. if conn is None:
  1047. raise RuntimeError(
  1048. "pubsub connection not set: "
  1049. "did you forget to call subscribe() or psubscribe()?"
  1050. )
  1051. if conn.health_check_interval and time.monotonic() > conn.next_health_check:
  1052. conn.send_command("PING", self.HEALTH_CHECK_MESSAGE, check_health=False)
  1053. self.health_check_response_counter += 1
  1054. def _normalize_keys(self, data) -> Dict:
  1055. """
  1056. normalize channel/pattern names to be either bytes or strings
  1057. based on whether responses are automatically decoded. this saves us
  1058. from coercing the value for each message coming in.
  1059. """
  1060. encode = self.encoder.encode
  1061. decode = self.encoder.decode
  1062. return {decode(encode(k)): v for k, v in data.items()}
  1063. def psubscribe(self, *args, **kwargs):
  1064. """
  1065. Subscribe to channel patterns. Patterns supplied as keyword arguments
  1066. expect a pattern name as the key and a callable as the value. A
  1067. pattern's callable will be invoked automatically when a message is
  1068. received on that pattern rather than producing a message via
  1069. ``listen()``.
  1070. """
  1071. if args:
  1072. args = list_or_args(args[0], args[1:])
  1073. new_patterns = dict.fromkeys(args)
  1074. new_patterns.update(kwargs)
  1075. ret_val = self.execute_command("PSUBSCRIBE", *new_patterns.keys())
  1076. # update the patterns dict AFTER we send the command. we don't want to
  1077. # subscribe twice to these patterns, once for the command and again
  1078. # for the reconnection.
  1079. new_patterns = self._normalize_keys(new_patterns)
  1080. self.patterns.update(new_patterns)
  1081. if not self.subscribed:
  1082. # Set the subscribed_event flag to True
  1083. self.subscribed_event.set()
  1084. # Clear the health check counter
  1085. self.health_check_response_counter = 0
  1086. self.pending_unsubscribe_patterns.difference_update(new_patterns)
  1087. return ret_val
  1088. def punsubscribe(self, *args):
  1089. """
  1090. Unsubscribe from the supplied patterns. If empty, unsubscribe from
  1091. all patterns.
  1092. """
  1093. if args:
  1094. args = list_or_args(args[0], args[1:])
  1095. patterns = self._normalize_keys(dict.fromkeys(args))
  1096. else:
  1097. patterns = self.patterns
  1098. self.pending_unsubscribe_patterns.update(patterns)
  1099. return self.execute_command("PUNSUBSCRIBE", *args)
  1100. def subscribe(self, *args, **kwargs):
  1101. """
  1102. Subscribe to channels. Channels supplied as keyword arguments expect
  1103. a channel name as the key and a callable as the value. A channel's
  1104. callable will be invoked automatically when a message is received on
  1105. that channel rather than producing a message via ``listen()`` or
  1106. ``get_message()``.
  1107. """
  1108. if args:
  1109. args = list_or_args(args[0], args[1:])
  1110. new_channels = dict.fromkeys(args)
  1111. new_channels.update(kwargs)
  1112. ret_val = self.execute_command("SUBSCRIBE", *new_channels.keys())
  1113. # update the channels dict AFTER we send the command. we don't want to
  1114. # subscribe twice to these channels, once for the command and again
  1115. # for the reconnection.
  1116. new_channels = self._normalize_keys(new_channels)
  1117. self.channels.update(new_channels)
  1118. if not self.subscribed:
  1119. # Set the subscribed_event flag to True
  1120. self.subscribed_event.set()
  1121. # Clear the health check counter
  1122. self.health_check_response_counter = 0
  1123. self.pending_unsubscribe_channels.difference_update(new_channels)
  1124. return ret_val
  1125. def unsubscribe(self, *args):
  1126. """
  1127. Unsubscribe from the supplied channels. If empty, unsubscribe from
  1128. all channels
  1129. """
  1130. if args:
  1131. args = list_or_args(args[0], args[1:])
  1132. channels = self._normalize_keys(dict.fromkeys(args))
  1133. else:
  1134. channels = self.channels
  1135. self.pending_unsubscribe_channels.update(channels)
  1136. return self.execute_command("UNSUBSCRIBE", *args)
  1137. def ssubscribe(self, *args, target_node=None, **kwargs):
  1138. """
  1139. Subscribes the client to the specified shard channels.
  1140. Channels supplied as keyword arguments expect a channel name as the key
  1141. and a callable as the value. A channel's callable will be invoked automatically
  1142. when a message is received on that channel rather than producing a message via
  1143. ``listen()`` or ``get_sharded_message()``.
  1144. """
  1145. if args:
  1146. args = list_or_args(args[0], args[1:])
  1147. new_s_channels = dict.fromkeys(args)
  1148. new_s_channels.update(kwargs)
  1149. ret_val = self.execute_command("SSUBSCRIBE", *new_s_channels.keys())
  1150. # update the s_channels dict AFTER we send the command. we don't want to
  1151. # subscribe twice to these channels, once for the command and again
  1152. # for the reconnection.
  1153. new_s_channels = self._normalize_keys(new_s_channels)
  1154. self.shard_channels.update(new_s_channels)
  1155. if not self.subscribed:
  1156. # Set the subscribed_event flag to True
  1157. self.subscribed_event.set()
  1158. # Clear the health check counter
  1159. self.health_check_response_counter = 0
  1160. self.pending_unsubscribe_shard_channels.difference_update(new_s_channels)
  1161. return ret_val
  1162. def sunsubscribe(self, *args, target_node=None):
  1163. """
  1164. Unsubscribe from the supplied shard_channels. If empty, unsubscribe from
  1165. all shard_channels
  1166. """
  1167. if args:
  1168. args = list_or_args(args[0], args[1:])
  1169. s_channels = self._normalize_keys(dict.fromkeys(args))
  1170. else:
  1171. s_channels = self.shard_channels
  1172. self.pending_unsubscribe_shard_channels.update(s_channels)
  1173. return self.execute_command("SUNSUBSCRIBE", *args)
  1174. def listen(self):
  1175. "Listen for messages on channels this client has been subscribed to"
  1176. while self.subscribed:
  1177. response = self.handle_message(self.parse_response(block=True))
  1178. if response is not None:
  1179. yield response
  1180. def get_message(
  1181. self, ignore_subscribe_messages: bool = False, timeout: float = 0.0
  1182. ):
  1183. """
  1184. Get the next message if one is available, otherwise None.
  1185. If timeout is specified, the system will wait for `timeout` seconds
  1186. before returning. Timeout should be specified as a floating point
  1187. number, or None, to wait indefinitely.
  1188. """
  1189. if not self.subscribed:
  1190. # Wait for subscription
  1191. start_time = time.monotonic()
  1192. if self.subscribed_event.wait(timeout) is True:
  1193. # The connection was subscribed during the timeout time frame.
  1194. # The timeout should be adjusted based on the time spent
  1195. # waiting for the subscription
  1196. time_spent = time.monotonic() - start_time
  1197. timeout = max(0.0, timeout - time_spent)
  1198. else:
  1199. # The connection isn't subscribed to any channels or patterns,
  1200. # so no messages are available
  1201. return None
  1202. response = self.parse_response(block=(timeout is None), timeout=timeout)
  1203. if response:
  1204. return self.handle_message(response, ignore_subscribe_messages)
  1205. return None
  1206. get_sharded_message = get_message
  1207. def ping(self, message: Union[str, None] = None) -> bool:
  1208. """
  1209. Ping the Redis server to test connectivity.
  1210. Sends a PING command to the Redis server and returns True if the server
  1211. responds with "PONG".
  1212. """
  1213. args = ["PING", message] if message is not None else ["PING"]
  1214. return self.execute_command(*args)
  1215. def handle_message(self, response, ignore_subscribe_messages=False):
  1216. """
  1217. Parses a pub/sub message. If the channel or pattern was subscribed to
  1218. with a message handler, the handler is invoked instead of a parsed
  1219. message being returned.
  1220. """
  1221. if response is None:
  1222. return None
  1223. if isinstance(response, bytes):
  1224. response = [b"pong", response] if response != b"PONG" else [b"pong", b""]
  1225. message_type = str_if_bytes(response[0])
  1226. if message_type == "pmessage":
  1227. message = {
  1228. "type": message_type,
  1229. "pattern": response[1],
  1230. "channel": response[2],
  1231. "data": response[3],
  1232. }
  1233. elif message_type == "pong":
  1234. message = {
  1235. "type": message_type,
  1236. "pattern": None,
  1237. "channel": None,
  1238. "data": response[1],
  1239. }
  1240. else:
  1241. message = {
  1242. "type": message_type,
  1243. "pattern": None,
  1244. "channel": response[1],
  1245. "data": response[2],
  1246. }
  1247. if message_type in ["message", "pmessage"]:
  1248. channel = str_if_bytes(message["channel"])
  1249. record_pubsub_message(
  1250. direction=PubSubDirection.RECEIVE,
  1251. channel=channel,
  1252. )
  1253. elif message_type == "smessage":
  1254. channel = str_if_bytes(message["channel"])
  1255. record_pubsub_message(
  1256. direction=PubSubDirection.RECEIVE,
  1257. channel=channel,
  1258. sharded=True,
  1259. )
  1260. # if this is an unsubscribe message, remove it from memory
  1261. if message_type in self.UNSUBSCRIBE_MESSAGE_TYPES:
  1262. if message_type == "punsubscribe":
  1263. pattern = response[1]
  1264. if pattern in self.pending_unsubscribe_patterns:
  1265. self.pending_unsubscribe_patterns.remove(pattern)
  1266. self.patterns.pop(pattern, None)
  1267. elif message_type == "sunsubscribe":
  1268. s_channel = response[1]
  1269. if s_channel in self.pending_unsubscribe_shard_channels:
  1270. self.pending_unsubscribe_shard_channels.remove(s_channel)
  1271. self.shard_channels.pop(s_channel, None)
  1272. else:
  1273. channel = response[1]
  1274. if channel in self.pending_unsubscribe_channels:
  1275. self.pending_unsubscribe_channels.remove(channel)
  1276. self.channels.pop(channel, None)
  1277. if not self.channels and not self.patterns and not self.shard_channels:
  1278. # There are no subscriptions anymore, set subscribed_event flag
  1279. # to false
  1280. self.subscribed_event.clear()
  1281. if message_type in self.PUBLISH_MESSAGE_TYPES:
  1282. # if there's a message handler, invoke it
  1283. if message_type == "pmessage":
  1284. handler = self.patterns.get(message["pattern"], None)
  1285. elif message_type == "smessage":
  1286. handler = self.shard_channels.get(message["channel"], None)
  1287. else:
  1288. handler = self.channels.get(message["channel"], None)
  1289. if handler:
  1290. handler(message)
  1291. return None
  1292. elif message_type != "pong":
  1293. # this is a subscribe/unsubscribe message. ignore if we don't
  1294. # want them
  1295. if ignore_subscribe_messages or self.ignore_subscribe_messages:
  1296. return None
  1297. return message
  1298. def run_in_thread(
  1299. self,
  1300. sleep_time: float = 0.0,
  1301. daemon: bool = False,
  1302. exception_handler: Optional[Callable] = None,
  1303. pubsub=None,
  1304. sharded_pubsub: bool = False,
  1305. ) -> "PubSubWorkerThread":
  1306. for channel, handler in self.channels.items():
  1307. if handler is None:
  1308. raise PubSubError(f"Channel: '{channel}' has no handler registered")
  1309. for pattern, handler in self.patterns.items():
  1310. if handler is None:
  1311. raise PubSubError(f"Pattern: '{pattern}' has no handler registered")
  1312. for s_channel, handler in self.shard_channels.items():
  1313. if handler is None:
  1314. raise PubSubError(
  1315. f"Shard Channel: '{s_channel}' has no handler registered"
  1316. )
  1317. pubsub = self if pubsub is None else pubsub
  1318. thread = PubSubWorkerThread(
  1319. pubsub,
  1320. sleep_time,
  1321. daemon=daemon,
  1322. exception_handler=exception_handler,
  1323. sharded_pubsub=sharded_pubsub,
  1324. )
  1325. thread.start()
  1326. return thread
  1327. class PubSubWorkerThread(threading.Thread):
  1328. def __init__(
  1329. self,
  1330. pubsub,
  1331. sleep_time: float,
  1332. daemon: bool = False,
  1333. exception_handler: Union[
  1334. Callable[[Exception, "PubSub", "PubSubWorkerThread"], None], None
  1335. ] = None,
  1336. sharded_pubsub: bool = False,
  1337. ):
  1338. super().__init__()
  1339. self.daemon = daemon
  1340. self.pubsub = pubsub
  1341. self.sleep_time = sleep_time
  1342. self.exception_handler = exception_handler
  1343. self.sharded_pubsub = sharded_pubsub
  1344. self._running = threading.Event()
  1345. def run(self) -> None:
  1346. if self._running.is_set():
  1347. return
  1348. self._running.set()
  1349. pubsub = self.pubsub
  1350. sleep_time = self.sleep_time
  1351. while self._running.is_set():
  1352. try:
  1353. if not self.sharded_pubsub:
  1354. pubsub.get_message(
  1355. ignore_subscribe_messages=True, timeout=sleep_time
  1356. )
  1357. else:
  1358. pubsub.get_sharded_message(
  1359. ignore_subscribe_messages=True, timeout=sleep_time
  1360. )
  1361. except BaseException as e:
  1362. if self.exception_handler is None:
  1363. raise
  1364. self.exception_handler(e, pubsub, self)
  1365. pubsub.close()
  1366. def stop(self) -> None:
  1367. # trip the flag so the run loop exits. the run loop will
  1368. # close the pubsub connection, which disconnects the socket
  1369. # and returns the connection to the pool.
  1370. self._running.clear()
  1371. class Pipeline(Redis):
  1372. """
  1373. Pipelines provide a way to transmit multiple commands to the Redis server
  1374. in one transmission. This is convenient for batch processing, such as
  1375. saving all the values in a list to Redis.
  1376. All commands executed within a pipeline(when running in transactional mode,
  1377. which is the default behavior) are wrapped with MULTI and EXEC
  1378. calls. This guarantees all commands executed in the pipeline will be
  1379. executed atomically.
  1380. Any command raising an exception does *not* halt the execution of
  1381. subsequent commands in the pipeline. Instead, the exception is caught
  1382. and its instance is placed into the response list returned by execute().
  1383. Code iterating over the response list should be able to deal with an
  1384. instance of an exception as a potential value. In general, these will be
  1385. ResponseError exceptions, such as those raised when issuing a command
  1386. on a key of a different datatype.
  1387. """
  1388. UNWATCH_COMMANDS = {"DISCARD", "EXEC", "UNWATCH"}
  1389. def __init__(
  1390. self,
  1391. connection_pool: ConnectionPool,
  1392. response_callbacks,
  1393. transaction,
  1394. shard_hint,
  1395. ):
  1396. self.connection_pool = connection_pool
  1397. self.connection: Optional[Connection] = None
  1398. self.response_callbacks = response_callbacks
  1399. self.transaction = transaction
  1400. self.shard_hint = shard_hint
  1401. self.watching = False
  1402. self.command_stack = []
  1403. self.scripts: Set[Script] = set()
  1404. self.explicit_transaction = False
  1405. def __enter__(self) -> "Pipeline":
  1406. return self
  1407. def __exit__(self, exc_type, exc_value, traceback):
  1408. self.reset()
  1409. def __del__(self):
  1410. try:
  1411. self.reset()
  1412. except Exception:
  1413. pass
  1414. def __len__(self) -> int:
  1415. return len(self.command_stack)
  1416. def __bool__(self) -> bool:
  1417. """Pipeline instances should always evaluate to True"""
  1418. return True
  1419. def reset(self) -> None:
  1420. self.command_stack = []
  1421. self.scripts = set()
  1422. # make sure to reset the connection state in the event that we were
  1423. # watching something
  1424. if self.watching and self.connection:
  1425. try:
  1426. # call this manually since our unwatch or
  1427. # immediate_execute_command methods can call reset()
  1428. self.connection.send_command("UNWATCH")
  1429. self.connection.read_response()
  1430. except ConnectionError:
  1431. # disconnect will also remove any previous WATCHes
  1432. self.connection.disconnect()
  1433. # clean up the other instance attributes
  1434. self.watching = False
  1435. self.explicit_transaction = False
  1436. # we can safely return the connection to the pool here since we're
  1437. # sure we're no longer WATCHing anything
  1438. if self.connection:
  1439. self.connection_pool.release(self.connection)
  1440. self.connection = None
  1441. def close(self) -> None:
  1442. """Close the pipeline"""
  1443. self.reset()
  1444. def multi(self) -> None:
  1445. """
  1446. Start a transactional block of the pipeline after WATCH commands
  1447. are issued. End the transactional block with `execute`.
  1448. """
  1449. if self.explicit_transaction:
  1450. raise RedisError("Cannot issue nested calls to MULTI")
  1451. if self.command_stack:
  1452. raise RedisError(
  1453. "Commands without an initial WATCH have already been issued"
  1454. )
  1455. self.explicit_transaction = True
  1456. def execute_command(self, *args, **kwargs):
  1457. if (self.watching or args[0] == "WATCH") and not self.explicit_transaction:
  1458. return self.immediate_execute_command(*args, **kwargs)
  1459. return self.pipeline_execute_command(*args, **kwargs)
  1460. def _disconnect_reset_raise_on_watching(
  1461. self,
  1462. conn: AbstractConnection,
  1463. error: Exception,
  1464. failure_count: Optional[int] = None,
  1465. start_time: Optional[float] = None,
  1466. command_name: Optional[str] = None,
  1467. ) -> None:
  1468. """
  1469. Close the connection reset watching state and
  1470. raise an exception if we were watching.
  1471. The supported exceptions are already checked in the
  1472. retry object so we don't need to do it here.
  1473. After we disconnect the connection, it will try to reconnect and
  1474. do a health check as part of the send_command logic(on connection level).
  1475. """
  1476. if error and failure_count <= conn.retry.get_retries():
  1477. record_operation_duration(
  1478. command_name=command_name,
  1479. duration_seconds=time.monotonic() - start_time,
  1480. server_address=getattr(conn, "host", None),
  1481. server_port=getattr(conn, "port", None),
  1482. db_namespace=str(conn.db),
  1483. error=error,
  1484. retry_attempts=failure_count,
  1485. )
  1486. conn.disconnect()
  1487. # if we were already watching a variable, the watch is no longer
  1488. # valid since this connection has died. raise a WatchError, which
  1489. # indicates the user should retry this transaction.
  1490. if self.watching:
  1491. self.reset()
  1492. raise WatchError(
  1493. f"A {type(error).__name__} occurred while watching one or more keys"
  1494. )
  1495. def immediate_execute_command(self, *args, **options):
  1496. """
  1497. Execute a command immediately, but don't auto-retry on the supported
  1498. errors for retry if we're already WATCHing a variable.
  1499. Used when issuing WATCH or subsequent commands retrieving their values but before
  1500. MULTI is called.
  1501. """
  1502. command_name = args[0]
  1503. conn = self.connection
  1504. # if this is the first call, we need a connection
  1505. if not conn:
  1506. conn = self.connection_pool.get_connection()
  1507. self.connection = conn
  1508. # Start timing for observability
  1509. start_time = time.monotonic()
  1510. # Track actual retry attempts for error reporting
  1511. actual_retry_attempts = [0]
  1512. def failure_callback(error, failure_count):
  1513. actual_retry_attempts[0] = failure_count
  1514. self._disconnect_reset_raise_on_watching(
  1515. conn, error, failure_count, start_time, command_name
  1516. )
  1517. try:
  1518. response = conn.retry.call_with_retry(
  1519. lambda: self._send_command_parse_response(
  1520. conn, command_name, *args, **options
  1521. ),
  1522. failure_callback,
  1523. with_failure_count=True,
  1524. )
  1525. record_operation_duration(
  1526. command_name=command_name,
  1527. duration_seconds=time.monotonic() - start_time,
  1528. server_address=getattr(conn, "host", None),
  1529. server_port=getattr(conn, "port", None),
  1530. db_namespace=str(conn.db),
  1531. )
  1532. return response
  1533. except Exception as e:
  1534. record_error_count(
  1535. server_address=getattr(conn, "host", None),
  1536. server_port=getattr(conn, "port", None),
  1537. network_peer_address=getattr(conn, "host", None),
  1538. network_peer_port=getattr(conn, "port", None),
  1539. error_type=e,
  1540. retry_attempts=actual_retry_attempts[0],
  1541. is_internal=False,
  1542. )
  1543. raise
  1544. def pipeline_execute_command(self, *args, **options) -> "Pipeline":
  1545. """
  1546. Stage a command to be executed when execute() is next called
  1547. Returns the current Pipeline object back so commands can be
  1548. chained together, such as:
  1549. pipe = pipe.set('foo', 'bar').incr('baz').decr('bang')
  1550. At some other point, you can then run: pipe.execute(),
  1551. which will execute all commands queued in the pipe.
  1552. """
  1553. self.command_stack.append((args, options))
  1554. return self
  1555. def _execute_transaction(
  1556. self, connection: Connection, commands, raise_on_error
  1557. ) -> List:
  1558. cmds = chain([(("MULTI",), {})], commands, [(("EXEC",), {})])
  1559. all_cmds = connection.pack_commands(
  1560. [args for args, options in cmds if EMPTY_RESPONSE not in options]
  1561. )
  1562. connection.send_packed_command(all_cmds)
  1563. errors = []
  1564. # parse off the response for MULTI
  1565. # NOTE: we need to handle ResponseErrors here and continue
  1566. # so that we read all the additional command messages from
  1567. # the socket
  1568. try:
  1569. self.parse_response(connection, "_")
  1570. except ResponseError as e:
  1571. errors.append((0, e))
  1572. # and all the other commands
  1573. for i, command in enumerate(commands):
  1574. if EMPTY_RESPONSE in command[1]:
  1575. errors.append((i, command[1][EMPTY_RESPONSE]))
  1576. else:
  1577. try:
  1578. self.parse_response(connection, "_")
  1579. except ResponseError as e:
  1580. self.annotate_exception(e, i + 1, command[0])
  1581. errors.append((i, e))
  1582. # parse the EXEC.
  1583. try:
  1584. response = self.parse_response(connection, "_")
  1585. except ExecAbortError:
  1586. if errors:
  1587. raise errors[0][1]
  1588. raise
  1589. # EXEC clears any watched keys
  1590. self.watching = False
  1591. if response is None:
  1592. raise WatchError("Watched variable changed.")
  1593. # put any parse errors into the response
  1594. for i, e in errors:
  1595. response.insert(i, e)
  1596. if len(response) != len(commands):
  1597. self.connection.disconnect()
  1598. raise ResponseError(
  1599. "Wrong number of response items from pipeline execution"
  1600. )
  1601. # find any errors in the response and raise if necessary
  1602. if raise_on_error:
  1603. self.raise_first_error(commands, response)
  1604. # We have to run response callbacks manually
  1605. data = []
  1606. for r, cmd in zip(response, commands):
  1607. if not isinstance(r, Exception):
  1608. args, options = cmd
  1609. # Remove keys entry, it needs only for cache.
  1610. options.pop("keys", None)
  1611. command_name = args[0]
  1612. if command_name in self.response_callbacks:
  1613. r = self.response_callbacks[command_name](r, **options)
  1614. data.append(r)
  1615. return data
  1616. def _execute_pipeline(self, connection, commands, raise_on_error):
  1617. # build up all commands into a single request to increase network perf
  1618. all_cmds = connection.pack_commands([args for args, _ in commands])
  1619. connection.send_packed_command(all_cmds)
  1620. responses = []
  1621. for args, options in commands:
  1622. try:
  1623. responses.append(self.parse_response(connection, args[0], **options))
  1624. except ResponseError as e:
  1625. responses.append(e)
  1626. if raise_on_error:
  1627. self.raise_first_error(commands, responses)
  1628. return responses
  1629. def raise_first_error(self, commands, response):
  1630. for i, r in enumerate(response):
  1631. if isinstance(r, ResponseError):
  1632. self.annotate_exception(r, i + 1, commands[i][0])
  1633. raise r
  1634. def annotate_exception(self, exception, number, command):
  1635. cmd = " ".join(map(safe_str, command))
  1636. msg = (
  1637. f"Command # {number} ({truncate_text(cmd)}) of pipeline "
  1638. f"caused error: {exception.args[0]}"
  1639. )
  1640. exception.args = (msg,) + exception.args[1:]
  1641. def parse_response(self, connection, command_name, **options):
  1642. result = Redis.parse_response(self, connection, command_name, **options)
  1643. if command_name in self.UNWATCH_COMMANDS:
  1644. self.watching = False
  1645. elif command_name == "WATCH":
  1646. self.watching = True
  1647. return result
  1648. def load_scripts(self):
  1649. # make sure all scripts that are about to be run on this pipeline exist
  1650. scripts = list(self.scripts)
  1651. immediate = self.immediate_execute_command
  1652. shas = [s.sha for s in scripts]
  1653. # we can't use the normal script_* methods because they would just
  1654. # get buffered in the pipeline.
  1655. exists = immediate("SCRIPT EXISTS", *shas)
  1656. if not all(exists):
  1657. for s, exist in zip(scripts, exists):
  1658. if not exist:
  1659. s.sha = immediate("SCRIPT LOAD", s.script)
  1660. def _disconnect_raise_on_watching(
  1661. self,
  1662. conn: AbstractConnection,
  1663. error: Exception,
  1664. failure_count: Optional[int] = None,
  1665. start_time: Optional[float] = None,
  1666. command_name: Optional[str] = None,
  1667. ) -> None:
  1668. """
  1669. Close the connection, raise an exception if we were watching.
  1670. The supported exceptions are already checked in the
  1671. retry object so we don't need to do it here.
  1672. After we disconnect the connection, it will try to reconnect and
  1673. do a health check as part of the send_command logic(on connection level).
  1674. """
  1675. if error and failure_count <= conn.retry.get_retries():
  1676. record_operation_duration(
  1677. command_name=command_name,
  1678. duration_seconds=time.monotonic() - start_time,
  1679. server_address=getattr(conn, "host", None),
  1680. server_port=getattr(conn, "port", None),
  1681. db_namespace=str(conn.db),
  1682. error=error,
  1683. retry_attempts=failure_count,
  1684. )
  1685. conn.disconnect()
  1686. # if we were watching a variable, the watch is no longer valid
  1687. # since this connection has died. raise a WatchError, which
  1688. # indicates the user should retry this transaction.
  1689. if self.watching:
  1690. raise WatchError(
  1691. f"A {type(error).__name__} occurred while watching one or more keys"
  1692. )
  1693. def execute(self, raise_on_error: bool = True) -> List[Any]:
  1694. """Execute all the commands in the current pipeline"""
  1695. stack = self.command_stack
  1696. if not stack and not self.watching:
  1697. return []
  1698. if self.scripts:
  1699. self.load_scripts()
  1700. if self.transaction or self.explicit_transaction:
  1701. execute = self._execute_transaction
  1702. operation_name = "MULTI"
  1703. else:
  1704. execute = self._execute_pipeline
  1705. operation_name = "PIPELINE"
  1706. conn = self.connection
  1707. if not conn:
  1708. conn = self.connection_pool.get_connection()
  1709. # assign to self.connection so reset() releases the connection
  1710. # back to the pool after we're done
  1711. self.connection = conn
  1712. # Start timing for observability
  1713. start_time = time.monotonic()
  1714. # Track actual retry attempts for error reporting
  1715. actual_retry_attempts = [0]
  1716. def failure_callback(error, failure_count):
  1717. actual_retry_attempts[0] = failure_count
  1718. self._disconnect_raise_on_watching(
  1719. conn, error, failure_count, start_time, operation_name
  1720. )
  1721. try:
  1722. response = conn.retry.call_with_retry(
  1723. lambda: execute(conn, stack, raise_on_error),
  1724. failure_callback,
  1725. with_failure_count=True,
  1726. )
  1727. record_operation_duration(
  1728. command_name=operation_name,
  1729. duration_seconds=time.monotonic() - start_time,
  1730. server_address=getattr(conn, "host", None),
  1731. server_port=getattr(conn, "port", None),
  1732. db_namespace=str(conn.db),
  1733. )
  1734. return response
  1735. except Exception as e:
  1736. record_error_count(
  1737. server_address=getattr(conn, "host", None),
  1738. server_port=getattr(conn, "port", None),
  1739. network_peer_address=getattr(conn, "host", None),
  1740. network_peer_port=getattr(conn, "port", None),
  1741. error_type=e,
  1742. retry_attempts=actual_retry_attempts[0],
  1743. is_internal=False,
  1744. )
  1745. raise
  1746. finally:
  1747. # in reset() the connection is disconnected before returned to the pool if
  1748. # it is marked for reconnect.
  1749. self.reset()
  1750. def discard(self):
  1751. """
  1752. Flushes all previously queued commands
  1753. See: https://redis.io/commands/DISCARD
  1754. """
  1755. self.execute_command("DISCARD")
  1756. def watch(self, *names):
  1757. """Watches the values at keys ``names``"""
  1758. if self.explicit_transaction:
  1759. raise RedisError("Cannot issue a WATCH after a MULTI")
  1760. return self.execute_command("WATCH", *names)
  1761. def unwatch(self) -> bool:
  1762. """Unwatches all previously specified keys"""
  1763. return self.watching and self.execute_command("UNWATCH") or True