图片解析应用
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.

1324 lines
50 KiB

  1. import copy
  2. import os
  3. import socket
  4. import ssl
  5. import sys
  6. import threading
  7. import weakref
  8. from abc import abstractmethod
  9. from itertools import chain
  10. from queue import Empty, Full, LifoQueue
  11. from time import time
  12. from typing import Any, Callable, List, Optional, Type, Union
  13. from urllib.parse import parse_qs, unquote, urlparse
  14. from ._parsers import Encoder, _HiredisParser, _RESP2Parser, _RESP3Parser
  15. from .backoff import NoBackoff
  16. from .credentials import CredentialProvider, UsernamePasswordCredentialProvider
  17. from .exceptions import (
  18. AuthenticationError,
  19. AuthenticationWrongNumberOfArgsError,
  20. ChildDeadlockedError,
  21. ConnectionError,
  22. DataError,
  23. RedisError,
  24. ResponseError,
  25. TimeoutError,
  26. )
  27. from .retry import Retry
  28. from .utils import (
  29. CRYPTOGRAPHY_AVAILABLE,
  30. HIREDIS_AVAILABLE,
  31. HIREDIS_PACK_AVAILABLE,
  32. SSL_AVAILABLE,
  33. format_error_message,
  34. get_lib_version,
  35. str_if_bytes,
  36. )
  37. if HIREDIS_AVAILABLE:
  38. import hiredis
  39. SYM_STAR = b"*"
  40. SYM_DOLLAR = b"$"
  41. SYM_CRLF = b"\r\n"
  42. SYM_EMPTY = b""
  43. DEFAULT_RESP_VERSION = 2
  44. SENTINEL = object()
  45. DefaultParser: Type[Union[_RESP2Parser, _RESP3Parser, _HiredisParser]]
  46. if HIREDIS_AVAILABLE:
  47. DefaultParser = _HiredisParser
  48. else:
  49. DefaultParser = _RESP2Parser
  50. class HiredisRespSerializer:
  51. def pack(self, *args: List):
  52. """Pack a series of arguments into the Redis protocol"""
  53. output = []
  54. if isinstance(args[0], str):
  55. args = tuple(args[0].encode().split()) + args[1:]
  56. elif b" " in args[0]:
  57. args = tuple(args[0].split()) + args[1:]
  58. try:
  59. output.append(hiredis.pack_command(args))
  60. except TypeError:
  61. _, value, traceback = sys.exc_info()
  62. raise DataError(value).with_traceback(traceback)
  63. return output
  64. class PythonRespSerializer:
  65. def __init__(self, buffer_cutoff, encode) -> None:
  66. self._buffer_cutoff = buffer_cutoff
  67. self.encode = encode
  68. def pack(self, *args):
  69. """Pack a series of arguments into the Redis protocol"""
  70. output = []
  71. # the client might have included 1 or more literal arguments in
  72. # the command name, e.g., 'CONFIG GET'. The Redis server expects these
  73. # arguments to be sent separately, so split the first argument
  74. # manually. These arguments should be bytestrings so that they are
  75. # not encoded.
  76. if isinstance(args[0], str):
  77. args = tuple(args[0].encode().split()) + args[1:]
  78. elif b" " in args[0]:
  79. args = tuple(args[0].split()) + args[1:]
  80. buff = SYM_EMPTY.join((SYM_STAR, str(len(args)).encode(), SYM_CRLF))
  81. buffer_cutoff = self._buffer_cutoff
  82. for arg in map(self.encode, args):
  83. # to avoid large string mallocs, chunk the command into the
  84. # output list if we're sending large values or memoryviews
  85. arg_length = len(arg)
  86. if (
  87. len(buff) > buffer_cutoff
  88. or arg_length > buffer_cutoff
  89. or isinstance(arg, memoryview)
  90. ):
  91. buff = SYM_EMPTY.join(
  92. (buff, SYM_DOLLAR, str(arg_length).encode(), SYM_CRLF)
  93. )
  94. output.append(buff)
  95. output.append(arg)
  96. buff = SYM_CRLF
  97. else:
  98. buff = SYM_EMPTY.join(
  99. (
  100. buff,
  101. SYM_DOLLAR,
  102. str(arg_length).encode(),
  103. SYM_CRLF,
  104. arg,
  105. SYM_CRLF,
  106. )
  107. )
  108. output.append(buff)
  109. return output
  110. class AbstractConnection:
  111. "Manages communication to and from a Redis server"
  112. def __init__(
  113. self,
  114. db: int = 0,
  115. password: Optional[str] = None,
  116. socket_timeout: Optional[float] = None,
  117. socket_connect_timeout: Optional[float] = None,
  118. retry_on_timeout: bool = False,
  119. retry_on_error=SENTINEL,
  120. encoding: str = "utf-8",
  121. encoding_errors: str = "strict",
  122. decode_responses: bool = False,
  123. parser_class=DefaultParser,
  124. socket_read_size: int = 65536,
  125. health_check_interval: int = 0,
  126. client_name: Optional[str] = None,
  127. lib_name: Optional[str] = "redis-py",
  128. lib_version: Optional[str] = get_lib_version(),
  129. username: Optional[str] = None,
  130. retry: Union[Any, None] = None,
  131. redis_connect_func: Optional[Callable[[], None]] = None,
  132. credential_provider: Optional[CredentialProvider] = None,
  133. protocol: Optional[int] = 2,
  134. command_packer: Optional[Callable[[], None]] = None,
  135. ):
  136. """
  137. Initialize a new Connection.
  138. To specify a retry policy for specific errors, first set
  139. `retry_on_error` to a list of the error/s to retry on, then set
  140. `retry` to a valid `Retry` object.
  141. To retry on TimeoutError, `retry_on_timeout` can also be set to `True`.
  142. """
  143. if (username or password) and credential_provider is not None:
  144. raise DataError(
  145. "'username' and 'password' cannot be passed along with 'credential_"
  146. "provider'. Please provide only one of the following arguments: \n"
  147. "1. 'password' and (optional) 'username'\n"
  148. "2. 'credential_provider'"
  149. )
  150. self.pid = os.getpid()
  151. self.db = db
  152. self.client_name = client_name
  153. self.lib_name = lib_name
  154. self.lib_version = lib_version
  155. self.credential_provider = credential_provider
  156. self.password = password
  157. self.username = username
  158. self.socket_timeout = socket_timeout
  159. if socket_connect_timeout is None:
  160. socket_connect_timeout = socket_timeout
  161. self.socket_connect_timeout = socket_connect_timeout
  162. self.retry_on_timeout = retry_on_timeout
  163. if retry_on_error is SENTINEL:
  164. retry_on_error = []
  165. if retry_on_timeout:
  166. # Add TimeoutError to the errors list to retry on
  167. retry_on_error.append(TimeoutError)
  168. self.retry_on_error = retry_on_error
  169. if retry or retry_on_error:
  170. if retry is None:
  171. self.retry = Retry(NoBackoff(), 1)
  172. else:
  173. # deep-copy the Retry object as it is mutable
  174. self.retry = copy.deepcopy(retry)
  175. # Update the retry's supported errors with the specified errors
  176. self.retry.update_supported_errors(retry_on_error)
  177. else:
  178. self.retry = Retry(NoBackoff(), 0)
  179. self.health_check_interval = health_check_interval
  180. self.next_health_check = 0
  181. self.redis_connect_func = redis_connect_func
  182. self.encoder = Encoder(encoding, encoding_errors, decode_responses)
  183. self._sock = None
  184. self._socket_read_size = socket_read_size
  185. self.set_parser(parser_class)
  186. self._connect_callbacks = []
  187. self._buffer_cutoff = 6000
  188. try:
  189. p = int(protocol)
  190. except TypeError:
  191. p = DEFAULT_RESP_VERSION
  192. except ValueError:
  193. raise ConnectionError("protocol must be an integer")
  194. finally:
  195. if p < 2 or p > 3:
  196. raise ConnectionError("protocol must be either 2 or 3")
  197. # p = DEFAULT_RESP_VERSION
  198. self.protocol = p
  199. self._command_packer = self._construct_command_packer(command_packer)
  200. def __repr__(self):
  201. repr_args = ",".join([f"{k}={v}" for k, v in self.repr_pieces()])
  202. return f"<{self.__class__.__module__}.{self.__class__.__name__}({repr_args})>"
  203. @abstractmethod
  204. def repr_pieces(self):
  205. pass
  206. def __del__(self):
  207. try:
  208. self.disconnect()
  209. except Exception:
  210. pass
  211. def _construct_command_packer(self, packer):
  212. if packer is not None:
  213. return packer
  214. elif HIREDIS_PACK_AVAILABLE:
  215. return HiredisRespSerializer()
  216. else:
  217. return PythonRespSerializer(self._buffer_cutoff, self.encoder.encode)
  218. def register_connect_callback(self, callback):
  219. """
  220. Register a callback to be called when the connection is established either
  221. initially or reconnected. This allows listeners to issue commands that
  222. are ephemeral to the connection, for example pub/sub subscription or
  223. key tracking. The callback must be a _method_ and will be kept as
  224. a weak reference.
  225. """
  226. wm = weakref.WeakMethod(callback)
  227. if wm not in self._connect_callbacks:
  228. self._connect_callbacks.append(wm)
  229. def deregister_connect_callback(self, callback):
  230. """
  231. De-register a previously registered callback. It will no-longer receive
  232. notifications on connection events. Calling this is not required when the
  233. listener goes away, since the callbacks are kept as weak methods.
  234. """
  235. try:
  236. self._connect_callbacks.remove(weakref.WeakMethod(callback))
  237. except ValueError:
  238. pass
  239. def set_parser(self, parser_class):
  240. """
  241. Creates a new instance of parser_class with socket size:
  242. _socket_read_size and assigns it to the parser for the connection
  243. :param parser_class: The required parser class
  244. """
  245. self._parser = parser_class(socket_read_size=self._socket_read_size)
  246. def connect(self):
  247. "Connects to the Redis server if not already connected"
  248. if self._sock:
  249. return
  250. try:
  251. sock = self.retry.call_with_retry(
  252. lambda: self._connect(), lambda error: self.disconnect(error)
  253. )
  254. except socket.timeout:
  255. raise TimeoutError("Timeout connecting to server")
  256. except OSError as e:
  257. raise ConnectionError(self._error_message(e))
  258. self._sock = sock
  259. try:
  260. if self.redis_connect_func is None:
  261. # Use the default on_connect function
  262. self.on_connect()
  263. else:
  264. # Use the passed function redis_connect_func
  265. self.redis_connect_func(self)
  266. except RedisError:
  267. # clean up after any error in on_connect
  268. self.disconnect()
  269. raise
  270. # run any user callbacks. right now the only internal callback
  271. # is for pubsub channel/pattern resubscription
  272. # first, remove any dead weakrefs
  273. self._connect_callbacks = [ref for ref in self._connect_callbacks if ref()]
  274. for ref in self._connect_callbacks:
  275. callback = ref()
  276. if callback:
  277. callback(self)
  278. @abstractmethod
  279. def _connect(self):
  280. pass
  281. @abstractmethod
  282. def _host_error(self):
  283. pass
  284. def _error_message(self, exception):
  285. return format_error_message(self._host_error(), exception)
  286. def on_connect(self):
  287. "Initialize the connection, authenticate and select a database"
  288. self._parser.on_connect(self)
  289. parser = self._parser
  290. auth_args = None
  291. # if credential provider or username and/or password are set, authenticate
  292. if self.credential_provider or (self.username or self.password):
  293. cred_provider = (
  294. self.credential_provider
  295. or UsernamePasswordCredentialProvider(self.username, self.password)
  296. )
  297. auth_args = cred_provider.get_credentials()
  298. # if resp version is specified and we have auth args,
  299. # we need to send them via HELLO
  300. if auth_args and self.protocol not in [2, "2"]:
  301. if isinstance(self._parser, _RESP2Parser):
  302. self.set_parser(_RESP3Parser)
  303. # update cluster exception classes
  304. self._parser.EXCEPTION_CLASSES = parser.EXCEPTION_CLASSES
  305. self._parser.on_connect(self)
  306. if len(auth_args) == 1:
  307. auth_args = ["default", auth_args[0]]
  308. self.send_command("HELLO", self.protocol, "AUTH", *auth_args)
  309. response = self.read_response()
  310. # if response.get(b"proto") != self.protocol and response.get(
  311. # "proto"
  312. # ) != self.protocol:
  313. # raise ConnectionError("Invalid RESP version")
  314. elif auth_args:
  315. # avoid checking health here -- PING will fail if we try
  316. # to check the health prior to the AUTH
  317. self.send_command("AUTH", *auth_args, check_health=False)
  318. try:
  319. auth_response = self.read_response()
  320. except AuthenticationWrongNumberOfArgsError:
  321. # a username and password were specified but the Redis
  322. # server seems to be < 6.0.0 which expects a single password
  323. # arg. retry auth with just the password.
  324. # https://github.com/andymccurdy/redis-py/issues/1274
  325. self.send_command("AUTH", auth_args[-1], check_health=False)
  326. auth_response = self.read_response()
  327. if str_if_bytes(auth_response) != "OK":
  328. raise AuthenticationError("Invalid Username or Password")
  329. # if resp version is specified, switch to it
  330. elif self.protocol not in [2, "2"]:
  331. if isinstance(self._parser, _RESP2Parser):
  332. self.set_parser(_RESP3Parser)
  333. # update cluster exception classes
  334. self._parser.EXCEPTION_CLASSES = parser.EXCEPTION_CLASSES
  335. self._parser.on_connect(self)
  336. self.send_command("HELLO", self.protocol)
  337. response = self.read_response()
  338. if (
  339. response.get(b"proto") != self.protocol
  340. and response.get("proto") != self.protocol
  341. ):
  342. raise ConnectionError("Invalid RESP version")
  343. # if a client_name is given, set it
  344. if self.client_name:
  345. self.send_command("CLIENT", "SETNAME", self.client_name)
  346. if str_if_bytes(self.read_response()) != "OK":
  347. raise ConnectionError("Error setting client name")
  348. try:
  349. # set the library name and version
  350. if self.lib_name:
  351. self.send_command("CLIENT", "SETINFO", "LIB-NAME", self.lib_name)
  352. self.read_response()
  353. except ResponseError:
  354. pass
  355. try:
  356. if self.lib_version:
  357. self.send_command("CLIENT", "SETINFO", "LIB-VER", self.lib_version)
  358. self.read_response()
  359. except ResponseError:
  360. pass
  361. # if a database is specified, switch to it
  362. if self.db:
  363. self.send_command("SELECT", self.db)
  364. if str_if_bytes(self.read_response()) != "OK":
  365. raise ConnectionError("Invalid Database")
  366. def disconnect(self, *args):
  367. "Disconnects from the Redis server"
  368. self._parser.on_disconnect()
  369. conn_sock = self._sock
  370. self._sock = None
  371. if conn_sock is None:
  372. return
  373. if os.getpid() == self.pid:
  374. try:
  375. conn_sock.shutdown(socket.SHUT_RDWR)
  376. except (OSError, TypeError):
  377. pass
  378. try:
  379. conn_sock.close()
  380. except OSError:
  381. pass
  382. def _send_ping(self):
  383. """Send PING, expect PONG in return"""
  384. self.send_command("PING", check_health=False)
  385. if str_if_bytes(self.read_response()) != "PONG":
  386. raise ConnectionError("Bad response from PING health check")
  387. def _ping_failed(self, error):
  388. """Function to call when PING fails"""
  389. self.disconnect()
  390. def check_health(self):
  391. """Check the health of the connection with a PING/PONG"""
  392. if self.health_check_interval and time() > self.next_health_check:
  393. self.retry.call_with_retry(self._send_ping, self._ping_failed)
  394. def send_packed_command(self, command, check_health=True):
  395. """Send an already packed command to the Redis server"""
  396. if not self._sock:
  397. self.connect()
  398. # guard against health check recursion
  399. if check_health:
  400. self.check_health()
  401. try:
  402. if isinstance(command, str):
  403. command = [command]
  404. for item in command:
  405. self._sock.sendall(item)
  406. except socket.timeout:
  407. self.disconnect()
  408. raise TimeoutError("Timeout writing to socket")
  409. except OSError as e:
  410. self.disconnect()
  411. if len(e.args) == 1:
  412. errno, errmsg = "UNKNOWN", e.args[0]
  413. else:
  414. errno = e.args[0]
  415. errmsg = e.args[1]
  416. raise ConnectionError(f"Error {errno} while writing to socket. {errmsg}.")
  417. except BaseException:
  418. # BaseExceptions can be raised when a socket send operation is not
  419. # finished, e.g. due to a timeout. Ideally, a caller could then re-try
  420. # to send un-sent data. However, the send_packed_command() API
  421. # does not support it so there is no point in keeping the connection open.
  422. self.disconnect()
  423. raise
  424. def send_command(self, *args, **kwargs):
  425. """Pack and send a command to the Redis server"""
  426. self.send_packed_command(
  427. self._command_packer.pack(*args),
  428. check_health=kwargs.get("check_health", True),
  429. )
  430. def can_read(self, timeout=0):
  431. """Poll the socket to see if there's data that can be read."""
  432. sock = self._sock
  433. if not sock:
  434. self.connect()
  435. host_error = self._host_error()
  436. try:
  437. return self._parser.can_read(timeout)
  438. except OSError as e:
  439. self.disconnect()
  440. raise ConnectionError(f"Error while reading from {host_error}: {e.args}")
  441. def read_response(
  442. self,
  443. disable_decoding=False,
  444. *,
  445. disconnect_on_error=True,
  446. push_request=False,
  447. ):
  448. """Read the response from a previously sent command"""
  449. host_error = self._host_error()
  450. try:
  451. if self.protocol in ["3", 3] and not HIREDIS_AVAILABLE:
  452. response = self._parser.read_response(
  453. disable_decoding=disable_decoding, push_request=push_request
  454. )
  455. else:
  456. response = self._parser.read_response(disable_decoding=disable_decoding)
  457. except socket.timeout:
  458. if disconnect_on_error:
  459. self.disconnect()
  460. raise TimeoutError(f"Timeout reading from {host_error}")
  461. except OSError as e:
  462. if disconnect_on_error:
  463. self.disconnect()
  464. raise ConnectionError(
  465. f"Error while reading from {host_error}" f" : {e.args}"
  466. )
  467. except BaseException:
  468. # Also by default close in case of BaseException. A lot of code
  469. # relies on this behaviour when doing Command/Response pairs.
  470. # See #1128.
  471. if disconnect_on_error:
  472. self.disconnect()
  473. raise
  474. if self.health_check_interval:
  475. self.next_health_check = time() + self.health_check_interval
  476. if isinstance(response, ResponseError):
  477. try:
  478. raise response
  479. finally:
  480. del response # avoid creating ref cycles
  481. return response
  482. def pack_command(self, *args):
  483. """Pack a series of arguments into the Redis protocol"""
  484. return self._command_packer.pack(*args)
  485. def pack_commands(self, commands):
  486. """Pack multiple commands into the Redis protocol"""
  487. output = []
  488. pieces = []
  489. buffer_length = 0
  490. buffer_cutoff = self._buffer_cutoff
  491. for cmd in commands:
  492. for chunk in self._command_packer.pack(*cmd):
  493. chunklen = len(chunk)
  494. if (
  495. buffer_length > buffer_cutoff
  496. or chunklen > buffer_cutoff
  497. or isinstance(chunk, memoryview)
  498. ):
  499. if pieces:
  500. output.append(SYM_EMPTY.join(pieces))
  501. buffer_length = 0
  502. pieces = []
  503. if chunklen > buffer_cutoff or isinstance(chunk, memoryview):
  504. output.append(chunk)
  505. else:
  506. pieces.append(chunk)
  507. buffer_length += chunklen
  508. if pieces:
  509. output.append(SYM_EMPTY.join(pieces))
  510. return output
  511. class Connection(AbstractConnection):
  512. "Manages TCP communication to and from a Redis server"
  513. def __init__(
  514. self,
  515. host="localhost",
  516. port=6379,
  517. socket_keepalive=False,
  518. socket_keepalive_options=None,
  519. socket_type=0,
  520. **kwargs,
  521. ):
  522. self.host = host
  523. self.port = int(port)
  524. self.socket_keepalive = socket_keepalive
  525. self.socket_keepalive_options = socket_keepalive_options or {}
  526. self.socket_type = socket_type
  527. super().__init__(**kwargs)
  528. def repr_pieces(self):
  529. pieces = [("host", self.host), ("port", self.port), ("db", self.db)]
  530. if self.client_name:
  531. pieces.append(("client_name", self.client_name))
  532. return pieces
  533. def _connect(self):
  534. "Create a TCP socket connection"
  535. # we want to mimic what socket.create_connection does to support
  536. # ipv4/ipv6, but we want to set options prior to calling
  537. # socket.connect()
  538. err = None
  539. for res in socket.getaddrinfo(
  540. self.host, self.port, self.socket_type, socket.SOCK_STREAM
  541. ):
  542. family, socktype, proto, canonname, socket_address = res
  543. sock = None
  544. try:
  545. sock = socket.socket(family, socktype, proto)
  546. # TCP_NODELAY
  547. sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
  548. # TCP_KEEPALIVE
  549. if self.socket_keepalive:
  550. sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
  551. for k, v in self.socket_keepalive_options.items():
  552. sock.setsockopt(socket.IPPROTO_TCP, k, v)
  553. # set the socket_connect_timeout before we connect
  554. sock.settimeout(self.socket_connect_timeout)
  555. # connect
  556. sock.connect(socket_address)
  557. # set the socket_timeout now that we're connected
  558. sock.settimeout(self.socket_timeout)
  559. return sock
  560. except OSError as _:
  561. err = _
  562. if sock is not None:
  563. sock.close()
  564. if err is not None:
  565. raise err
  566. raise OSError("socket.getaddrinfo returned an empty list")
  567. def _host_error(self):
  568. return f"{self.host}:{self.port}"
  569. class SSLConnection(Connection):
  570. """Manages SSL connections to and from the Redis server(s).
  571. This class extends the Connection class, adding SSL functionality, and making
  572. use of ssl.SSLContext (https://docs.python.org/3/library/ssl.html#ssl.SSLContext)
  573. """ # noqa
  574. def __init__(
  575. self,
  576. ssl_keyfile=None,
  577. ssl_certfile=None,
  578. ssl_cert_reqs="required",
  579. ssl_ca_certs=None,
  580. ssl_ca_data=None,
  581. ssl_check_hostname=False,
  582. ssl_ca_path=None,
  583. ssl_password=None,
  584. ssl_validate_ocsp=False,
  585. ssl_validate_ocsp_stapled=False,
  586. ssl_ocsp_context=None,
  587. ssl_ocsp_expected_cert=None,
  588. ssl_min_version=None,
  589. ssl_ciphers=None,
  590. **kwargs,
  591. ):
  592. """Constructor
  593. Args:
  594. ssl_keyfile: Path to an ssl private key. Defaults to None.
  595. ssl_certfile: Path to an ssl certificate. Defaults to None.
  596. ssl_cert_reqs: The string value for the SSLContext.verify_mode (none, optional, required). Defaults to "required".
  597. ssl_ca_certs: The path to a file of concatenated CA certificates in PEM format. Defaults to None.
  598. ssl_ca_data: Either an ASCII string of one or more PEM-encoded certificates or a bytes-like object of DER-encoded certificates.
  599. ssl_check_hostname: If set, match the hostname during the SSL handshake. Defaults to False.
  600. ssl_ca_path: The path to a directory containing several CA certificates in PEM format. Defaults to None.
  601. ssl_password: Password for unlocking an encrypted private key. Defaults to None.
  602. ssl_validate_ocsp: If set, perform a full ocsp validation (i.e not a stapled verification)
  603. ssl_validate_ocsp_stapled: If set, perform a validation on a stapled ocsp response
  604. ssl_ocsp_context: A fully initialized OpenSSL.SSL.Context object to be used in verifying the ssl_ocsp_expected_cert
  605. ssl_ocsp_expected_cert: A PEM armoured string containing the expected certificate to be returned from the ocsp verification service.
  606. ssl_min_version: The lowest supported SSL version. It affects the supported SSL versions of the SSLContext. None leaves the default provided by ssl module.
  607. ssl_ciphers: A string listing the ciphers that are allowed to be used. Defaults to None, which means that the default ciphers are used. See https://docs.python.org/3/library/ssl.html#ssl.SSLContext.set_ciphers for more information.
  608. Raises:
  609. RedisError
  610. """ # noqa
  611. if not SSL_AVAILABLE:
  612. raise RedisError("Python wasn't built with SSL support")
  613. self.keyfile = ssl_keyfile
  614. self.certfile = ssl_certfile
  615. if ssl_cert_reqs is None:
  616. ssl_cert_reqs = ssl.CERT_NONE
  617. elif isinstance(ssl_cert_reqs, str):
  618. CERT_REQS = {
  619. "none": ssl.CERT_NONE,
  620. "optional": ssl.CERT_OPTIONAL,
  621. "required": ssl.CERT_REQUIRED,
  622. }
  623. if ssl_cert_reqs not in CERT_REQS:
  624. raise RedisError(
  625. f"Invalid SSL Certificate Requirements Flag: {ssl_cert_reqs}"
  626. )
  627. ssl_cert_reqs = CERT_REQS[ssl_cert_reqs]
  628. self.cert_reqs = ssl_cert_reqs
  629. self.ca_certs = ssl_ca_certs
  630. self.ca_data = ssl_ca_data
  631. self.ca_path = ssl_ca_path
  632. self.check_hostname = ssl_check_hostname
  633. self.certificate_password = ssl_password
  634. self.ssl_validate_ocsp = ssl_validate_ocsp
  635. self.ssl_validate_ocsp_stapled = ssl_validate_ocsp_stapled
  636. self.ssl_ocsp_context = ssl_ocsp_context
  637. self.ssl_ocsp_expected_cert = ssl_ocsp_expected_cert
  638. self.ssl_min_version = ssl_min_version
  639. self.ssl_ciphers = ssl_ciphers
  640. super().__init__(**kwargs)
  641. def _connect(self):
  642. "Wrap the socket with SSL support"
  643. sock = super()._connect()
  644. context = ssl.create_default_context()
  645. context.check_hostname = self.check_hostname
  646. context.verify_mode = self.cert_reqs
  647. if self.certfile or self.keyfile:
  648. context.load_cert_chain(
  649. certfile=self.certfile,
  650. keyfile=self.keyfile,
  651. password=self.certificate_password,
  652. )
  653. if (
  654. self.ca_certs is not None
  655. or self.ca_path is not None
  656. or self.ca_data is not None
  657. ):
  658. context.load_verify_locations(
  659. cafile=self.ca_certs, capath=self.ca_path, cadata=self.ca_data
  660. )
  661. if self.ssl_min_version is not None:
  662. context.minimum_version = self.ssl_min_version
  663. if self.ssl_ciphers:
  664. context.set_ciphers(self.ssl_ciphers)
  665. sslsock = context.wrap_socket(sock, server_hostname=self.host)
  666. if self.ssl_validate_ocsp is True and CRYPTOGRAPHY_AVAILABLE is False:
  667. raise RedisError("cryptography is not installed.")
  668. if self.ssl_validate_ocsp_stapled and self.ssl_validate_ocsp:
  669. raise RedisError(
  670. "Either an OCSP staple or pure OCSP connection must be validated "
  671. "- not both."
  672. )
  673. # validation for the stapled case
  674. if self.ssl_validate_ocsp_stapled:
  675. import OpenSSL
  676. from .ocsp import ocsp_staple_verifier
  677. # if a context is provided use it - otherwise, a basic context
  678. if self.ssl_ocsp_context is None:
  679. staple_ctx = OpenSSL.SSL.Context(OpenSSL.SSL.SSLv23_METHOD)
  680. staple_ctx.use_certificate_file(self.certfile)
  681. staple_ctx.use_privatekey_file(self.keyfile)
  682. else:
  683. staple_ctx = self.ssl_ocsp_context
  684. staple_ctx.set_ocsp_client_callback(
  685. ocsp_staple_verifier, self.ssl_ocsp_expected_cert
  686. )
  687. # need another socket
  688. con = OpenSSL.SSL.Connection(staple_ctx, socket.socket())
  689. con.request_ocsp()
  690. con.connect((self.host, self.port))
  691. con.do_handshake()
  692. con.shutdown()
  693. return sslsock
  694. # pure ocsp validation
  695. if self.ssl_validate_ocsp is True and CRYPTOGRAPHY_AVAILABLE:
  696. from .ocsp import OCSPVerifier
  697. o = OCSPVerifier(sslsock, self.host, self.port, self.ca_certs)
  698. if o.is_valid():
  699. return sslsock
  700. else:
  701. raise ConnectionError("ocsp validation error")
  702. return sslsock
  703. class UnixDomainSocketConnection(AbstractConnection):
  704. "Manages UDS communication to and from a Redis server"
  705. def __init__(self, path="", socket_timeout=None, **kwargs):
  706. super().__init__(**kwargs)
  707. self.path = path
  708. self.socket_timeout = socket_timeout
  709. def repr_pieces(self):
  710. pieces = [("path", self.path), ("db", self.db)]
  711. if self.client_name:
  712. pieces.append(("client_name", self.client_name))
  713. return pieces
  714. def _connect(self):
  715. "Create a Unix domain socket connection"
  716. sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
  717. sock.settimeout(self.socket_connect_timeout)
  718. sock.connect(self.path)
  719. sock.settimeout(self.socket_timeout)
  720. return sock
  721. def _host_error(self):
  722. return self.path
  723. FALSE_STRINGS = ("0", "F", "FALSE", "N", "NO")
  724. def to_bool(value):
  725. if value is None or value == "":
  726. return None
  727. if isinstance(value, str) and value.upper() in FALSE_STRINGS:
  728. return False
  729. return bool(value)
  730. URL_QUERY_ARGUMENT_PARSERS = {
  731. "db": int,
  732. "socket_timeout": float,
  733. "socket_connect_timeout": float,
  734. "socket_keepalive": to_bool,
  735. "retry_on_timeout": to_bool,
  736. "retry_on_error": list,
  737. "max_connections": int,
  738. "health_check_interval": int,
  739. "ssl_check_hostname": to_bool,
  740. "timeout": float,
  741. }
  742. def parse_url(url):
  743. if not (
  744. url.startswith("redis://")
  745. or url.startswith("rediss://")
  746. or url.startswith("unix://")
  747. ):
  748. raise ValueError(
  749. "Redis URL must specify one of the following "
  750. "schemes (redis://, rediss://, unix://)"
  751. )
  752. url = urlparse(url)
  753. kwargs = {}
  754. for name, value in parse_qs(url.query).items():
  755. if value and len(value) > 0:
  756. value = unquote(value[0])
  757. parser = URL_QUERY_ARGUMENT_PARSERS.get(name)
  758. if parser:
  759. try:
  760. kwargs[name] = parser(value)
  761. except (TypeError, ValueError):
  762. raise ValueError(f"Invalid value for `{name}` in connection URL.")
  763. else:
  764. kwargs[name] = value
  765. if url.username:
  766. kwargs["username"] = unquote(url.username)
  767. if url.password:
  768. kwargs["password"] = unquote(url.password)
  769. # We only support redis://, rediss:// and unix:// schemes.
  770. if url.scheme == "unix":
  771. if url.path:
  772. kwargs["path"] = unquote(url.path)
  773. kwargs["connection_class"] = UnixDomainSocketConnection
  774. else: # implied: url.scheme in ("redis", "rediss"):
  775. if url.hostname:
  776. kwargs["host"] = unquote(url.hostname)
  777. if url.port:
  778. kwargs["port"] = int(url.port)
  779. # If there's a path argument, use it as the db argument if a
  780. # querystring value wasn't specified
  781. if url.path and "db" not in kwargs:
  782. try:
  783. kwargs["db"] = int(unquote(url.path).replace("/", ""))
  784. except (AttributeError, ValueError):
  785. pass
  786. if url.scheme == "rediss":
  787. kwargs["connection_class"] = SSLConnection
  788. return kwargs
  789. class ConnectionPool:
  790. """
  791. Create a connection pool. ``If max_connections`` is set, then this
  792. object raises :py:class:`~redis.exceptions.ConnectionError` when the pool's
  793. limit is reached.
  794. By default, TCP connections are created unless ``connection_class``
  795. is specified. Use class:`.UnixDomainSocketConnection` for
  796. unix sockets.
  797. Any additional keyword arguments are passed to the constructor of
  798. ``connection_class``.
  799. """
  800. @classmethod
  801. def from_url(cls, url, **kwargs):
  802. """
  803. Return a connection pool configured from the given URL.
  804. For example::
  805. redis://[[username]:[password]]@localhost:6379/0
  806. rediss://[[username]:[password]]@localhost:6379/0
  807. unix://[username@]/path/to/socket.sock?db=0[&password=password]
  808. Three URL schemes are supported:
  809. - `redis://` creates a TCP socket connection. See more at:
  810. <https://www.iana.org/assignments/uri-schemes/prov/redis>
  811. - `rediss://` creates a SSL wrapped TCP socket connection. See more at:
  812. <https://www.iana.org/assignments/uri-schemes/prov/rediss>
  813. - ``unix://``: creates a Unix Domain Socket connection.
  814. The username, password, hostname, path and all querystring values
  815. are passed through urllib.parse.unquote in order to replace any
  816. percent-encoded values with their corresponding characters.
  817. There are several ways to specify a database number. The first value
  818. found will be used:
  819. 1. A ``db`` querystring option, e.g. redis://localhost?db=0
  820. 2. If using the redis:// or rediss:// schemes, the path argument
  821. of the url, e.g. redis://localhost/0
  822. 3. A ``db`` keyword argument to this function.
  823. If none of these options are specified, the default db=0 is used.
  824. All querystring options are cast to their appropriate Python types.
  825. Boolean arguments can be specified with string values "True"/"False"
  826. or "Yes"/"No". Values that cannot be properly cast cause a
  827. ``ValueError`` to be raised. Once parsed, the querystring arguments
  828. and keyword arguments are passed to the ``ConnectionPool``'s
  829. class initializer. In the case of conflicting arguments, querystring
  830. arguments always win.
  831. """
  832. url_options = parse_url(url)
  833. if "connection_class" in kwargs:
  834. url_options["connection_class"] = kwargs["connection_class"]
  835. kwargs.update(url_options)
  836. return cls(**kwargs)
  837. def __init__(
  838. self,
  839. connection_class=Connection,
  840. max_connections: Optional[int] = None,
  841. **connection_kwargs,
  842. ):
  843. max_connections = max_connections or 2**31
  844. if not isinstance(max_connections, int) or max_connections < 0:
  845. raise ValueError('"max_connections" must be a positive integer')
  846. self.connection_class = connection_class
  847. self.connection_kwargs = connection_kwargs
  848. self.max_connections = max_connections
  849. # a lock to protect the critical section in _checkpid().
  850. # this lock is acquired when the process id changes, such as
  851. # after a fork. during this time, multiple threads in the child
  852. # process could attempt to acquire this lock. the first thread
  853. # to acquire the lock will reset the data structures and lock
  854. # object of this pool. subsequent threads acquiring this lock
  855. # will notice the first thread already did the work and simply
  856. # release the lock.
  857. self._fork_lock = threading.Lock()
  858. self.reset()
  859. def __repr__(self) -> (str, str):
  860. return (
  861. f"<{type(self).__module__}.{type(self).__name__}"
  862. f"({repr(self.connection_class(**self.connection_kwargs))})>"
  863. )
  864. def reset(self) -> None:
  865. self._lock = threading.Lock()
  866. self._created_connections = 0
  867. self._available_connections = []
  868. self._in_use_connections = set()
  869. # this must be the last operation in this method. while reset() is
  870. # called when holding _fork_lock, other threads in this process
  871. # can call _checkpid() which compares self.pid and os.getpid() without
  872. # holding any lock (for performance reasons). keeping this assignment
  873. # as the last operation ensures that those other threads will also
  874. # notice a pid difference and block waiting for the first thread to
  875. # release _fork_lock. when each of these threads eventually acquire
  876. # _fork_lock, they will notice that another thread already called
  877. # reset() and they will immediately release _fork_lock and continue on.
  878. self.pid = os.getpid()
  879. def _checkpid(self) -> None:
  880. # _checkpid() attempts to keep ConnectionPool fork-safe on modern
  881. # systems. this is called by all ConnectionPool methods that
  882. # manipulate the pool's state such as get_connection() and release().
  883. #
  884. # _checkpid() determines whether the process has forked by comparing
  885. # the current process id to the process id saved on the ConnectionPool
  886. # instance. if these values are the same, _checkpid() simply returns.
  887. #
  888. # when the process ids differ, _checkpid() assumes that the process
  889. # has forked and that we're now running in the child process. the child
  890. # process cannot use the parent's file descriptors (e.g., sockets).
  891. # therefore, when _checkpid() sees the process id change, it calls
  892. # reset() in order to reinitialize the child's ConnectionPool. this
  893. # will cause the child to make all new connection objects.
  894. #
  895. # _checkpid() is protected by self._fork_lock to ensure that multiple
  896. # threads in the child process do not call reset() multiple times.
  897. #
  898. # there is an extremely small chance this could fail in the following
  899. # scenario:
  900. # 1. process A calls _checkpid() for the first time and acquires
  901. # self._fork_lock.
  902. # 2. while holding self._fork_lock, process A forks (the fork()
  903. # could happen in a different thread owned by process A)
  904. # 3. process B (the forked child process) inherits the
  905. # ConnectionPool's state from the parent. that state includes
  906. # a locked _fork_lock. process B will not be notified when
  907. # process A releases the _fork_lock and will thus never be
  908. # able to acquire the _fork_lock.
  909. #
  910. # to mitigate this possible deadlock, _checkpid() will only wait 5
  911. # seconds to acquire _fork_lock. if _fork_lock cannot be acquired in
  912. # that time it is assumed that the child is deadlocked and a
  913. # redis.ChildDeadlockedError error is raised.
  914. if self.pid != os.getpid():
  915. acquired = self._fork_lock.acquire(timeout=5)
  916. if not acquired:
  917. raise ChildDeadlockedError
  918. # reset() the instance for the new process if another thread
  919. # hasn't already done so
  920. try:
  921. if self.pid != os.getpid():
  922. self.reset()
  923. finally:
  924. self._fork_lock.release()
  925. def get_connection(self, command_name: str, *keys, **options) -> "Connection":
  926. "Get a connection from the pool"
  927. self._checkpid()
  928. with self._lock:
  929. try:
  930. connection = self._available_connections.pop()
  931. except IndexError:
  932. connection = self.make_connection()
  933. self._in_use_connections.add(connection)
  934. try:
  935. # ensure this connection is connected to Redis
  936. connection.connect()
  937. # connections that the pool provides should be ready to send
  938. # a command. if not, the connection was either returned to the
  939. # pool before all data has been read or the socket has been
  940. # closed. either way, reconnect and verify everything is good.
  941. try:
  942. if connection.can_read():
  943. raise ConnectionError("Connection has data")
  944. except (ConnectionError, OSError):
  945. connection.disconnect()
  946. connection.connect()
  947. if connection.can_read():
  948. raise ConnectionError("Connection not ready")
  949. except BaseException:
  950. # release the connection back to the pool so that we don't
  951. # leak it
  952. self.release(connection)
  953. raise
  954. return connection
  955. def get_encoder(self) -> Encoder:
  956. "Return an encoder based on encoding settings"
  957. kwargs = self.connection_kwargs
  958. return Encoder(
  959. encoding=kwargs.get("encoding", "utf-8"),
  960. encoding_errors=kwargs.get("encoding_errors", "strict"),
  961. decode_responses=kwargs.get("decode_responses", False),
  962. )
  963. def make_connection(self) -> "Connection":
  964. "Create a new connection"
  965. if self._created_connections >= self.max_connections:
  966. raise ConnectionError("Too many connections")
  967. self._created_connections += 1
  968. return self.connection_class(**self.connection_kwargs)
  969. def release(self, connection: "Connection") -> None:
  970. "Releases the connection back to the pool"
  971. self._checkpid()
  972. with self._lock:
  973. try:
  974. self._in_use_connections.remove(connection)
  975. except KeyError:
  976. # Gracefully fail when a connection is returned to this pool
  977. # that the pool doesn't actually own
  978. pass
  979. if self.owns_connection(connection):
  980. self._available_connections.append(connection)
  981. else:
  982. # pool doesn't own this connection. do not add it back
  983. # to the pool and decrement the count so that another
  984. # connection can take its place if needed
  985. self._created_connections -= 1
  986. connection.disconnect()
  987. return
  988. def owns_connection(self, connection: "Connection") -> int:
  989. return connection.pid == self.pid
  990. def disconnect(self, inuse_connections: bool = True) -> None:
  991. """
  992. Disconnects connections in the pool
  993. If ``inuse_connections`` is True, disconnect connections that are
  994. current in use, potentially by other threads. Otherwise only disconnect
  995. connections that are idle in the pool.
  996. """
  997. self._checkpid()
  998. with self._lock:
  999. if inuse_connections:
  1000. connections = chain(
  1001. self._available_connections, self._in_use_connections
  1002. )
  1003. else:
  1004. connections = self._available_connections
  1005. for connection in connections:
  1006. connection.disconnect()
  1007. def close(self) -> None:
  1008. """Close the pool, disconnecting all connections"""
  1009. self.disconnect()
  1010. def set_retry(self, retry: "Retry") -> None:
  1011. self.connection_kwargs.update({"retry": retry})
  1012. for conn in self._available_connections:
  1013. conn.retry = retry
  1014. for conn in self._in_use_connections:
  1015. conn.retry = retry
  1016. class BlockingConnectionPool(ConnectionPool):
  1017. """
  1018. Thread-safe blocking connection pool::
  1019. >>> from redis.client import Redis
  1020. >>> client = Redis(connection_pool=BlockingConnectionPool())
  1021. It performs the same function as the default
  1022. :py:class:`~redis.ConnectionPool` implementation, in that,
  1023. it maintains a pool of reusable connections that can be shared by
  1024. multiple redis clients (safely across threads if required).
  1025. The difference is that, in the event that a client tries to get a
  1026. connection from the pool when all of connections are in use, rather than
  1027. raising a :py:class:`~redis.ConnectionError` (as the default
  1028. :py:class:`~redis.ConnectionPool` implementation does), it
  1029. makes the client wait ("blocks") for a specified number of seconds until
  1030. a connection becomes available.
  1031. Use ``max_connections`` to increase / decrease the pool size::
  1032. >>> pool = BlockingConnectionPool(max_connections=10)
  1033. Use ``timeout`` to tell it either how many seconds to wait for a connection
  1034. to become available, or to block forever:
  1035. >>> # Block forever.
  1036. >>> pool = BlockingConnectionPool(timeout=None)
  1037. >>> # Raise a ``ConnectionError`` after five seconds if a connection is
  1038. >>> # not available.
  1039. >>> pool = BlockingConnectionPool(timeout=5)
  1040. """
  1041. def __init__(
  1042. self,
  1043. max_connections=50,
  1044. timeout=20,
  1045. connection_class=Connection,
  1046. queue_class=LifoQueue,
  1047. **connection_kwargs,
  1048. ):
  1049. self.queue_class = queue_class
  1050. self.timeout = timeout
  1051. super().__init__(
  1052. connection_class=connection_class,
  1053. max_connections=max_connections,
  1054. **connection_kwargs,
  1055. )
  1056. def reset(self):
  1057. # Create and fill up a thread safe queue with ``None`` values.
  1058. self.pool = self.queue_class(self.max_connections)
  1059. while True:
  1060. try:
  1061. self.pool.put_nowait(None)
  1062. except Full:
  1063. break
  1064. # Keep a list of actual connection instances so that we can
  1065. # disconnect them later.
  1066. self._connections = []
  1067. # this must be the last operation in this method. while reset() is
  1068. # called when holding _fork_lock, other threads in this process
  1069. # can call _checkpid() which compares self.pid and os.getpid() without
  1070. # holding any lock (for performance reasons). keeping this assignment
  1071. # as the last operation ensures that those other threads will also
  1072. # notice a pid difference and block waiting for the first thread to
  1073. # release _fork_lock. when each of these threads eventually acquire
  1074. # _fork_lock, they will notice that another thread already called
  1075. # reset() and they will immediately release _fork_lock and continue on.
  1076. self.pid = os.getpid()
  1077. def make_connection(self):
  1078. "Make a fresh connection."
  1079. connection = self.connection_class(**self.connection_kwargs)
  1080. self._connections.append(connection)
  1081. return connection
  1082. def get_connection(self, command_name, *keys, **options):
  1083. """
  1084. Get a connection, blocking for ``self.timeout`` until a connection
  1085. is available from the pool.
  1086. If the connection returned is ``None`` then creates a new connection.
  1087. Because we use a last-in first-out queue, the existing connections
  1088. (having been returned to the pool after the initial ``None`` values
  1089. were added) will be returned before ``None`` values. This means we only
  1090. create new connections when we need to, i.e.: the actual number of
  1091. connections will only increase in response to demand.
  1092. """
  1093. # Make sure we haven't changed process.
  1094. self._checkpid()
  1095. # Try and get a connection from the pool. If one isn't available within
  1096. # self.timeout then raise a ``ConnectionError``.
  1097. connection = None
  1098. try:
  1099. connection = self.pool.get(block=True, timeout=self.timeout)
  1100. except Empty:
  1101. # Note that this is not caught by the redis client and will be
  1102. # raised unless handled by application code. If you want never to
  1103. raise ConnectionError("No connection available.")
  1104. # If the ``connection`` is actually ``None`` then that's a cue to make
  1105. # a new connection to add to the pool.
  1106. if connection is None:
  1107. connection = self.make_connection()
  1108. try:
  1109. # ensure this connection is connected to Redis
  1110. connection.connect()
  1111. # connections that the pool provides should be ready to send
  1112. # a command. if not, the connection was either returned to the
  1113. # pool before all data has been read or the socket has been
  1114. # closed. either way, reconnect and verify everything is good.
  1115. try:
  1116. if connection.can_read():
  1117. raise ConnectionError("Connection has data")
  1118. except (ConnectionError, OSError):
  1119. connection.disconnect()
  1120. connection.connect()
  1121. if connection.can_read():
  1122. raise ConnectionError("Connection not ready")
  1123. except BaseException:
  1124. # release the connection back to the pool so that we don't leak it
  1125. self.release(connection)
  1126. raise
  1127. return connection
  1128. def release(self, connection):
  1129. "Releases the connection back to the pool."
  1130. # Make sure we haven't changed process.
  1131. self._checkpid()
  1132. if not self.owns_connection(connection):
  1133. # pool doesn't own this connection. do not add it back
  1134. # to the pool. instead add a None value which is a placeholder
  1135. # that will cause the pool to recreate the connection if
  1136. # its needed.
  1137. connection.disconnect()
  1138. self.pool.put_nowait(None)
  1139. return
  1140. # Put the connection back into the pool.
  1141. try:
  1142. self.pool.put_nowait(connection)
  1143. except Full:
  1144. # perhaps the pool has been reset() after a fork? regardless,
  1145. # we don't want this connection
  1146. pass
  1147. def disconnect(self):
  1148. "Disconnects all connections in the pool."
  1149. self._checkpid()
  1150. for connection in self._connections:
  1151. connection.disconnect()