connection.py 49 KB

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