图片解析应用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1525 lines
57 KiB

  1. import copy
  2. import re
  3. import threading
  4. import time
  5. import warnings
  6. from itertools import chain
  7. from typing import Any, Callable, Dict, List, Optional, Type, Union
  8. from redis._parsers.encoders import Encoder
  9. from redis._parsers.helpers import (
  10. _RedisCallbacks,
  11. _RedisCallbacksRESP2,
  12. _RedisCallbacksRESP3,
  13. bool_ok,
  14. )
  15. from redis.commands import (
  16. CoreCommands,
  17. RedisModuleCommands,
  18. SentinelCommands,
  19. list_or_args,
  20. )
  21. from redis.connection import (
  22. AbstractConnection,
  23. ConnectionPool,
  24. SSLConnection,
  25. UnixDomainSocketConnection,
  26. )
  27. from redis.credentials import CredentialProvider
  28. from redis.exceptions import (
  29. ConnectionError,
  30. ExecAbortError,
  31. PubSubError,
  32. RedisError,
  33. ResponseError,
  34. TimeoutError,
  35. WatchError,
  36. )
  37. from redis.lock import Lock
  38. from redis.retry import Retry
  39. from redis.utils import (
  40. HIREDIS_AVAILABLE,
  41. _set_info_logger,
  42. get_lib_version,
  43. safe_str,
  44. str_if_bytes,
  45. )
  46. SYM_EMPTY = b""
  47. EMPTY_RESPONSE = "EMPTY_RESPONSE"
  48. # some responses (ie. dump) are binary, and just meant to never be decoded
  49. NEVER_DECODE = "NEVER_DECODE"
  50. class CaseInsensitiveDict(dict):
  51. "Case insensitive dict implementation. Assumes string keys only."
  52. def __init__(self, data: Dict[str, str]) -> None:
  53. for k, v in data.items():
  54. self[k.upper()] = v
  55. def __contains__(self, k):
  56. return super().__contains__(k.upper())
  57. def __delitem__(self, k):
  58. super().__delitem__(k.upper())
  59. def __getitem__(self, k):
  60. return super().__getitem__(k.upper())
  61. def get(self, k, default=None):
  62. return super().get(k.upper(), default)
  63. def __setitem__(self, k, v):
  64. super().__setitem__(k.upper(), v)
  65. def update(self, data):
  66. data = CaseInsensitiveDict(data)
  67. super().update(data)
  68. class AbstractRedis:
  69. pass
  70. class Redis(RedisModuleCommands, CoreCommands, SentinelCommands):
  71. """
  72. Implementation of the Redis protocol.
  73. This abstract class provides a Python interface to all Redis commands
  74. and an implementation of the Redis protocol.
  75. Pipelines derive from this, implementing how
  76. the commands are sent and received to the Redis server. Based on
  77. configuration, an instance will either use a ConnectionPool, or
  78. Connection object to talk to redis.
  79. It is not safe to pass PubSub or Pipeline objects between threads.
  80. """
  81. @classmethod
  82. def from_url(cls, url: str, **kwargs) -> "Redis":
  83. """
  84. Return a Redis client object configured from the given URL
  85. For example::
  86. redis://[[username]:[password]]@localhost:6379/0
  87. rediss://[[username]:[password]]@localhost:6379/0
  88. unix://[username@]/path/to/socket.sock?db=0[&password=password]
  89. Three URL schemes are supported:
  90. - `redis://` creates a TCP socket connection. See more at:
  91. <https://www.iana.org/assignments/uri-schemes/prov/redis>
  92. - `rediss://` creates a SSL wrapped TCP socket connection. See more at:
  93. <https://www.iana.org/assignments/uri-schemes/prov/rediss>
  94. - ``unix://``: creates a Unix Domain Socket connection.
  95. The username, password, hostname, path and all querystring values
  96. are passed through urllib.parse.unquote in order to replace any
  97. percent-encoded values with their corresponding characters.
  98. There are several ways to specify a database number. The first value
  99. found will be used:
  100. 1. A ``db`` querystring option, e.g. redis://localhost?db=0
  101. 2. If using the redis:// or rediss:// schemes, the path argument
  102. of the url, e.g. redis://localhost/0
  103. 3. A ``db`` keyword argument to this function.
  104. If none of these options are specified, the default db=0 is used.
  105. All querystring options are cast to their appropriate Python types.
  106. Boolean arguments can be specified with string values "True"/"False"
  107. or "Yes"/"No". Values that cannot be properly cast cause a
  108. ``ValueError`` to be raised. Once parsed, the querystring arguments
  109. and keyword arguments are passed to the ``ConnectionPool``'s
  110. class initializer. In the case of conflicting arguments, querystring
  111. arguments always win.
  112. """
  113. single_connection_client = kwargs.pop("single_connection_client", False)
  114. connection_pool = ConnectionPool.from_url(url, **kwargs)
  115. client = cls(
  116. connection_pool=connection_pool,
  117. single_connection_client=single_connection_client,
  118. )
  119. client.auto_close_connection_pool = True
  120. return client
  121. @classmethod
  122. def from_pool(
  123. cls: Type["Redis"],
  124. connection_pool: ConnectionPool,
  125. ) -> "Redis":
  126. """
  127. Return a Redis client from the given connection pool.
  128. The Redis client will take ownership of the connection pool and
  129. close it when the Redis client is closed.
  130. """
  131. client = cls(
  132. connection_pool=connection_pool,
  133. )
  134. client.auto_close_connection_pool = True
  135. return client
  136. def __init__(
  137. self,
  138. host="localhost",
  139. port=6379,
  140. db=0,
  141. password=None,
  142. socket_timeout=None,
  143. socket_connect_timeout=None,
  144. socket_keepalive=None,
  145. socket_keepalive_options=None,
  146. connection_pool=None,
  147. unix_socket_path=None,
  148. encoding="utf-8",
  149. encoding_errors="strict",
  150. charset=None,
  151. errors=None,
  152. decode_responses=False,
  153. retry_on_timeout=False,
  154. retry_on_error=None,
  155. ssl=False,
  156. ssl_keyfile=None,
  157. ssl_certfile=None,
  158. ssl_cert_reqs="required",
  159. ssl_ca_certs=None,
  160. ssl_ca_path=None,
  161. ssl_ca_data=None,
  162. ssl_check_hostname=False,
  163. ssl_password=None,
  164. ssl_validate_ocsp=False,
  165. ssl_validate_ocsp_stapled=False,
  166. ssl_ocsp_context=None,
  167. ssl_ocsp_expected_cert=None,
  168. ssl_min_version=None,
  169. ssl_ciphers=None,
  170. max_connections=None,
  171. single_connection_client=False,
  172. health_check_interval=0,
  173. client_name=None,
  174. lib_name="redis-py",
  175. lib_version=get_lib_version(),
  176. username=None,
  177. retry=None,
  178. redis_connect_func=None,
  179. credential_provider: Optional[CredentialProvider] = None,
  180. protocol: Optional[int] = 2,
  181. ) -> None:
  182. """
  183. Initialize a new Redis client.
  184. To specify a retry policy for specific errors, first set
  185. `retry_on_error` to a list of the error/s to retry on, then set
  186. `retry` to a valid `Retry` object.
  187. To retry on TimeoutError, `retry_on_timeout` can also be set to `True`.
  188. Args:
  189. single_connection_client:
  190. if `True`, connection pool is not used. In that case `Redis`
  191. instance use is not thread safe.
  192. """
  193. if not connection_pool:
  194. if charset is not None:
  195. warnings.warn(
  196. DeprecationWarning(
  197. '"charset" is deprecated. Use "encoding" instead'
  198. )
  199. )
  200. encoding = charset
  201. if errors is not None:
  202. warnings.warn(
  203. DeprecationWarning(
  204. '"errors" is deprecated. Use "encoding_errors" instead'
  205. )
  206. )
  207. encoding_errors = errors
  208. if not retry_on_error:
  209. retry_on_error = []
  210. if retry_on_timeout is True:
  211. retry_on_error.append(TimeoutError)
  212. kwargs = {
  213. "db": db,
  214. "username": username,
  215. "password": password,
  216. "socket_timeout": socket_timeout,
  217. "encoding": encoding,
  218. "encoding_errors": encoding_errors,
  219. "decode_responses": decode_responses,
  220. "retry_on_error": retry_on_error,
  221. "retry": copy.deepcopy(retry),
  222. "max_connections": max_connections,
  223. "health_check_interval": health_check_interval,
  224. "client_name": client_name,
  225. "lib_name": lib_name,
  226. "lib_version": lib_version,
  227. "redis_connect_func": redis_connect_func,
  228. "credential_provider": credential_provider,
  229. "protocol": protocol,
  230. }
  231. # based on input, setup appropriate connection args
  232. if unix_socket_path is not None:
  233. kwargs.update(
  234. {
  235. "path": unix_socket_path,
  236. "connection_class": UnixDomainSocketConnection,
  237. }
  238. )
  239. else:
  240. # TCP specific options
  241. kwargs.update(
  242. {
  243. "host": host,
  244. "port": port,
  245. "socket_connect_timeout": socket_connect_timeout,
  246. "socket_keepalive": socket_keepalive,
  247. "socket_keepalive_options": socket_keepalive_options,
  248. }
  249. )
  250. if ssl:
  251. kwargs.update(
  252. {
  253. "connection_class": SSLConnection,
  254. "ssl_keyfile": ssl_keyfile,
  255. "ssl_certfile": ssl_certfile,
  256. "ssl_cert_reqs": ssl_cert_reqs,
  257. "ssl_ca_certs": ssl_ca_certs,
  258. "ssl_ca_data": ssl_ca_data,
  259. "ssl_check_hostname": ssl_check_hostname,
  260. "ssl_password": ssl_password,
  261. "ssl_ca_path": ssl_ca_path,
  262. "ssl_validate_ocsp_stapled": ssl_validate_ocsp_stapled,
  263. "ssl_validate_ocsp": ssl_validate_ocsp,
  264. "ssl_ocsp_context": ssl_ocsp_context,
  265. "ssl_ocsp_expected_cert": ssl_ocsp_expected_cert,
  266. "ssl_min_version": ssl_min_version,
  267. "ssl_ciphers": ssl_ciphers,
  268. }
  269. )
  270. connection_pool = ConnectionPool(**kwargs)
  271. self.auto_close_connection_pool = True
  272. else:
  273. self.auto_close_connection_pool = False
  274. self.connection_pool = connection_pool
  275. self.connection = None
  276. if single_connection_client:
  277. self.connection = self.connection_pool.get_connection("_")
  278. self.response_callbacks = CaseInsensitiveDict(_RedisCallbacks)
  279. if self.connection_pool.connection_kwargs.get("protocol") in ["3", 3]:
  280. self.response_callbacks.update(_RedisCallbacksRESP3)
  281. else:
  282. self.response_callbacks.update(_RedisCallbacksRESP2)
  283. def __repr__(self) -> str:
  284. return (
  285. f"<{type(self).__module__}.{type(self).__name__}"
  286. f"({repr(self.connection_pool)})>"
  287. )
  288. def get_encoder(self) -> "Encoder":
  289. """Get the connection pool's encoder"""
  290. return self.connection_pool.get_encoder()
  291. def get_connection_kwargs(self) -> Dict:
  292. """Get the connection's key-word arguments"""
  293. return self.connection_pool.connection_kwargs
  294. def get_retry(self) -> Optional["Retry"]:
  295. return self.get_connection_kwargs().get("retry")
  296. def set_retry(self, retry: "Retry") -> None:
  297. self.get_connection_kwargs().update({"retry": retry})
  298. self.connection_pool.set_retry(retry)
  299. def set_response_callback(self, command: str, callback: Callable) -> None:
  300. """Set a custom Response Callback"""
  301. self.response_callbacks[command] = callback
  302. def load_external_module(self, funcname, func) -> None:
  303. """
  304. This function can be used to add externally defined redis modules,
  305. and their namespaces to the redis client.
  306. funcname - A string containing the name of the function to create
  307. func - The function, being added to this class.
  308. ex: Assume that one has a custom redis module named foomod that
  309. creates command named 'foo.dothing' and 'foo.anotherthing' in redis.
  310. To load function functions into this namespace:
  311. from redis import Redis
  312. from foomodule import F
  313. r = Redis()
  314. r.load_external_module("foo", F)
  315. r.foo().dothing('your', 'arguments')
  316. For a concrete example see the reimport of the redisjson module in
  317. tests/test_connection.py::test_loading_external_modules
  318. """
  319. setattr(self, funcname, func)
  320. def pipeline(self, transaction=True, shard_hint=None) -> "Pipeline":
  321. """
  322. Return a new pipeline object that can queue multiple commands for
  323. later execution. ``transaction`` indicates whether all commands
  324. should be executed atomically. Apart from making a group of operations
  325. atomic, pipelines are useful for reducing the back-and-forth overhead
  326. between the client and server.
  327. """
  328. return Pipeline(
  329. self.connection_pool, self.response_callbacks, transaction, shard_hint
  330. )
  331. def transaction(
  332. self, func: Callable[["Pipeline"], None], *watches, **kwargs
  333. ) -> None:
  334. """
  335. Convenience method for executing the callable `func` as a transaction
  336. while watching all keys specified in `watches`. The 'func' callable
  337. should expect a single argument which is a Pipeline object.
  338. """
  339. shard_hint = kwargs.pop("shard_hint", None)
  340. value_from_callable = kwargs.pop("value_from_callable", False)
  341. watch_delay = kwargs.pop("watch_delay", None)
  342. with self.pipeline(True, shard_hint) as pipe:
  343. while True:
  344. try:
  345. if watches:
  346. pipe.watch(*watches)
  347. func_value = func(pipe)
  348. exec_value = pipe.execute()
  349. return func_value if value_from_callable else exec_value
  350. except WatchError:
  351. if watch_delay is not None and watch_delay > 0:
  352. time.sleep(watch_delay)
  353. continue
  354. def lock(
  355. self,
  356. name: str,
  357. timeout: Optional[float] = None,
  358. sleep: float = 0.1,
  359. blocking: bool = True,
  360. blocking_timeout: Optional[float] = None,
  361. lock_class: Union[None, Any] = None,
  362. thread_local: bool = True,
  363. ):
  364. """
  365. Return a new Lock object using key ``name`` that mimics
  366. the behavior of threading.Lock.
  367. If specified, ``timeout`` indicates a maximum life for the lock.
  368. By default, it will remain locked until release() is called.
  369. ``sleep`` indicates the amount of time to sleep per loop iteration
  370. when the lock is in blocking mode and another client is currently
  371. holding the lock.
  372. ``blocking`` indicates whether calling ``acquire`` should block until
  373. the lock has been acquired or to fail immediately, causing ``acquire``
  374. to return False and the lock not being acquired. Defaults to True.
  375. Note this value can be overridden by passing a ``blocking``
  376. argument to ``acquire``.
  377. ``blocking_timeout`` indicates the maximum amount of time in seconds to
  378. spend trying to acquire the lock. A value of ``None`` indicates
  379. continue trying forever. ``blocking_timeout`` can be specified as a
  380. float or integer, both representing the number of seconds to wait.
  381. ``lock_class`` forces the specified lock implementation. Note that as
  382. of redis-py 3.0, the only lock class we implement is ``Lock`` (which is
  383. a Lua-based lock). So, it's unlikely you'll need this parameter, unless
  384. you have created your own custom lock class.
  385. ``thread_local`` indicates whether the lock token is placed in
  386. thread-local storage. By default, the token is placed in thread local
  387. storage so that a thread only sees its token, not a token set by
  388. another thread. Consider the following timeline:
  389. time: 0, thread-1 acquires `my-lock`, with a timeout of 5 seconds.
  390. thread-1 sets the token to "abc"
  391. time: 1, thread-2 blocks trying to acquire `my-lock` using the
  392. Lock instance.
  393. time: 5, thread-1 has not yet completed. redis expires the lock
  394. key.
  395. time: 5, thread-2 acquired `my-lock` now that it's available.
  396. thread-2 sets the token to "xyz"
  397. time: 6, thread-1 finishes its work and calls release(). if the
  398. token is *not* stored in thread local storage, then
  399. thread-1 would see the token value as "xyz" and would be
  400. able to successfully release the thread-2's lock.
  401. In some use cases it's necessary to disable thread local storage. For
  402. example, if you have code where one thread acquires a lock and passes
  403. that lock instance to a worker thread to release later. If thread
  404. local storage isn't disabled in this case, the worker thread won't see
  405. the token set by the thread that acquired the lock. Our assumption
  406. is that these cases aren't common and as such default to using
  407. thread local storage."""
  408. if lock_class is None:
  409. lock_class = Lock
  410. return lock_class(
  411. self,
  412. name,
  413. timeout=timeout,
  414. sleep=sleep,
  415. blocking=blocking,
  416. blocking_timeout=blocking_timeout,
  417. thread_local=thread_local,
  418. )
  419. def pubsub(self, **kwargs):
  420. """
  421. Return a Publish/Subscribe object. With this object, you can
  422. subscribe to channels and listen for messages that get published to
  423. them.
  424. """
  425. return PubSub(self.connection_pool, **kwargs)
  426. def monitor(self):
  427. return Monitor(self.connection_pool)
  428. def client(self):
  429. return self.__class__(
  430. connection_pool=self.connection_pool, single_connection_client=True
  431. )
  432. def __enter__(self):
  433. return self
  434. def __exit__(self, exc_type, exc_value, traceback):
  435. self.close()
  436. def __del__(self):
  437. self.close()
  438. def close(self):
  439. # In case a connection property does not yet exist
  440. # (due to a crash earlier in the Redis() constructor), return
  441. # immediately as there is nothing to clean-up.
  442. if not hasattr(self, "connection"):
  443. return
  444. conn = self.connection
  445. if conn:
  446. self.connection = None
  447. self.connection_pool.release(conn)
  448. if self.auto_close_connection_pool:
  449. self.connection_pool.disconnect()
  450. def _send_command_parse_response(self, conn, command_name, *args, **options):
  451. """
  452. Send a command and parse the response
  453. """
  454. conn.send_command(*args)
  455. return self.parse_response(conn, command_name, **options)
  456. def _disconnect_raise(self, conn, error):
  457. """
  458. Close the connection and raise an exception
  459. if retry_on_error is not set or the error
  460. is not one of the specified error types
  461. """
  462. conn.disconnect()
  463. if (
  464. conn.retry_on_error is None
  465. or isinstance(error, tuple(conn.retry_on_error)) is False
  466. ):
  467. raise error
  468. # COMMAND EXECUTION AND PROTOCOL PARSING
  469. def execute_command(self, *args, **options):
  470. """Execute a command and return a parsed response"""
  471. pool = self.connection_pool
  472. command_name = args[0]
  473. conn = self.connection or pool.get_connection(command_name, **options)
  474. try:
  475. return conn.retry.call_with_retry(
  476. lambda: self._send_command_parse_response(
  477. conn, command_name, *args, **options
  478. ),
  479. lambda error: self._disconnect_raise(conn, error),
  480. )
  481. finally:
  482. if not self.connection:
  483. pool.release(conn)
  484. def parse_response(self, connection, command_name, **options):
  485. """Parses a response from the Redis server"""
  486. try:
  487. if NEVER_DECODE in options:
  488. response = connection.read_response(disable_decoding=True)
  489. options.pop(NEVER_DECODE)
  490. else:
  491. response = connection.read_response()
  492. except ResponseError:
  493. if EMPTY_RESPONSE in options:
  494. return options[EMPTY_RESPONSE]
  495. raise
  496. if EMPTY_RESPONSE in options:
  497. options.pop(EMPTY_RESPONSE)
  498. if command_name in self.response_callbacks:
  499. return self.response_callbacks[command_name](response, **options)
  500. return response
  501. StrictRedis = Redis
  502. class Monitor:
  503. """
  504. Monitor is useful for handling the MONITOR command to the redis server.
  505. next_command() method returns one command from monitor
  506. listen() method yields commands from monitor.
  507. """
  508. monitor_re = re.compile(r"\[(\d+) (.*?)\] (.*)")
  509. command_re = re.compile(r'"(.*?)(?<!\\)"')
  510. def __init__(self, connection_pool):
  511. self.connection_pool = connection_pool
  512. self.connection = self.connection_pool.get_connection("MONITOR")
  513. def __enter__(self):
  514. self.connection.send_command("MONITOR")
  515. # check that monitor returns 'OK', but don't return it to user
  516. response = self.connection.read_response()
  517. if not bool_ok(response):
  518. raise RedisError(f"MONITOR failed: {response}")
  519. return self
  520. def __exit__(self, *args):
  521. self.connection.disconnect()
  522. self.connection_pool.release(self.connection)
  523. def next_command(self):
  524. """Parse the response from a monitor command"""
  525. response = self.connection.read_response()
  526. if isinstance(response, bytes):
  527. response = self.connection.encoder.decode(response, force=True)
  528. command_time, command_data = response.split(" ", 1)
  529. m = self.monitor_re.match(command_data)
  530. db_id, client_info, command = m.groups()
  531. command = " ".join(self.command_re.findall(command))
  532. # Redis escapes double quotes because each piece of the command
  533. # string is surrounded by double quotes. We don't have that
  534. # requirement so remove the escaping and leave the quote.
  535. command = command.replace('\\"', '"')
  536. if client_info == "lua":
  537. client_address = "lua"
  538. client_port = ""
  539. client_type = "lua"
  540. elif client_info.startswith("unix"):
  541. client_address = "unix"
  542. client_port = client_info[5:]
  543. client_type = "unix"
  544. else:
  545. # use rsplit as ipv6 addresses contain colons
  546. client_address, client_port = client_info.rsplit(":", 1)
  547. client_type = "tcp"
  548. return {
  549. "time": float(command_time),
  550. "db": int(db_id),
  551. "client_address": client_address,
  552. "client_port": client_port,
  553. "client_type": client_type,
  554. "command": command,
  555. }
  556. def listen(self):
  557. """Listen for commands coming to the server."""
  558. while True:
  559. yield self.next_command()
  560. class PubSub:
  561. """
  562. PubSub provides publish, subscribe and listen support to Redis channels.
  563. After subscribing to one or more channels, the listen() method will block
  564. until a message arrives on one of the subscribed channels. That message
  565. will be returned and it's safe to start listening again.
  566. """
  567. PUBLISH_MESSAGE_TYPES = ("message", "pmessage", "smessage")
  568. UNSUBSCRIBE_MESSAGE_TYPES = ("unsubscribe", "punsubscribe", "sunsubscribe")
  569. HEALTH_CHECK_MESSAGE = "redis-py-health-check"
  570. def __init__(
  571. self,
  572. connection_pool,
  573. shard_hint=None,
  574. ignore_subscribe_messages: bool = False,
  575. encoder: Optional["Encoder"] = None,
  576. push_handler_func: Union[None, Callable[[str], None]] = None,
  577. ):
  578. self.connection_pool = connection_pool
  579. self.shard_hint = shard_hint
  580. self.ignore_subscribe_messages = ignore_subscribe_messages
  581. self.connection = None
  582. self.subscribed_event = threading.Event()
  583. # we need to know the encoding options for this connection in order
  584. # to lookup channel and pattern names for callback handlers.
  585. self.encoder = encoder
  586. self.push_handler_func = push_handler_func
  587. if self.encoder is None:
  588. self.encoder = self.connection_pool.get_encoder()
  589. self.health_check_response_b = self.encoder.encode(self.HEALTH_CHECK_MESSAGE)
  590. if self.encoder.decode_responses:
  591. self.health_check_response = ["pong", self.HEALTH_CHECK_MESSAGE]
  592. else:
  593. self.health_check_response = [b"pong", self.health_check_response_b]
  594. if self.push_handler_func is None:
  595. _set_info_logger()
  596. self.reset()
  597. def __enter__(self) -> "PubSub":
  598. return self
  599. def __exit__(self, exc_type, exc_value, traceback) -> None:
  600. self.reset()
  601. def __del__(self) -> None:
  602. try:
  603. # if this object went out of scope prior to shutting down
  604. # subscriptions, close the connection manually before
  605. # returning it to the connection pool
  606. self.reset()
  607. except Exception:
  608. pass
  609. def reset(self) -> None:
  610. if self.connection:
  611. self.connection.disconnect()
  612. self.connection.deregister_connect_callback(self.on_connect)
  613. self.connection_pool.release(self.connection)
  614. self.connection = None
  615. self.health_check_response_counter = 0
  616. self.channels = {}
  617. self.pending_unsubscribe_channels = set()
  618. self.shard_channels = {}
  619. self.pending_unsubscribe_shard_channels = set()
  620. self.patterns = {}
  621. self.pending_unsubscribe_patterns = set()
  622. self.subscribed_event.clear()
  623. def close(self) -> None:
  624. self.reset()
  625. def on_connect(self, connection) -> None:
  626. "Re-subscribe to any channels and patterns previously subscribed to"
  627. # NOTE: for python3, we can't pass bytestrings as keyword arguments
  628. # so we need to decode channel/pattern names back to unicode strings
  629. # before passing them to [p]subscribe.
  630. self.pending_unsubscribe_channels.clear()
  631. self.pending_unsubscribe_patterns.clear()
  632. self.pending_unsubscribe_shard_channels.clear()
  633. if self.channels:
  634. channels = {
  635. self.encoder.decode(k, force=True): v for k, v in self.channels.items()
  636. }
  637. self.subscribe(**channels)
  638. if self.patterns:
  639. patterns = {
  640. self.encoder.decode(k, force=True): v for k, v in self.patterns.items()
  641. }
  642. self.psubscribe(**patterns)
  643. if self.shard_channels:
  644. shard_channels = {
  645. self.encoder.decode(k, force=True): v
  646. for k, v in self.shard_channels.items()
  647. }
  648. self.ssubscribe(**shard_channels)
  649. @property
  650. def subscribed(self) -> bool:
  651. """Indicates if there are subscriptions to any channels or patterns"""
  652. return self.subscribed_event.is_set()
  653. def execute_command(self, *args):
  654. """Execute a publish/subscribe command"""
  655. # NOTE: don't parse the response in this function -- it could pull a
  656. # legitimate message off the stack if the connection is already
  657. # subscribed to one or more channels
  658. if self.connection is None:
  659. self.connection = self.connection_pool.get_connection(
  660. "pubsub", self.shard_hint
  661. )
  662. # register a callback that re-subscribes to any channels we
  663. # were listening to when we were disconnected
  664. self.connection.register_connect_callback(self.on_connect)
  665. if self.push_handler_func is not None and not HIREDIS_AVAILABLE:
  666. self.connection._parser.set_push_handler(self.push_handler_func)
  667. connection = self.connection
  668. kwargs = {"check_health": not self.subscribed}
  669. if not self.subscribed:
  670. self.clean_health_check_responses()
  671. self._execute(connection, connection.send_command, *args, **kwargs)
  672. def clean_health_check_responses(self) -> None:
  673. """
  674. If any health check responses are present, clean them
  675. """
  676. ttl = 10
  677. conn = self.connection
  678. while self.health_check_response_counter > 0 and ttl > 0:
  679. if self._execute(conn, conn.can_read, timeout=conn.socket_timeout):
  680. response = self._execute(conn, conn.read_response)
  681. if self.is_health_check_response(response):
  682. self.health_check_response_counter -= 1
  683. else:
  684. raise PubSubError(
  685. "A non health check response was cleaned by "
  686. "execute_command: {0}".format(response)
  687. )
  688. ttl -= 1
  689. def _disconnect_raise_connect(self, conn, error) -> None:
  690. """
  691. Close the connection and raise an exception
  692. if retry_on_error is not set or the error is not one
  693. of the specified error types. Otherwise, try to
  694. reconnect
  695. """
  696. conn.disconnect()
  697. if (
  698. conn.retry_on_error is None
  699. or isinstance(error, tuple(conn.retry_on_error)) is False
  700. ):
  701. raise error
  702. conn.connect()
  703. def _execute(self, conn, command, *args, **kwargs):
  704. """
  705. Connect manually upon disconnection. If the Redis server is down,
  706. this will fail and raise a ConnectionError as desired.
  707. After reconnection, the ``on_connect`` callback should have been
  708. called by the # connection to resubscribe us to any channels and
  709. patterns we were previously listening to
  710. """
  711. return conn.retry.call_with_retry(
  712. lambda: command(*args, **kwargs),
  713. lambda error: self._disconnect_raise_connect(conn, error),
  714. )
  715. def parse_response(self, block=True, timeout=0):
  716. """Parse the response from a publish/subscribe command"""
  717. conn = self.connection
  718. if conn is None:
  719. raise RuntimeError(
  720. "pubsub connection not set: "
  721. "did you forget to call subscribe() or psubscribe()?"
  722. )
  723. self.check_health()
  724. def try_read():
  725. if not block:
  726. if not conn.can_read(timeout=timeout):
  727. return None
  728. else:
  729. conn.connect()
  730. return conn.read_response(disconnect_on_error=False, push_request=True)
  731. response = self._execute(conn, try_read)
  732. if self.is_health_check_response(response):
  733. # ignore the health check message as user might not expect it
  734. self.health_check_response_counter -= 1
  735. return None
  736. return response
  737. def is_health_check_response(self, response) -> bool:
  738. """
  739. Check if the response is a health check response.
  740. If there are no subscriptions redis responds to PING command with a
  741. bulk response, instead of a multi-bulk with "pong" and the response.
  742. """
  743. return response in [
  744. self.health_check_response, # If there was a subscription
  745. self.health_check_response_b, # If there wasn't
  746. ]
  747. def check_health(self) -> None:
  748. conn = self.connection
  749. if conn is None:
  750. raise RuntimeError(
  751. "pubsub connection not set: "
  752. "did you forget to call subscribe() or psubscribe()?"
  753. )
  754. if conn.health_check_interval and time.time() > conn.next_health_check:
  755. conn.send_command("PING", self.HEALTH_CHECK_MESSAGE, check_health=False)
  756. self.health_check_response_counter += 1
  757. def _normalize_keys(self, data) -> Dict:
  758. """
  759. normalize channel/pattern names to be either bytes or strings
  760. based on whether responses are automatically decoded. this saves us
  761. from coercing the value for each message coming in.
  762. """
  763. encode = self.encoder.encode
  764. decode = self.encoder.decode
  765. return {decode(encode(k)): v for k, v in data.items()}
  766. def psubscribe(self, *args, **kwargs):
  767. """
  768. Subscribe to channel patterns. Patterns supplied as keyword arguments
  769. expect a pattern name as the key and a callable as the value. A
  770. pattern's callable will be invoked automatically when a message is
  771. received on that pattern rather than producing a message via
  772. ``listen()``.
  773. """
  774. if args:
  775. args = list_or_args(args[0], args[1:])
  776. new_patterns = dict.fromkeys(args)
  777. new_patterns.update(kwargs)
  778. ret_val = self.execute_command("PSUBSCRIBE", *new_patterns.keys())
  779. # update the patterns dict AFTER we send the command. we don't want to
  780. # subscribe twice to these patterns, once for the command and again
  781. # for the reconnection.
  782. new_patterns = self._normalize_keys(new_patterns)
  783. self.patterns.update(new_patterns)
  784. if not self.subscribed:
  785. # Set the subscribed_event flag to True
  786. self.subscribed_event.set()
  787. # Clear the health check counter
  788. self.health_check_response_counter = 0
  789. self.pending_unsubscribe_patterns.difference_update(new_patterns)
  790. return ret_val
  791. def punsubscribe(self, *args):
  792. """
  793. Unsubscribe from the supplied patterns. If empty, unsubscribe from
  794. all patterns.
  795. """
  796. if args:
  797. args = list_or_args(args[0], args[1:])
  798. patterns = self._normalize_keys(dict.fromkeys(args))
  799. else:
  800. patterns = self.patterns
  801. self.pending_unsubscribe_patterns.update(patterns)
  802. return self.execute_command("PUNSUBSCRIBE", *args)
  803. def subscribe(self, *args, **kwargs):
  804. """
  805. Subscribe to channels. Channels supplied as keyword arguments expect
  806. a channel name as the key and a callable as the value. A channel's
  807. callable will be invoked automatically when a message is received on
  808. that channel rather than producing a message via ``listen()`` or
  809. ``get_message()``.
  810. """
  811. if args:
  812. args = list_or_args(args[0], args[1:])
  813. new_channels = dict.fromkeys(args)
  814. new_channels.update(kwargs)
  815. ret_val = self.execute_command("SUBSCRIBE", *new_channels.keys())
  816. # update the channels dict AFTER we send the command. we don't want to
  817. # subscribe twice to these channels, once for the command and again
  818. # for the reconnection.
  819. new_channels = self._normalize_keys(new_channels)
  820. self.channels.update(new_channels)
  821. if not self.subscribed:
  822. # Set the subscribed_event flag to True
  823. self.subscribed_event.set()
  824. # Clear the health check counter
  825. self.health_check_response_counter = 0
  826. self.pending_unsubscribe_channels.difference_update(new_channels)
  827. return ret_val
  828. def unsubscribe(self, *args):
  829. """
  830. Unsubscribe from the supplied channels. If empty, unsubscribe from
  831. all channels
  832. """
  833. if args:
  834. args = list_or_args(args[0], args[1:])
  835. channels = self._normalize_keys(dict.fromkeys(args))
  836. else:
  837. channels = self.channels
  838. self.pending_unsubscribe_channels.update(channels)
  839. return self.execute_command("UNSUBSCRIBE", *args)
  840. def ssubscribe(self, *args, target_node=None, **kwargs):
  841. """
  842. Subscribes the client to the specified shard channels.
  843. Channels supplied as keyword arguments expect a channel name as the key
  844. and a callable as the value. A channel's callable will be invoked automatically
  845. when a message is received on that channel rather than producing a message via
  846. ``listen()`` or ``get_sharded_message()``.
  847. """
  848. if args:
  849. args = list_or_args(args[0], args[1:])
  850. new_s_channels = dict.fromkeys(args)
  851. new_s_channels.update(kwargs)
  852. ret_val = self.execute_command("SSUBSCRIBE", *new_s_channels.keys())
  853. # update the s_channels dict AFTER we send the command. we don't want to
  854. # subscribe twice to these channels, once for the command and again
  855. # for the reconnection.
  856. new_s_channels = self._normalize_keys(new_s_channels)
  857. self.shard_channels.update(new_s_channels)
  858. if not self.subscribed:
  859. # Set the subscribed_event flag to True
  860. self.subscribed_event.set()
  861. # Clear the health check counter
  862. self.health_check_response_counter = 0
  863. self.pending_unsubscribe_shard_channels.difference_update(new_s_channels)
  864. return ret_val
  865. def sunsubscribe(self, *args, target_node=None):
  866. """
  867. Unsubscribe from the supplied shard_channels. If empty, unsubscribe from
  868. all shard_channels
  869. """
  870. if args:
  871. args = list_or_args(args[0], args[1:])
  872. s_channels = self._normalize_keys(dict.fromkeys(args))
  873. else:
  874. s_channels = self.shard_channels
  875. self.pending_unsubscribe_shard_channels.update(s_channels)
  876. return self.execute_command("SUNSUBSCRIBE", *args)
  877. def listen(self):
  878. "Listen for messages on channels this client has been subscribed to"
  879. while self.subscribed:
  880. response = self.handle_message(self.parse_response(block=True))
  881. if response is not None:
  882. yield response
  883. def get_message(
  884. self, ignore_subscribe_messages: bool = False, timeout: float = 0.0
  885. ):
  886. """
  887. Get the next message if one is available, otherwise None.
  888. If timeout is specified, the system will wait for `timeout` seconds
  889. before returning. Timeout should be specified as a floating point
  890. number, or None, to wait indefinitely.
  891. """
  892. if not self.subscribed:
  893. # Wait for subscription
  894. start_time = time.time()
  895. if self.subscribed_event.wait(timeout) is True:
  896. # The connection was subscribed during the timeout time frame.
  897. # The timeout should be adjusted based on the time spent
  898. # waiting for the subscription
  899. time_spent = time.time() - start_time
  900. timeout = max(0.0, timeout - time_spent)
  901. else:
  902. # The connection isn't subscribed to any channels or patterns,
  903. # so no messages are available
  904. return None
  905. response = self.parse_response(block=(timeout is None), timeout=timeout)
  906. if response:
  907. return self.handle_message(response, ignore_subscribe_messages)
  908. return None
  909. get_sharded_message = get_message
  910. def ping(self, message: Union[str, None] = None) -> bool:
  911. """
  912. Ping the Redis server
  913. """
  914. args = ["PING", message] if message is not None else ["PING"]
  915. return self.execute_command(*args)
  916. def handle_message(self, response, ignore_subscribe_messages=False):
  917. """
  918. Parses a pub/sub message. If the channel or pattern was subscribed to
  919. with a message handler, the handler is invoked instead of a parsed
  920. message being returned.
  921. """
  922. if response is None:
  923. return None
  924. if isinstance(response, bytes):
  925. response = [b"pong", response] if response != b"PONG" else [b"pong", b""]
  926. message_type = str_if_bytes(response[0])
  927. if message_type == "pmessage":
  928. message = {
  929. "type": message_type,
  930. "pattern": response[1],
  931. "channel": response[2],
  932. "data": response[3],
  933. }
  934. elif message_type == "pong":
  935. message = {
  936. "type": message_type,
  937. "pattern": None,
  938. "channel": None,
  939. "data": response[1],
  940. }
  941. else:
  942. message = {
  943. "type": message_type,
  944. "pattern": None,
  945. "channel": response[1],
  946. "data": response[2],
  947. }
  948. # if this is an unsubscribe message, remove it from memory
  949. if message_type in self.UNSUBSCRIBE_MESSAGE_TYPES:
  950. if message_type == "punsubscribe":
  951. pattern = response[1]
  952. if pattern in self.pending_unsubscribe_patterns:
  953. self.pending_unsubscribe_patterns.remove(pattern)
  954. self.patterns.pop(pattern, None)
  955. elif message_type == "sunsubscribe":
  956. s_channel = response[1]
  957. if s_channel in self.pending_unsubscribe_shard_channels:
  958. self.pending_unsubscribe_shard_channels.remove(s_channel)
  959. self.shard_channels.pop(s_channel, None)
  960. else:
  961. channel = response[1]
  962. if channel in self.pending_unsubscribe_channels:
  963. self.pending_unsubscribe_channels.remove(channel)
  964. self.channels.pop(channel, None)
  965. if not self.channels and not self.patterns and not self.shard_channels:
  966. # There are no subscriptions anymore, set subscribed_event flag
  967. # to false
  968. self.subscribed_event.clear()
  969. if message_type in self.PUBLISH_MESSAGE_TYPES:
  970. # if there's a message handler, invoke it
  971. if message_type == "pmessage":
  972. handler = self.patterns.get(message["pattern"], None)
  973. elif message_type == "smessage":
  974. handler = self.shard_channels.get(message["channel"], None)
  975. else:
  976. handler = self.channels.get(message["channel"], None)
  977. if handler:
  978. handler(message)
  979. return None
  980. elif message_type != "pong":
  981. # this is a subscribe/unsubscribe message. ignore if we don't
  982. # want them
  983. if ignore_subscribe_messages or self.ignore_subscribe_messages:
  984. return None
  985. return message
  986. def run_in_thread(
  987. self,
  988. sleep_time: float = 0.0,
  989. daemon: bool = False,
  990. exception_handler: Optional[Callable] = None,
  991. ) -> "PubSubWorkerThread":
  992. for channel, handler in self.channels.items():
  993. if handler is None:
  994. raise PubSubError(f"Channel: '{channel}' has no handler registered")
  995. for pattern, handler in self.patterns.items():
  996. if handler is None:
  997. raise PubSubError(f"Pattern: '{pattern}' has no handler registered")
  998. for s_channel, handler in self.shard_channels.items():
  999. if handler is None:
  1000. raise PubSubError(
  1001. f"Shard Channel: '{s_channel}' has no handler registered"
  1002. )
  1003. thread = PubSubWorkerThread(
  1004. self, sleep_time, daemon=daemon, exception_handler=exception_handler
  1005. )
  1006. thread.start()
  1007. return thread
  1008. class PubSubWorkerThread(threading.Thread):
  1009. def __init__(
  1010. self,
  1011. pubsub,
  1012. sleep_time: float,
  1013. daemon: bool = False,
  1014. exception_handler: Union[
  1015. Callable[[Exception, "PubSub", "PubSubWorkerThread"], None], None
  1016. ] = None,
  1017. ):
  1018. super().__init__()
  1019. self.daemon = daemon
  1020. self.pubsub = pubsub
  1021. self.sleep_time = sleep_time
  1022. self.exception_handler = exception_handler
  1023. self._running = threading.Event()
  1024. def run(self) -> None:
  1025. if self._running.is_set():
  1026. return
  1027. self._running.set()
  1028. pubsub = self.pubsub
  1029. sleep_time = self.sleep_time
  1030. while self._running.is_set():
  1031. try:
  1032. pubsub.get_message(ignore_subscribe_messages=True, timeout=sleep_time)
  1033. except BaseException as e:
  1034. if self.exception_handler is None:
  1035. raise
  1036. self.exception_handler(e, pubsub, self)
  1037. pubsub.close()
  1038. def stop(self) -> None:
  1039. # trip the flag so the run loop exits. the run loop will
  1040. # close the pubsub connection, which disconnects the socket
  1041. # and returns the connection to the pool.
  1042. self._running.clear()
  1043. class Pipeline(Redis):
  1044. """
  1045. Pipelines provide a way to transmit multiple commands to the Redis server
  1046. in one transmission. This is convenient for batch processing, such as
  1047. saving all the values in a list to Redis.
  1048. All commands executed within a pipeline are wrapped with MULTI and EXEC
  1049. calls. This guarantees all commands executed in the pipeline will be
  1050. executed atomically.
  1051. Any command raising an exception does *not* halt the execution of
  1052. subsequent commands in the pipeline. Instead, the exception is caught
  1053. and its instance is placed into the response list returned by execute().
  1054. Code iterating over the response list should be able to deal with an
  1055. instance of an exception as a potential value. In general, these will be
  1056. ResponseError exceptions, such as those raised when issuing a command
  1057. on a key of a different datatype.
  1058. """
  1059. UNWATCH_COMMANDS = {"DISCARD", "EXEC", "UNWATCH"}
  1060. def __init__(self, connection_pool, response_callbacks, transaction, shard_hint):
  1061. self.connection_pool = connection_pool
  1062. self.connection = None
  1063. self.response_callbacks = response_callbacks
  1064. self.transaction = transaction
  1065. self.shard_hint = shard_hint
  1066. self.watching = False
  1067. self.reset()
  1068. def __enter__(self) -> "Pipeline":
  1069. return self
  1070. def __exit__(self, exc_type, exc_value, traceback):
  1071. self.reset()
  1072. def __del__(self):
  1073. try:
  1074. self.reset()
  1075. except Exception:
  1076. pass
  1077. def __len__(self) -> int:
  1078. return len(self.command_stack)
  1079. def __bool__(self) -> bool:
  1080. """Pipeline instances should always evaluate to True"""
  1081. return True
  1082. def reset(self) -> None:
  1083. self.command_stack = []
  1084. self.scripts = set()
  1085. # make sure to reset the connection state in the event that we were
  1086. # watching something
  1087. if self.watching and self.connection:
  1088. try:
  1089. # call this manually since our unwatch or
  1090. # immediate_execute_command methods can call reset()
  1091. self.connection.send_command("UNWATCH")
  1092. self.connection.read_response()
  1093. except ConnectionError:
  1094. # disconnect will also remove any previous WATCHes
  1095. self.connection.disconnect()
  1096. # clean up the other instance attributes
  1097. self.watching = False
  1098. self.explicit_transaction = False
  1099. # we can safely return the connection to the pool here since we're
  1100. # sure we're no longer WATCHing anything
  1101. if self.connection:
  1102. self.connection_pool.release(self.connection)
  1103. self.connection = None
  1104. def close(self) -> None:
  1105. """Close the pipeline"""
  1106. self.reset()
  1107. def multi(self) -> None:
  1108. """
  1109. Start a transactional block of the pipeline after WATCH commands
  1110. are issued. End the transactional block with `execute`.
  1111. """
  1112. if self.explicit_transaction:
  1113. raise RedisError("Cannot issue nested calls to MULTI")
  1114. if self.command_stack:
  1115. raise RedisError(
  1116. "Commands without an initial WATCH have already been issued"
  1117. )
  1118. self.explicit_transaction = True
  1119. def execute_command(self, *args, **kwargs):
  1120. if (self.watching or args[0] == "WATCH") and not self.explicit_transaction:
  1121. return self.immediate_execute_command(*args, **kwargs)
  1122. return self.pipeline_execute_command(*args, **kwargs)
  1123. def _disconnect_reset_raise(self, conn, error) -> None:
  1124. """
  1125. Close the connection, reset watching state and
  1126. raise an exception if we were watching,
  1127. if retry_on_error is not set or the error is not one
  1128. of the specified error types.
  1129. """
  1130. conn.disconnect()
  1131. # if we were already watching a variable, the watch is no longer
  1132. # valid since this connection has died. raise a WatchError, which
  1133. # indicates the user should retry this transaction.
  1134. if self.watching:
  1135. self.reset()
  1136. raise WatchError(
  1137. "A ConnectionError occurred on while watching one or more keys"
  1138. )
  1139. # if retry_on_error is not set or the error is not one
  1140. # of the specified error types, raise it
  1141. if (
  1142. conn.retry_on_error is None
  1143. or isinstance(error, tuple(conn.retry_on_error)) is False
  1144. ):
  1145. self.reset()
  1146. raise
  1147. def immediate_execute_command(self, *args, **options):
  1148. """
  1149. Execute a command immediately, but don't auto-retry on a
  1150. ConnectionError if we're already WATCHing a variable. Used when
  1151. issuing WATCH or subsequent commands retrieving their values but before
  1152. MULTI is called.
  1153. """
  1154. command_name = args[0]
  1155. conn = self.connection
  1156. # if this is the first call, we need a connection
  1157. if not conn:
  1158. conn = self.connection_pool.get_connection(command_name, self.shard_hint)
  1159. self.connection = conn
  1160. return conn.retry.call_with_retry(
  1161. lambda: self._send_command_parse_response(
  1162. conn, command_name, *args, **options
  1163. ),
  1164. lambda error: self._disconnect_reset_raise(conn, error),
  1165. )
  1166. def pipeline_execute_command(self, *args, **options) -> "Pipeline":
  1167. """
  1168. Stage a command to be executed when execute() is next called
  1169. Returns the current Pipeline object back so commands can be
  1170. chained together, such as:
  1171. pipe = pipe.set('foo', 'bar').incr('baz').decr('bang')
  1172. At some other point, you can then run: pipe.execute(),
  1173. which will execute all commands queued in the pipe.
  1174. """
  1175. self.command_stack.append((args, options))
  1176. return self
  1177. def _execute_transaction(self, connection, commands, raise_on_error) -> List:
  1178. cmds = chain([(("MULTI",), {})], commands, [(("EXEC",), {})])
  1179. all_cmds = connection.pack_commands(
  1180. [args for args, options in cmds if EMPTY_RESPONSE not in options]
  1181. )
  1182. connection.send_packed_command(all_cmds)
  1183. errors = []
  1184. # parse off the response for MULTI
  1185. # NOTE: we need to handle ResponseErrors here and continue
  1186. # so that we read all the additional command messages from
  1187. # the socket
  1188. try:
  1189. self.parse_response(connection, "_")
  1190. except ResponseError as e:
  1191. errors.append((0, e))
  1192. # and all the other commands
  1193. for i, command in enumerate(commands):
  1194. if EMPTY_RESPONSE in command[1]:
  1195. errors.append((i, command[1][EMPTY_RESPONSE]))
  1196. else:
  1197. try:
  1198. self.parse_response(connection, "_")
  1199. except ResponseError as e:
  1200. self.annotate_exception(e, i + 1, command[0])
  1201. errors.append((i, e))
  1202. # parse the EXEC.
  1203. try:
  1204. response = self.parse_response(connection, "_")
  1205. except ExecAbortError:
  1206. if errors:
  1207. raise errors[0][1]
  1208. raise
  1209. # EXEC clears any watched keys
  1210. self.watching = False
  1211. if response is None:
  1212. raise WatchError("Watched variable changed.")
  1213. # put any parse errors into the response
  1214. for i, e in errors:
  1215. response.insert(i, e)
  1216. if len(response) != len(commands):
  1217. self.connection.disconnect()
  1218. raise ResponseError(
  1219. "Wrong number of response items from pipeline execution"
  1220. )
  1221. # find any errors in the response and raise if necessary
  1222. if raise_on_error:
  1223. self.raise_first_error(commands, response)
  1224. # We have to run response callbacks manually
  1225. data = []
  1226. for r, cmd in zip(response, commands):
  1227. if not isinstance(r, Exception):
  1228. args, options = cmd
  1229. command_name = args[0]
  1230. if command_name in self.response_callbacks:
  1231. r = self.response_callbacks[command_name](r, **options)
  1232. data.append(r)
  1233. return data
  1234. def _execute_pipeline(self, connection, commands, raise_on_error):
  1235. # build up all commands into a single request to increase network perf
  1236. all_cmds = connection.pack_commands([args for args, _ in commands])
  1237. connection.send_packed_command(all_cmds)
  1238. response = []
  1239. for args, options in commands:
  1240. try:
  1241. response.append(self.parse_response(connection, args[0], **options))
  1242. except ResponseError as e:
  1243. response.append(e)
  1244. if raise_on_error:
  1245. self.raise_first_error(commands, response)
  1246. return response
  1247. def raise_first_error(self, commands, response):
  1248. for i, r in enumerate(response):
  1249. if isinstance(r, ResponseError):
  1250. self.annotate_exception(r, i + 1, commands[i][0])
  1251. raise r
  1252. def annotate_exception(self, exception, number, command):
  1253. cmd = " ".join(map(safe_str, command))
  1254. msg = (
  1255. f"Command # {number} ({cmd}) of pipeline "
  1256. f"caused error: {exception.args[0]}"
  1257. )
  1258. exception.args = (msg,) + exception.args[1:]
  1259. def parse_response(self, connection, command_name, **options):
  1260. result = Redis.parse_response(self, connection, command_name, **options)
  1261. if command_name in self.UNWATCH_COMMANDS:
  1262. self.watching = False
  1263. elif command_name == "WATCH":
  1264. self.watching = True
  1265. return result
  1266. def load_scripts(self):
  1267. # make sure all scripts that are about to be run on this pipeline exist
  1268. scripts = list(self.scripts)
  1269. immediate = self.immediate_execute_command
  1270. shas = [s.sha for s in scripts]
  1271. # we can't use the normal script_* methods because they would just
  1272. # get buffered in the pipeline.
  1273. exists = immediate("SCRIPT EXISTS", *shas)
  1274. if not all(exists):
  1275. for s, exist in zip(scripts, exists):
  1276. if not exist:
  1277. s.sha = immediate("SCRIPT LOAD", s.script)
  1278. def _disconnect_raise_reset(
  1279. self,
  1280. conn: AbstractConnection,
  1281. error: Exception,
  1282. ) -> None:
  1283. """
  1284. Close the connection, raise an exception if we were watching,
  1285. and raise an exception if retry_on_error is not set or the
  1286. error is not one of the specified error types.
  1287. """
  1288. conn.disconnect()
  1289. # if we were watching a variable, the watch is no longer valid
  1290. # since this connection has died. raise a WatchError, which
  1291. # indicates the user should retry this transaction.
  1292. if self.watching:
  1293. raise WatchError(
  1294. "A ConnectionError occurred on while watching one or more keys"
  1295. )
  1296. # if retry_on_error is not set or the error is not one
  1297. # of the specified error types, raise it
  1298. if (
  1299. conn.retry_on_error is None
  1300. or isinstance(error, tuple(conn.retry_on_error)) is False
  1301. ):
  1302. self.reset()
  1303. raise error
  1304. def execute(self, raise_on_error=True):
  1305. """Execute all the commands in the current pipeline"""
  1306. stack = self.command_stack
  1307. if not stack and not self.watching:
  1308. return []
  1309. if self.scripts:
  1310. self.load_scripts()
  1311. if self.transaction or self.explicit_transaction:
  1312. execute = self._execute_transaction
  1313. else:
  1314. execute = self._execute_pipeline
  1315. conn = self.connection
  1316. if not conn:
  1317. conn = self.connection_pool.get_connection("MULTI", self.shard_hint)
  1318. # assign to self.connection so reset() releases the connection
  1319. # back to the pool after we're done
  1320. self.connection = conn
  1321. try:
  1322. return conn.retry.call_with_retry(
  1323. lambda: execute(conn, stack, raise_on_error),
  1324. lambda error: self._disconnect_raise_reset(conn, error),
  1325. )
  1326. finally:
  1327. self.reset()
  1328. def discard(self):
  1329. """
  1330. Flushes all previously queued commands
  1331. See: https://redis.io/commands/DISCARD
  1332. """
  1333. self.execute_command("DISCARD")
  1334. def watch(self, *names):
  1335. """Watches the values at keys ``names``"""
  1336. if self.explicit_transaction:
  1337. raise RedisError("Cannot issue a WATCH after a MULTI")
  1338. return self.execute_command("WATCH", *names)
  1339. def unwatch(self) -> bool:
  1340. """Unwatches all previously specified keys"""
  1341. return self.watching and self.execute_command("UNWATCH") or True