
    fi                     B   U d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlmZ d dl	m
Z
 d dlmZmZmZ d dlmZ d dlmZmZmZmZmZmZmZmZ d dlmZmZmZ d dlmZmZm Z m!Z!m"Z"m#Z# d	d
l$m%Z%m&Z&m'Z'm(Z( d	dl)m*Z* d	dl+m,Z, d	dl-m.Z.m/Z/ d	dl0m1Z1m2Z2 d	dl3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z; d	dl<m=Z= d	dl>m?Z?m@Z@mAZAmBZBmCZCmDZDmEZEmFZFmGZG e@rd dlHZHdZIdZJdZKdZLdZM eN            ZOeee'e(e&f                  ePd<   e@re&ZQne'ZQ G d d          ZR G d d          ZS G d d          ZT G d deT          ZU G d  d!eU          ZV G d" d#eT          ZW G d$ d%eV          ZX G d& d'eU          ZYd(ZZd) Z[e\e]e]e[e[e^e\e\e[e]d*
Z_d+ Z` ed,d-.          Za G d/ d-          Zb G d0 d1eb          ZcdS )2    N)abstractmethod)chain)EmptyFull	LifoQueue)time)AnyCallableDictListOptionalTypeTypeVarUnion)parse_qsunquoteurlparse)
CacheEntryCacheEntryStatusCacheFactoryCacheFactoryInterfaceCacheInterfaceCacheKey   )Encoder_HiredisParser_RESP2Parser_RESP3Parser)TokenInterface)	NoBackoff)CredentialProvider"UsernamePasswordCredentialProvider)AfterConnectionReleasedEventEventDispatcher)AuthenticationError$AuthenticationWrongNumberOfArgsErrorChildDeadlockedErrorConnectionError	DataError
RedisErrorResponseErrorTimeoutError)Retry)	CRYPTOGRAPHY_AVAILABLEHIREDIS_AVAILABLESSL_AVAILABLEcompare_versionsdeprecated_argsensure_stringformat_error_messageget_lib_versionstr_if_bytes   *   $s   
       DefaultParserc                       e Zd ZdefdZdS )HiredisRespSerializerargsc                 "   g }t          |d         t                    rEt          |d                                                                                   |dd         z   }n<d|d         v r2t          |d                                                   |dd         z   }	 |                    t          j        |                     nG# t          $ r: t          j
                    \  }}}t          |                              |          w xY w|S 2Pack a series of arguments into the Redis protocolr   r   N    )
isinstancestrtupleencodesplitappendhiredispack_command	TypeErrorsysexc_infor)   with_traceback)selfr>   output_value	tracebacks         `/var/www/html/Pagina-Ingenieria-Requisitos/venv/lib/python3.11/site-packages/redis/connection.pypackzHiredisRespSerializer.packH   s    d1gs## 	5a))//1122T!""X=DDT!W__a))DH4D	=MM'.t445555 	= 	= 	="%,..AuiE""11)<<<	= s    'C ADN)__name__
__module____qualname__r   rU    r9   rT   r=   r=   G   s/        $      r9   r=   c                       e Zd ZddZd ZdS )PythonRespSerializerreturnNc                 "    || _         || _        d S N)_buffer_cutoffrF   )rO   buffer_cutoffrF   s      rT   __init__zPythonRespSerializer.__init__Z   s    +r9   c           	      \   g }t          |d         t                    rEt          |d                                                                                   |dd         z   }n<d|d         v r2t          |d                                                   |dd         z   }t
                              t          t          t          |                                                    t          f          }| j
        }t          | j        |          D ]}t          |          }t          |          |k    s||k    st          |t                    ryt
                              |t          t          |                                          t          f          }|                    |           |                    |           t          }t
                              |t          t          |                                          t          |t          f          }	|                    |           |S r@   )rC   rD   rE   rF   rG   	SYM_EMPTYjoinSYM_STARlenSYM_CRLFr_   map
memoryview
SYM_DOLLARrH   )rO   r>   rP   buffr`   arg
arg_lengths          rT   rU   zPythonRespSerializer.pack^   s    d1gs## 	5a))//1122T!""X=DDT!W__a))DH4D~~xSYY)>)>)@)@(KLL+t{D)) 	 	C SJD		M))--c:.. . !~~:s:'='='?'?J  d###c""" ~~"J..00  	 	 	dr9   r\   N)rV   rW   rX   ra   rU   rY   r9   rT   r[   r[   Y   s7           + + + + +r9   r[   c                      e Zd Zed             Zed             Zed             Zed             Zed             Zed             Z	ed             Z
ed             Zed	             Zedd            Zed             Zedd            Ze	 dd
ddd            Zed             Zed             Zeedeeeef         eeef         f         fd                        Zedefd            Zed             ZdS )ConnectionInterfacec                     d S r^   rY   rO   s    rT   repr_pieceszConnectionInterface.repr_pieces       r9   c                     d S r^   rY   rO   callbacks     rT   register_connect_callbackz-ConnectionInterface.register_connect_callback   rt   r9   c                     d S r^   rY   rv   s     rT   deregister_connect_callbackz/ConnectionInterface.deregister_connect_callback   rt   r9   c                     d S r^   rY   rO   parser_classs     rT   
set_parserzConnectionInterface.set_parser   rt   r9   c                     d S r^   rY   rr   s    rT   get_protocolz ConnectionInterface.get_protocol   rt   r9   c                     d S r^   rY   rr   s    rT   connectzConnectionInterface.connect   rt   r9   c                     d S r^   rY   rr   s    rT   
on_connectzConnectionInterface.on_connect   rt   r9   c                     d S r^   rY   rO   r>   s     rT   
disconnectzConnectionInterface.disconnect   rt   r9   c                     d S r^   rY   rr   s    rT   check_healthz ConnectionInterface.check_health   rt   r9   Tc                     d S r^   rY   rO   commandr   s      rT   send_packed_commandz'ConnectionInterface.send_packed_command   rt   r9   c                     d S r^   rY   rO   r>   kwargss      rT   send_commandz ConnectionInterface.send_command   rt   r9   r   c                     d S r^   rY   rO   timeouts     rT   can_readzConnectionInterface.can_read   rt   r9   Fdisconnect_on_errorpush_requestc                    d S r^   rY   )rO   disable_decodingr   r   s       rT   read_responsez!ConnectionInterface.read_response   s	     	r9   c                     d S r^   rY   r   s     rT   rJ   z ConnectionInterface.pack_command   rt   r9   c                     d S r^   rY   rO   commandss     rT   pack_commandsz!ConnectionInterface.pack_commands   rt   r9   r\   c                     d S r^   rY   rr   s    rT   handshake_metadataz&ConnectionInterface.handshake_metadata   s	     	r9   tokenc                     d S r^   rY   rO   r   s     rT   set_re_auth_tokenz%ConnectionInterface.set_re_auth_token   rt   r9   c                     d S r^   rY   rr   s    rT   re_authzConnectionInterface.re_auth   rt   r9   NTr   F)rV   rW   rX   r   rs   rx   rz   r~   r   r   r   r   r   r   r   r   r   rJ   r   propertyr   r   bytesrD   r   r   r   r   rY   r9   rT   rp   rp      s[         ^   ^   ^   ^   ^   ^   ^   ^   ^    ^   ^    ^   !    ^   ^   ^ E$ue|*<d38n*L$M    ^ X ~    ^   ^  r9   rp   c            *          e Zd ZdZdddddedddedddd e            ddddd	ddfd
edee	         dee
         dee
         dede	de	dedededee	         dee	         dee	         dee	         deedf         deeg df                  dee         dee         deeg df                  dee         f(dZd Zed              Zd! Zd" Zd# Zd$ Zd% Zd& Zed'             Zed(             Zd) Zd* Zd+ Zd, Z d- Z!d. Z"d?d0Z#d1 Z$d@d2Z%	 dAd/dd3d4Z&d5 Z'd6 Z(d7epe	fd8Z)e*d7ee+e,e,f         e+e	e	f         f         fd9            Z-e-j.        d:ee+e,e,f         e+e	e	f         f         fd;            Z-d<e/fd=Z0d> Z1dS )BAbstractConnectionz0Manages communication to and from a Redis serverr   NFutf-8stricti   zredis-pyr:   dbpasswordsocket_timeoutsocket_connect_timeoutretry_on_timeoutencodingencoding_errorsdecode_responsessocket_read_sizehealth_check_intervalclient_namelib_namelib_versionusernameretryredis_connect_funccredential_providerprotocolcommand_packerevent_dispatcherc                    |s|r|t          d          |t                      | _        n|| _        t          j                    | _        || _        || _        || _        || _	        || _
        || _        || _        || _        ||}|| _        || _        |t           u rg }|r|                    t$                     || _        |s|rX|"t)          t+                      d          | _        nt/          j        |          | _        | j                            |           n!t)          t+                      d          | _        || _        d| _        || _        t;          |||	          | _        d| _        d| _         || _!        | "                    |
           g | _#        d| _$        d| _%        	 tM          |          }n/# tN          $ r
 tP          }Y ntR          $ r tU          d          w xY w|dk     s|dk    rtU          d	          || _+        n'# |dk     s|dk    rtU          d	          || _+        w xY w| ,                    |          | _-        dS )
a2  
        Initialize a new Connection.
        To specify a retry policy for specific errors, first set
        `retry_on_error` to a list of the error/s to retry on, then set
        `retry` to a valid `Retry` object.
        To retry on TimeoutError, `retry_on_timeout` can also be set to `True`.
        Nz'username' and 'password' cannot be passed along with 'credential_provider'. Please provide only one of the following arguments: 
1. 'password' and (optional) 'username'
2. 'credential_provider'r   r   ip  zprotocol must be an integerr:      zprotocol must be either 2 or 3).r)   r$   _event_dispatcherosgetpidpidr   r   r   r   r   r   r   r   r   r   SENTINELrH   r,   retry_on_errorr-   r    r   copydeepcopyupdate_supported_errorsr   next_health_checkr   r   encoderr   _sock_socket_read_sizer~   _connect_callbacksr_   _re_auth_tokenintrK   DEFAULT_RESP_VERSION
ValueErrorr(   r   _construct_command_packer_command_packer)rO   r   r   r   r   r   r   r   r   r   r}   r   r   r   r   r   r   r   r   r   r   r   r   ps                           rT   ra   zAbstractConnection.__init__   s   @  	 	&9&E+   #%4%6%6D""%5D"9;;& &#6   ,!)%3"&<# 0X%%N 	0!!,///, 		/N 		/}"9;;22

 "]511
J..~>>>>y{{A..DJ%:"!""4x:JKK"&
!1%%%"$"8<
	HAA 	% 	% 	%$AAA 	A 	A 	A!"?@@@	A 1uuA%&FGGGDMM 1uuA%&FGGGDM#==nMMs*   +F; :H ;G'H G''H $H1c                     d                     d |                                 D                       }d| j        j         d| j        j         d| dS )N,c                 "    g | ]\  }}| d | S )=rY   ).0kvs      rT   
<listcomp>z/AbstractConnection.__repr__.<locals>.<listcomp>A  s&    HHHTQjjQjjHHHr9   <.()>)rd   rs   	__class__rW   rV   )rO   	repr_argss     rT   __repr__zAbstractConnection.__repr__@  sY    HHHHT5E5E5G5GHHHII	V4>,VVt~/FVVVVVVr9   c                     d S r^   rY   rr   s    rT   rs   zAbstractConnection.repr_piecesD  rt   r9   c                 R    	 |                                   d S # t          $ r Y d S w xY wr^   )r   	Exceptionrr   s    rT   __del__zAbstractConnection.__del__H  s?    	OO 	 	 	DD	s    
&&c                 r    ||S t           rt                      S t          | j        | j        j                  S r^   )r/   r=   r[   r_   r   rF   )rO   packers     rT   r   z,AbstractConnection._construct_command_packerN  s:    M 	R(***'(;T\=PQQQr9   c                 x    t          j        |          }|| j        vr| j                            |           dS dS )a^  
        Register a callback to be called when the connection is established either
        initially or reconnected.  This allows listeners to issue commands that
        are ephemeral to the connection, for example pub/sub subscription or
        key tracking.  The callback must be a _method_ and will be kept as
        a weak reference.
        N)weakref
WeakMethodr   rH   )rO   rw   wms      rT   rx   z,AbstractConnection.register_connect_callbackV  sG     ))T,,,#**2..... -,r9   c                     	 | j                             t          j        |                     dS # t          $ r Y dS w xY w)z
        De-register a previously registered callback.  It will no-longer receive
        notifications on connection events.  Calling this is not required when the
        listener goes away, since the callbacks are kept as weak methods.
        N)r   remover   r   r   rv   s     rT   rz   z.AbstractConnection.deregister_connect_callbackb  sQ    	#**7+=h+G+GHHHHH 	 	 	DD	s   ,0 
>>c                 2     || j                   | _        dS )z
        Creates a new instance of parser_class with socket size:
        _socket_read_size and assigns it to the parser for the connection
        :param parser_class: The required parser class
        )r   N)r   _parserr|   s     rT   r~   zAbstractConnection.set_parserm  s     $|T5KLLLr9   c                 *     j         rdS 	  j                             fd fd          }nQ# t          j        $ r t          d          t          $ r'}t                               |                    d}~ww xY w| _         	  j	         
                                 n 	                                n## t          $ r                                    w xY wd  j        D              _         j        D ]} |            }|r |            dS )z5Connects to the Redis server if not already connectedNc                  ,                                      S r^   )_connectrr   s   rT   <lambda>z,AbstractConnection.connect.<locals>.<lambda>{  s     r9   c                 .                         |           S r^   r   errorrO   s    rT   r   z,AbstractConnection.connect.<locals>.<lambda>{  s    tu7M7M r9   zTimeout connecting to serverc                 &    g | ]} |            |S rY   rY   )r   refs     rT   r   z.AbstractConnection.connect.<locals>.<listcomp>  s#    "S"S"S3SSUU"S3"S"S"Sr9   )r   r   call_with_retrysocketr   r,   OSErrorr(   _error_messager   r   r*   r   r   )rO   socker  rw   s   `    rT   r   zAbstractConnection.connectu  sp   : 	F	::--'''')M)M)M)M DD ~ 	? 	? 	?=>>> 	: 	: 	:!$"5"5a"8"8999	: 

	&.!!!! ''--- 	 	 	OO	 #T"S$2I"S"S"S* 	 	CsuuH 	 	s!   !. 'A<"A77A<1B9 9 Cc                     d S r^   rY   rr   s    rT   r   zAbstractConnection._connect  rt   r9   c                     d S r^   rY   rr   s    rT   _host_errorzAbstractConnection._host_error  rt   r9   c                 F    t          |                                 |          S r^   )r4   r  )rO   	exceptions     rT   r  z!AbstractConnection._error_message  s    #D$4$4$6$6	BBBr9   c                    | j                             |            | j         }d}| j        s| j        s| j        r5| j        pt          | j        | j                  }|                                }|r| j        dvrt          | j         t                    rE| 
                    t                     |j        | j         _        | j                             |            t          |          dk    r
d|d         g} | j        d| j        dg|R   |                                 | _        n|r | j        dg|R dd	i 	 |                                 }nB# t"          $ r5 |                     d|d
         d	           |                                 }Y nw xY wt%          |          dk    rt'          d          n| j        dvrt          | j         t                    rE| 
                    t                     |j        | j         _        | j                             |            |                     d| j                   |                                 | _        | j                            d          | j        k    r2| j                            d          | j        k    rt+          d          | j        rP|                     dd| j                   t%          |                                           dk    rt+          d          	 | j        r1|                     ddd| j                   |                                  n# t0          $ r Y nw xY w	 | j        r1|                     ddd| j                   |                                  n# t0          $ r Y nw xY w| j        rO|                     d| j                   t%          |                                           dk    rt+          d          dS dS )z=Initialize the connection, authenticate and select a databaseN)r:   2r   defaultr   HELLOAUTHr   Fr   OKzInvalid Username or Passwords   protoprotozInvalid RESP versionCLIENTSETNAMEzError setting client nameSETINFOzLIB-NAMEzLIB-VERSELECTzInvalid Database)r   r   r   r   r   r"   get_credentialsr   rC   r   r~   r   EXCEPTION_CLASSESrf   r   r   r   r&   r6   r%   getr(   r   r   r+   r   r   )rO   parser	auth_argscred_providerauth_responses        rT   r   zAbstractConnection.on_connect  s3   %%%	# 	8 	8 	8( T5dmT]SS  &5577I  -	>h66$,55 .---171I.''---9~~""&	!5	Dgt}fIyIIII&*&8&8&:&:D##
  	> DfEyEEEuEEE5 $ 2 2 4 47 5 5 5
 !!&)B-e!LLL $ 2 2 4 45 M**d22)*HIII 3 ](**$,55 .---171I.''---gt}555&*&8&8&:&:D#'++H55FF+//88DMII%&<===  	Ch	43CDDDD..0011T99%&ABBB	} %!!(Iz4=QQQ""$$$ 	 	 	D		 %!!(Iy$BRSSS""$$$ 	 	 	D	 7 	:h000D..0011T99%&8999	: 	:99s6   9E <FF<8L5 5
MM8M? ?
NNc                 P   | j                                          | j        }d| _        |dS t          j                    | j        k    r8	 |                    t          j                   n# t          t          f$ r Y nw xY w	 |                                 dS # t          $ r Y dS w xY w)z!Disconnects from the Redis serverN)r   on_disconnectr   r   r   r   shutdownr  	SHUT_RDWRr  rK   close)rO   r>   	conn_socks      rT   r   zAbstractConnection.disconnect  s    ""$$$J	
F9;;$(""""6#34444Y'   	OO 	 	 	DD	s$   	A) )A=<A=B 
B%$B%c                     |                      dd           t          |                                           dk    rt          d          dS )z Send PING, expect PONG in returnPINGFr  PONGz#Bad response from PING health checkN)r   r6   r   r(   rr   s    rT   
_send_pingzAbstractConnection._send_ping  sQ    &u555**,,--77!"GHHH 87r9   c                 .    |                                   dS )z Function to call when PING failsNr   rO   r  s     rT   _ping_failedzAbstractConnection._ping_failed      r9   c                     | j         r>t                      | j        k    r)| j                            | j        | j                   dS dS dS )z3Check the health of the connection with a PING/PONGN)r   r   r   r   r  r,  r/  rr   s    rT   r   zAbstractConnection.check_health  sT    % 	K$&&43I*I*IJ&&t8IJJJJJ	K 	K*I*Ir9   Tc                 r   | j         s|                                  |r|                                  	 t          |t                    r|g}|D ]}| j                             |           dS # t          j        $ r$ |                                  t          d          t          $ rq}|                                  t          |j                  dk    rd|j        d         }}n|j        d         }|j        d         }t          d| d| d          d}~wt          $ r |                                   w xY w)	z2Send an already packed command to the Redis serverzTimeout writing to socketr   UNKNOWNr   zError z while writing to socket. r   N)r   r   r   rC   rD   sendallr  r   r   r,   r  rf   r>   r(   BaseException)rO   r   r   itemr	  errnoerrmsgs          rT   r   z&AbstractConnection.send_packed_command   sk   z 	LLNNN 	 	'3'' $") ) )
""4(((() )~ 	< 	< 	<OO:;;; 	W 	W 	WOO16{{a )16!9vq	!"U5"U"UF"U"U"UVVV 	 	 	
 OO	s   7A, ,;D6'A,D#D6c                 v    |                       | j        j        | |                    dd                     dS )z+Pack and send a command to the Redis serverr   Tr  N)r   r   rU   r  r   s      rT   r   zAbstractConnection.send_command?  sJ      %D %t,ND99 	! 	
 	
 	
 	
 	
r9   c                    | j         }|s|                                  |                                 }	 | j                            |          S # t
          $ r3}|                                  t          d| d|j                   d}~ww xY w)z8Poll the socket to see if there's data that can be read.Error while reading from z: N)	r   r   r  r   r   r  r   r(   r>   )rO   r   r  
host_errorr	  s        rT   r   zAbstractConnection.can_readF  s    z 	LLNNN%%''
	V<((111 	V 	V 	VOO!"Tj"T"TAF"T"TUUU	Vs   A 
B
.BB
r   c                r   |                                  }	 | j        dv r$t          s| j                            ||          }n| j                            |          }n# t
          j        $ r) |r|                                  t          d|           t          $ r5}|r|                                  t          d| d|j                   d}~wt          $ r |r|                                   w xY w| j        rt                      | j        z   | _        t!          |t"                    r		 |# ~w xY w|S )z0Read the response from a previously sent command)3r   )r   r   )r   zTimeout reading from r;  z : N)r  r   r/   r   r   r  r   r   r,   r  r(   r>   r5  r   r   r   rC   r+   )rO   r   r   r   r<  responser	  s          rT   r   z AbstractConnection.read_responseT  s    %%''
	}((1B(<55%5L 6    <55GW5XX~ 	E 	E 	E" "!!!CzCCDDD 	 	 	" "!!!!GJGGqvGG    	 	 	 # "!!!	 % 	I%)VVd.H%HD"h.. 	s%   AA A C40C%C4/D1 1D4c                       | j         j        | S )rA   )r   rU   r   s     rT   rJ   zAbstractConnection.pack_command  s    (t#($//r9   c                    g }g }d}| j         }|D ]} | j        j        | D ]}t          |          }||k    s||k    st	          |t
                    r3|r-|                    t                              |                     d}g }||k    st	          |t
                    r|                    |           |                    |           ||z  }|r-|                    t                              |                     |S )z.Pack multiple commands into the Redis protocolr   )	r_   r   rU   rf   rC   ri   rH   rc   rd   )	rO   r   rP   piecesbuffer_lengthr`   cmdchunkchunklens	            rT   r   z AbstractConnection.pack_commands  s(   + 	. 	.C2-2C8 . .u::!M11-//!%44 0  >innV&<&<===$%MFm++z%/L/L+MM%((((MM%(((!X-MM!.$  	2MM)..00111r9   r\   c                     | j         S r^   )r   rr   s    rT   r   zAbstractConnection.get_protocol  s
    }r9   c                     | j         S r^   _handshake_metadatarr   s    rT   r   z%AbstractConnection.handshake_metadata  s    ''r9   rR   c                     || _         d S r^   rI  )rO   rR   s     rT   r   z%AbstractConnection.handshake_metadata  s    #(   r9   r   c                     || _         d S r^   )r   r   s     rT   r   z$AbstractConnection.set_re_auth_token  s    #r9   c                     | j         c|                     d| j                             d          | j                                                    |                                  d | _         d S d S Nr  oid)r   r   try_get	get_valuer   rr   s    rT   r   zAbstractConnection.re_auth  sv    *#++E22#--//  
    "&D +*r9   r   r   r   )2rV   rW   rX   __doc__r   r;   r5   r   r   rD   floatboolr   r	   r
   r!   r$   ra   r   r   rs   r   r   rx   rz   r~   r   r   r  r  r   r   r,  r/  r   r   r   r   r   rJ   r   r   r   r   r   r   setterr   r   r   rY   r9   rT   r   r      sI       66 "&*.26!&'!&" %%&%)",%4_%6%6"&"&;?<@"#7;6:/^N ^N^N 3-^N !	^N
 !)^N ^N ^N ^N ^N ^N  #^N c]^N 3-^N  c]!^N" 3-#^N$ S$Y%^N& %Xb$h%78'^N( &&89)^N* 3-+^N, !"d(!34-^N. #?3/^N ^N ^N ^N@W W W   ^  R R R
/ 
/ 
/	 	 	M M M! ! !F   ^   ^C C CX: X: X:t  (I I I  K K K
   >
 
 
V V V V  , !, , , , ,\0 0 0  <cjS     (E$ue|*<d38n*L$M ( ( ( X( )d5%<.@$sCx..P(Q ) ) ) )$~ $ $ $ $' ' ' ' 'r9   r   c                   @     e Zd ZdZ	 	 	 	 	 d fd	Zd Zd	 Zd
 Z xZS )
Connectionz4Manages TCP communication to and from a Redis server	localhost  FNr   c                     || _         t          |          | _        || _        |pi | _        || _         t                      j        di | d S NrY   )hostr   portsocket_keepalivesocket_keepalive_optionssocket_typesuperra   )rO   r\  r]  r^  r_  r`  r   r   s          rT   ra   zConnection.__init__  sY     	II	 0(@(FB%&""6"""""r9   c                     d| j         fd| j        fd| j        fg}| j        r|                    d| j        f           |S )Nr\  r]  r   r   )r\  r]  r   r   rH   rO   rB  s     rT   rs   zConnection.repr_pieces  sL    49%	':T47OL 	=MM=$*:;<<<r9   c                    d}t          j        | j        | j        | j        t           j                  D ]<}|\  }}}}}d}	 t          j         |||          }|                    t           j        t           j        d           | j	        rk|                    t           j
        t           j        d           | j                                        D ]&\  }	}
|                    t           j        |	|
           '|                    | j                   |                    |           |                    | j                   |c S # t$          $ r#}|}||                                 Y d}~6d}~ww xY w||t%          d          )zCreate a TCP socket connectionNr   z)socket.getaddrinfo returned an empty list)r  getaddrinfor\  r]  r`  SOCK_STREAM
setsockoptIPPROTO_TCPTCP_NODELAYr^  
SOL_SOCKETSO_KEEPALIVEr_  items
settimeoutr   r   r   r  r'  )rO   errresfamilysocktyper  	canonnamesocket_addressr  r   r   rQ   s               rT   r   zConnection._connect  s   
 %Ity$"2F4F
 
 	! 	!C BE>FHeYD!}VXu== 2F4FJJJ ( BOOF$5v7JANNN $ = C C E E B B1(:AqAAAA  ;<<< ^,,,  3444 ! ! !#JJLLL!
 ?IABBBs   C=E
E.E))E.c                 $    | j          d| j         S )N:)r\  r]  rr   s    rT   r  zConnection._host_error  s    )))di)))r9   )rX  rY  FNr   	rV   rW   rX   rR  ra   rs   r   r  __classcell__r   s   @rT   rW  rW    s        :: !%# # # # # #   'C 'C 'CR* * * * * * *r9   rW  c                   b   e Zd ZdZdZdZdededej	        fdZ
d Zd	 Zd
 Zd Zd Zd Zd Zd Zd'dZd Zd(dZ	 d)ddddZd Zd Zedeeeef         eeef         f         fd            Zd Zd Z deddfdZ!d  Z"d!e#eee$e#e                  f                  fd"Z%d# Z&d$e'fd%Z(d& Z)dS )*CacheProxyConnections   fooz7.4.0redisconncache	pool_lockc                 ^   t          j                    | _        || _        | j        j        | _        | j        j        | _        | j        j        | _        |j        | _        || _        || _	        t          j                    | _        d | _        d | _        |                     | j                   d S r^   )r   r   r   _connr   r\  r]  r   
_pool_lock_cache	threadingLock_cache_lock_current_command_cache_key_current_optionsrx   _enable_tracking_callback)rO   r|  r}  r~  s       rT   ra   zCacheProxyConnection.__init__  s     9;;
Z%
JO	JO	#'#; #$>++*.' $&&t'EFFFFFr9   c                 4    | j                                         S r^   )r  rs   rr   s    rT   rs   z CacheProxyConnection.repr_pieces  s    z%%'''r9   c                 :    | j                             |           d S r^   )r  rx   rv   s     rT   rx   z.CacheProxyConnection.register_connect_callback  s    
,,X66666r9   c                 :    | j                             |           d S r^   )r  rz   rv   s     rT   rz   z0CacheProxyConnection.deregister_connect_callback!  s    
..x88888r9   c                 :    | j                             |           d S r^   )r  r~   r|   s     rT   r~   zCacheProxyConnection.set_parser$  s    
l+++++r9   c                    | j                                          | j         j                            dd           }| | j         j                            dd           }| j         j                            dd           }| | j         j                            dd           }||t	          d          t          |          }t          |          }|| j        k    st          || j                  dk    rt	          d          d S )Ns   serverservers   versionversionz0Cannot retrieve information about server versionr   ziTo maximize compatibility with all Redis products, client-side caching is supported by Redis 7.4 or later)	r  r   r   r  r(   r3   DEFAULT_SERVER_NAMEr1   MIN_ALLOWED_VERSION)rO   server_name
server_vers      rT   r   zCacheProxyConnection.connect'  s   
j377	4HH*7;;HdKKKZ266z4HH
6::9dKKJ!3!"TUUU":..
#K00 4333
D,DEEJJ!{   KJr9   c                 8    | j                                          d S r^   )r  r   rr   s    rT   r   zCacheProxyConnection.on_connect>  s    
r9   c                     | j         5  | j                                         d d d            n# 1 swxY w Y    | j        j        |  d S r^   )r  r  flushr  r   r   s     rT   r   zCacheProxyConnection.disconnectA  s     	  	 K	  	  	  	  	  	  	  	  	  	  	  	  	  	  	 
t$$$$s   .22c                 8    | j                                          d S r^   )r  r   rr   s    rT   r   z!CacheProxyConnection.check_healthF  s    
!!!!!r9   Tc                 :    | j                             |           d S r^   )r  r   r   s      rT   r   z(CacheProxyConnection.send_packed_commandI  s      	
&&w/////r9   c           	      T   |                                   | j        5  | j                            t	          |d         d                    s'd | _         | j        j        |i | 	 d d d            d S 	 d d d            n# 1 swxY w Y   |                    d          t          d          t	          |d         t          |                    d                              | _        | j        5  | j                            | j                  r| j                            | j                  }|j        | j        k    rl| j        5  |j                                        r4|j                            d           |j                                        4d d d            n# 1 swxY w Y   	 d d d            d S | j                            t!          | j        | j        t$          j        | j                             d d d            n# 1 swxY w Y    | j        j        |i | d S )	Nr   rY   )r   
redis_keyskeyszCannot create cache key.Tr   )	cache_keycache_valuestatusconnection_ref)_process_pending_invalidationsr  r  is_cachabler   r  r  r   r  r   rE   r  r  r   r   setr   DUMMY_CACHE_VALUEr   IN_PROGRESS)rO   r>   r   entrys       rT   r   z!CacheProxyConnection.send_commandN  s6   ++--- 	 	 ;**8DGPR+S+S+STT 26/'
'8888	 	 	 	 	 	 	 		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ::f%7888 +3Gfjj.@.@(A(A+
 +
 +
'  	 	 {t>?? (GHH'4:55 R R#2;;== R!0>>D>QQQ $2;;== RR R R R R R R R R R R R R R R 	 	 	 	 	 	 	 	  KOO"= $ 6+7#':	    !	 	 	 	 	 	 	 	 	 	 	 	 	 	 	6 	 
000000sQ   A
B  BB2AHAF"H"F&	&H)F&	*H;AHHHr   c                 6    | j                             |          S r^   )r  r   r   s     rT   r   zCacheProxyConnection.can_read~  s    z""7+++r9   Fr   c                @   | j         5  | j        | j                            | j                  }| j                            | j                  j        t
          j        k    rKt          j        | j                            | j                  j	                  }d | _        |cd d d            S d d d            n# 1 swxY w Y   | j
                            |||          }| j         5  | j        |cd d d            S |.| j                            | j        g           |cd d d            S | j                            | j                  }|2t
          j        |_        ||_	        | j                            |           d | _        d d d            n# 1 swxY w Y   |S )N)r   r   r   )r  r  r  r  r  r   r  r   r   r  r  r   delete_by_cache_keysVALIDr  )rO   r   r   r   ro  r?  cache_entrys          rT   r   z"CacheProxyConnection.read_response  sj     	 	 /;KOOD$CDDPKOOD$CDDK#/0 0 mKOOD$CDDP  37/	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 :++- 3% , 
 
  	3 	3.6	3 	3 	3 	3 	3 	3 	3 	3
 00$2Q1RSSS	3 	3 	3 	3 	3 	3 	3 	3 +//$*IJJK &%5%;"*2',,,.2D+%	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3( s1   BB88B<?B<'	F=#F-AFFFc                       | j         j        | S r^   )r  rJ   r   s     rT   rJ   z!CacheProxyConnection.pack_command  s    &tz&--r9   c                 6    | j                             |          S r^   )r  r   r   s     rT   r   z"CacheProxyConnection.pack_commands  s    z''111r9   r\   c                     | j         j        S r^   )r  r   rr   s    rT   r   z'CacheProxyConnection.handshake_metadata  s    z,,r9   c                 8    | j                                          d S r^   )r  r   rr   s    rT   r   zCacheProxyConnection._connect  s    
r9   c                 8    | j                                          d S r^   )r  r  rr   s    rT   r  z CacheProxyConnection._host_error  s    
     r9   Nc                     |                     ddd           |                                 |j                            | j                   d S )Nr  TRACKINGON)r   r   r   set_invalidation_push_handler_on_invalidation_callback)rO   r|  s     rT   r  z.CacheProxyConnection._enable_tracking_callback  sK    (J5552243QRRRRRr9   c                     |                                  r1| j                            d           |                                  /d S d S )NTr  )r   r  r   rr   s    rT   r  z3CacheProxyConnection._process_pending_invalidations  sQ    mmoo 	8J$$$$777 mmoo 	8 	8 	8 	8 	8r9   datac                     | j         5  |d         | j                                         n | j                            |d                    d d d            d S # 1 swxY w Y   d S )Nr   )r  r  r  delete_by_redis_keys)rO   r  s     rT   r  z.CacheProxyConnection._on_invalidation_callback  s     	: 	:Aw!!####00a999	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	:s   AAAAc                 4    | j                                         S r^   )r  r   rr   s    rT   r   z!CacheProxyConnection.get_protocol  s    z&&(((r9   r   c                 :    | j                             |           d S r^   )r  r   r   s     rT   r   z&CacheProxyConnection.set_re_auth_token  s    
$$U+++++r9   c                 8    | j                                          d S r^   )r  r   rr   s    rT   r   zCacheProxyConnection.re_auth  s    
r9   r   r   r   )*rV   rW   rX   r  r  r  rp   r   r  r  ra   rs   rx   rz   r~   r   r   r   r   r   r   r   r   rJ   r   r   r   r   r   rD   r   r   r  r  r  r   r   r  r   r   r   r   rY   r9   rT   rz  rz    s[       !!G!G G >	G G G G&( ( (7 7 79 9 9, , ,  .     % % %
" " "0 0 0 0
.1 .1 .1`, , , ,  %+=APU+ + + + +Z. . .2 2 2 -E$ue|*<d38n*L$M - - - X-  ! ! !S.A Sd S S S S
8 8 8:d5htE{>S9S3T.U : : : :) ) ),~ , , , ,    r9   rz  c                   P     e Zd ZdZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 d fd	Z fdZd Z xZS )	SSLConnectionzManages SSL connections to and from the Redis server(s).
    This class extends the Connection class, adding SSL functionality, and making
    use of ssl.SSLContext (https://docs.python.org/3/library/ssl.html#ssl.SSLContext)
    NrequiredFc                    t           st          d          || _        || _        |t          j        }nWt          |t                    rBt          j        t          j        t          j	        d}||vrt          d|           ||         }|| _
        || _        || _        || _        || _        || _        |	| _        |
| _        || _        || _        || _        || _         t-                      j        di | dS )a  Constructor

        Args:
            ssl_keyfile: Path to an ssl private key. Defaults to None.
            ssl_certfile: Path to an ssl certificate. Defaults to None.
            ssl_cert_reqs: The string value for the SSLContext.verify_mode (none, optional, required). Defaults to "required".
            ssl_ca_certs: The path to a file of concatenated CA certificates in PEM format. Defaults to None.
            ssl_ca_data: Either an ASCII string of one or more PEM-encoded certificates or a bytes-like object of DER-encoded certificates.
            ssl_check_hostname: If set, match the hostname during the SSL handshake. Defaults to False.
            ssl_ca_path: The path to a directory containing several CA certificates in PEM format. Defaults to None.
            ssl_password: Password for unlocking an encrypted private key. Defaults to None.

            ssl_validate_ocsp: If set, perform a full ocsp validation (i.e not a stapled verification)
            ssl_validate_ocsp_stapled: If set, perform a validation on a stapled ocsp response
            ssl_ocsp_context: A fully initialized OpenSSL.SSL.Context object to be used in verifying the ssl_ocsp_expected_cert
            ssl_ocsp_expected_cert: A PEM armoured string containing the expected certificate to be returned from the ocsp verification service.
            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.
            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.

        Raises:
            RedisError
        z$Python wasn't built with SSL supportN)noneoptionalr  z+Invalid SSL Certificate Requirements Flag: rY   )r0   r*   keyfilecertfilessl	CERT_NONErC   rD   CERT_OPTIONALCERT_REQUIRED	cert_reqsca_certsca_dataca_pathcheck_hostnamecertificate_passwordssl_validate_ocspssl_validate_ocsp_stapledssl_ocsp_contextssl_ocsp_expected_certssl_min_versionssl_ciphersra  ra   )rO   ssl_keyfilessl_certfilessl_cert_reqsssl_ca_certsssl_ca_datassl_check_hostnamessl_ca_pathssl_passwordr  r  r  r  r  r  r   	CERT_REQSr   s                    rT   ra   zSSLConnection.__init__  s   P  	ECDDD"$ MMMs++ 
	5-- I
 I-- Q-QQ   &m4M&$""0$0!!2)B& 0&<#.&""6"""""r9   c                     t                                                      }	 |                     |          S # t          t          f$ r |                                  w xY w)zN
        Wrap the socket with SSL support, handling potential errors.
        )ra  r   _wrap_socket_with_sslr  r*   r'  )rO   r  r   s     rT   r   zSSLConnection._connect'  sc     ww!!	--d333$ 	 	 	JJLLL	s	   8 'Ac                 H   t          j                    }| j        |_        | j        |_        | j        s| j        r'|                    | j        | j        | j                   | j	        | j
        | j        '|                    | j	        | j
        | j                   | j        | j        |_        | j        r|                    | j                   | j        du rt$          du rt'          d          | j        r| j        rt'          d          |                    || j                  }| j        rd	dl}d
dlm} | j        Y|j                            |j        j                  }|                    | j                   |                    | j                   n| j        }|                     || j!                   |j        "                    |tG          j#                              }|$                                 |%                    | j        | j&        f           |'                                 |(                                 |S | j        du rOt$          rHd
dlm)}  ||| j        | j&        | j	                  }	|	*                                r|S tW          d          |S )z
        Wraps the socket with SSL support.

        Args:
            sock: The plain socket to wrap with SSL.

        Returns:
            An SSL wrapped socket.
        )r  r  r   N)cafilecapathcadataTFzcryptography is not installed.zKEither an OCSP staple or pure OCSP connection must be validated - not both.)server_hostnamer   r   )ocsp_staple_verifier)OCSPVerifierzocsp validation error),r  create_default_contextr  r  verify_moder  r  load_cert_chainr  r  r  r  load_verify_locationsr  minimum_versionr  set_ciphersr  r.   r*   r  wrap_socketr\  OpenSSLocspr  r  SSLContextSSLv23_METHODuse_certificate_fileuse_privatekey_fileset_ocsp_client_callbackr  rW  r  request_ocspr   r]  do_handshaker%  r  is_validr(   )
rO   r  contextsslsockr  r  
staple_ctxconr  os
             rT   r  z#SSLConnection._wrap_socket_with_ssl2  s    ,..!%!4"n= 	DL 	##2 $    M%|'|'))}T\$, *    +&*&:G# 	2 0111!T)).D.M.M=>>>) 	d.D 	  
 %%dDI%FF ) 	NNN222222 $,$[001JKK
//>>>..t|<<<<!2
//$d&A  
 +((V]__EECKKDI.///LLNNNN !T)).D)******WdiDMJJAzz|| ?%&=>>>r9   )NNr  NNFNNFFNNNN)rV   rW   rX   rR  ra   r   r  rw  rx  s   @rT   r  r    s            "'#F# F# F# F# F# F#P	 	 	 	 	M M M M M M Mr9   r  c                   6     e Zd ZdZd fd	Zd Zd Zd Z xZS )	UnixDomainSocketConnectionz4Manages UDS communication to and from a Redis server Nc                 V     t                      j        di | || _        || _        d S r[  )ra  ra   pathr   )rO   r  r   r   r   s       rT   ra   z#UnixDomainSocketConnection.__init__  s5    ""6"""	,r9   c                 p    d| j         fd| j        fg}| j        r|                    d| j        f           |S )Nr  r   r   )r  r   r   rH   rc  s     rT   rs   z&UnixDomainSocketConnection.repr_pieces  sC    49%dg7 	=MM=$*:;<<<r9   c                 >   t          j         t           j        t           j                  }|                    | j                   	 |                    | j                   n## t          $ r |                                  w xY w|                    | j	                   |S )z&Create a Unix domain socket connection)
r  AF_UNIXrf  rm  r   r   r  r  r'  r   )rO   r  s     rT   r   z#UnixDomainSocketConnection._connect  s    }V^V-?@@3444	LL#### 	 	 	JJLLL	 	+,,,s   A    B c                     | j         S r^   )r  rr   s    rT   r  z&UnixDomainSocketConnection._host_error  s
    yr9   )r   Nrv  rx  s   @rT   r  r    sp        ::- - - - - -
          r9   r  )0FFALSENNOc                     | | dk    rd S t          | t                    r|                                 t          v rdS t	          |           S )Nr   F)rC   rD   upperFALSE_STRINGSrT  )rR   s    rT   to_boolr    sI    }t% %++--="@"@u;;r9   )
r   r   r   r^  r   r   max_connectionsr   r  r   c                 \   |                      d          s9|                      d          s$|                      d          st          d          t          |           } i }t          | j                                                  D ]\  }}|rt          |          dk    rnt          |d                   }t          	                    |          }|r8	  ||          ||<   [# t          t          f$ r t          d| d          w xY w|||<   | j        rt          | j                  |d<   | j        rt          | j                  |d	<   | j        d
k    r)| j        rt          | j                  |d<   t          |d<   n| j        rt          | j                  |d<   | j        rt%          | j                  |d<   | j        rUd|vrQ	 t%          t          | j                                      dd                    |d<   n# t(          t          f$ r Y nw xY w| j        dk    r
t*          |d<   |S )Nzredis://z	rediss://zunix://zRRedis URL must specify one of the following schemes (redis://, rediss://, unix://)r   zInvalid value for 'z' in connection URL.r   r   unixr  connection_classr\  r]  r   /r   rediss)
startswithr   r   r   queryrl  rf   r   URL_QUERY_ARGUMENT_PARSERSr  rK   r   r   schemer  r  hostnamer]  r   replaceAttributeErrorr  )urlr   namerR   r  s        rT   	parse_urlr    su   z""
>>+&&
 >>)$$

 5
 
 	

 3--CF	**0022 
% 
%e 		%SZZ!^^E!H%%E/33D99F %W#)6%==F4LL!:. W W W$%U4%U%U%UVVVW  %t
| 3$S\22z
| 3$S\22z zV8 	/$SX..F6N%?!"" < 	3$S\22F6N8 	+ ]]F6N 8 	F**"738#4#4#<#<S"#E#EFFt"J/    :!!)6F%&Ms   C!!%D8H   HH_CPConnectionPool)boundc                   2   e Zd ZdZedee         dedefd            Ze	ddfde
e         de
e         fd	Zdeeffd
Zd Zd%dZd%dZ edgdd          d&d'd            ZdefdZd(dZd)dZdddefdZd*deddfdZd%dZd+d Zd!efd"Zd#efd$Z dS ),r!  a  
    Create a connection pool. ``If max_connections`` is set, then this
    object raises :py:class:`~redis.exceptions.ConnectionError` when the pool's
    limit is reached.

    By default, TCP connections are created unless ``connection_class``
    is specified. Use class:`.UnixDomainSocketConnection` for
    unix sockets.

    Any additional keyword arguments are passed to the constructor of
    ``connection_class``.
    clsr  r\   c                 x    t          |          }d|v r|d         |d<   |                    |            | di |S )a  
        Return a connection pool configured from the given URL.

        For example::

            redis://[[username]:[password]]@localhost:6379/0
            rediss://[[username]:[password]]@localhost:6379/0
            unix://[username@]/path/to/socket.sock?db=0[&password=password]

        Three URL schemes are supported:

        - `redis://` creates a TCP socket connection. See more at:
          <https://www.iana.org/assignments/uri-schemes/prov/redis>
        - `rediss://` creates a SSL wrapped TCP socket connection. See more at:
          <https://www.iana.org/assignments/uri-schemes/prov/rediss>
        - ``unix://``: creates a Unix Domain Socket connection.

        The username, password, hostname, path and all querystring values
        are passed through urllib.parse.unquote in order to replace any
        percent-encoded values with their corresponding characters.

        There are several ways to specify a database number. The first value
        found will be used:

            1. A ``db`` querystring option, e.g. redis://localhost?db=0
            2. If using the redis:// or rediss:// schemes, the path argument
               of the url, e.g. redis://localhost/0
            3. A ``db`` keyword argument to this function.

        If none of these options are specified, the default db=0 is used.

        All querystring options are cast to their appropriate Python types.
        Boolean arguments can be specified with string values "True"/"False"
        or "Yes"/"No". Values that cannot be properly cast cause a
        ``ValueError`` to be raised. Once parsed, the querystring arguments
        and keyword arguments are passed to the ``ConnectionPool``'s
        class initializer. In the case of conflicting arguments, querystring
        arguments always win.
        r  rY   )r  update)r$  r  r   url_optionss       rT   from_urlzConnectionPool.from_url  sP    R  nn''.45G.HK*+k"""s}}V}}r9   Nr  cache_factoryc                    |pd}t          |t                    r|dk     rt          d          || _        || _        || _        d | _        || _        |                    d          s|                    d          r|                    d          dvrt          d          | j                            d          }|,t          |t                    st          d	          || _        nd| j        | j                                        | _        n>t          | j                            d                                                    | _        |                    dd            |                    dd            | j                            d
d           | _        | j        t                      | _        t!          j                    | _        t!          j                    | _        |                                  d S )Nl        r   z,"max_connections" must be a positive integercache_configr}  r   )r   r>  z4Client caching is only supported with RESP version 3z#Cache must implement CacheInterfacer   )rC   r   r   r  connection_kwargsr  r}  _cache_factoryr  r*   r   	get_cacher   popr   r$   r  r  
_fork_lock_lockreset)rO   r  r  r)  r,  r}  s         rT   ra   zConnectionPool.__init__5  s    *2U/3// 	M?Q3F3FKLLL 0!2.
+  00 	"4E4I4I'4R4R 	" $$Z00@@ !WXXX*..w77E !%88 L$%JKKK"

&2!%!4!>!>!@!@DJJ!-.22>BB" "ikk J 	gt,,,nd333!%!7!;!;<NPT!U!U!)%4%6%6D" $.**^%%


r9   c                     dt          |           j         dt          |           j         dt           | j        di | j                   dS )Nr   r   r   r   rY   )typerW   rV   reprr  r,  rr   s    rT   r   zConnectionPool.__repr__l  so    JT

% J JT

(; J J*T*DDT-CDDEEJ J J	
r9   c                 8    | j                             dd          S )z
        Returns:
            The RESP protocol version, or ``None`` if the protocol is not specified,
            in which case the server default will be used.
        r   N)r,  r  rr   s    rT   r   zConnectionPool.get_protocolr  s     %))*d;;;r9   c                 x    d| _         g | _        t                      | _        t	          j                    | _        d S )Nr   )_created_connections_available_connectionsr  _in_use_connectionsr   r   r   rr   s    rT   r2  zConnectionPool.resetz  s1    $%!&(##&55  9;;r9   c                 Z   | j         t          j                    k    r| j                            d          }|st
          	 | j         t          j                    k    r|                                  | j                                         d S # | j                                         w xY wd S )N   )r   )r   r   r   r0  acquirer'   r2  release)rO   acquireds     rT   	_checkpidzConnectionPool._checkpid  s    F 8ry{{""..q.99H +***8ry{{**JJLLL'')))))'')))) #"s   0B B(*)Use get_connection() without args instead5.3.0args_to_warnreasonr  rW  c                    |                                   | j        5  	 | j                                        }n$# t          $ r |                                 }Y nw xY w| j                            |           ddd           n# 1 swxY w Y   	 |                                 	 |	                                r| j
        t          d          nb# t          t          f$ rN |                                 |                                 |	                                rt          d          Y nw xY wn$# t          $ r |                     |            w xY w|S )zGet a connection from the poolNConnection has dataConnection not ready)r@  r1  r9  r/  
IndexErrormake_connectionr:  addr   r   r}  r(   r  r   r5  r>  rO   command_namer  options
connections        rT   get_connectionzConnectionPool.get_connection  s    	Z 	5 	54!8<<>>

 4 4 4!1133


4$((444	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5	   
B&&(( ATZ-?)*?@@@#W- B B B%%'''""$$$&&(( B)*@AAAB BB
  	 	 	 LL$$$		 sb   B8BABABB	BD1 #*C D1 AD-*D1 ,D--D1 1!Ec                     | j         }t          |                    dd          |                    dd          |                    dd                    S )z,Return an encoder based on encoding settingsr   r   r   r   r   F)r   r   r   )r,  r   r  )rO   r   s     rT   get_encoderzConnectionPool.get_encoder  sV    'ZZ
G44"JJ'8(CC#ZZ(:EBB
 
 
 	
r9   rp   c                     | j         | j        k    rt          d          | xj         dz  c_         | j        +t	           | j        di | j        | j        | j                  S  | j        di | j        S )zCreate a new connectionzToo many connectionsr   NrY   )r8  r  r(   r}  rz  r  r,  r1  rr   s    rT   rK  zConnectionPool.make_connection  s    $(<<<!"8999!!Q&!!:!'%%??(>??TZ   %t$>>t'=>>>r9   rP  c                    |                                   | j        5  	 | j                            |           n# t          $ r Y ddd           dS w xY w|                     |          rB| j                            |           | j        	                    t          |                     n"|                                 	 ddd           dS 	 ddd           dS # 1 swxY w Y   dS )z(Releases the connection back to the poolN)r@  r1  r:  r   KeyErrorowns_connectionr9  rH   r   dispatchr#   r   rO   rP  s     rT   r>  zConnectionPool.release  s   Z 	 	(//
;;;;    	 	 	 	 	 	 	 	
 ##J// +22:>>>&//0<<    %%''''	 	 	 	 	 	 	 		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s2   C9C
ACAA/CC #C c                 "    |j         | j         k    S r^   )r   rY  s     rT   rW  zConnectionPool.owns_connection  s    ~))r9   Tinuse_connectionsc                     |                                   | j        5  |rt          | j        | j                  }n| j        }|D ]}|                                 	 ddd           dS # 1 swxY w Y   dS )z
        Disconnects connections in the pool

        If ``inuse_connections`` is True, disconnect connections that are
        current in use, potentially by other threads. Otherwise only disconnect
        connections that are idle in the pool.
        N)r@  r1  r   r9  r:  r   )rO   r[  connectionsrP  s       rT   r   zConnectionPool.disconnect  s     	Z 		( 		(  :#/1I  #9) ( (
%%''''(		( 		( 		( 		( 		( 		( 		( 		( 		( 		( 		( 		( 		( 		( 		( 		( 		( 		(s   >A((A,/A,c                 .    |                                   dS )z-Close the pool, disconnecting all connectionsNr   rr   s    rT   r'  zConnectionPool.close%  r0  r9   r   r-   c                     | j                             d|i           | j        D ]	}||_        
| j        D ]	}||_        
d S )Nr   )r,  r&  r9  r   r:  )rO   r   r|  s      rT   	set_retryzConnectionPool.set_retry)  s^    %%w&6777/ 	 	DDJJ, 	 	DDJJ	 	r9   r   c                 &     j         5   j        D ]Ej                            fd fd           j                            fd fd           F j        D ]                               	 d d d            d S # 1 swxY w Y   d S )Nc                  |                          d                    d                                                    S rN  )r   rP  rQ  )r|  r   s   rT   r   z1ConnectionPool.re_auth_callback.<locals>.<lambda>4  s4    D--e 4 4eoo6G6G  r9   c                 .                         |           S r^   _mockr   s    rT   r   z1ConnectionPool.re_auth_callback.<locals>.<lambda>7  s    $**U"3"3 r9   c                  ,                                      S r^   )r   )r|  s   rT   r   z1ConnectionPool.re_auth_callback.<locals>.<lambda>:  s    D..00 r9   c                 .                         |           S r^   rd  r   s    rT   r   z1ConnectionPool.re_auth_callback.<locals>.<lambda>:  s    

5@Q@Q r9   )r1  r9  r   r  r:  r   )rO   r   r|  s   ``@rT   re_auth_callbackzConnectionPool.re_auth_callback0  s-   Z 	. 	.3 	 	
**     4333	   
**00002Q2Q2Q2Q    0 . .&&u----.	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	.s   A-BB
B
r  c                 
   K   dS )z
        Dummy functions, needs to be passed as error callback to retry object.
        :param error:
        :return:
        NrY   r.  s     rT   re  zConnectionPool._mock?  s       	r9   rn   r^   )r\   rW  )r\   rp   )rP  rW  r\   Nr   )r   r-   r\   N)!rV   rW   rX   rR  classmethodr   r   rD   r(  rW  r   r   r   ra   r   r   r2  r@  r2   rQ  r   rS  rK  r>  rW  rT  r   r'  r`  r   rh  r*   re  rY   r9   rT   r!  r!    s!         .d3i .c . . . . [.d $)-9=	5 5 "#5   56	5 5 5 5n
3* 
 
 
 
< < <    -* -* -* -*^ _U:  
        
 D
W 
 
 
 
? ? ? ?   0*, *3 * * * *( (D (D ( ( ( ((      .n . . . .      r9   c                   v     e Zd ZdZddeef fd	Zd Zd Z e	dgdd	
          dd            Z
d Zd Z xZS )BlockingConnectionPoola  
    Thread-safe blocking connection pool::

        >>> from redis.client import Redis
        >>> client = Redis(connection_pool=BlockingConnectionPool())

    It performs the same function as the default
    :py:class:`~redis.ConnectionPool` implementation, in that,
    it maintains a pool of reusable connections that can be shared by
    multiple redis clients (safely across threads if required).

    The difference is that, in the event that a client tries to get a
    connection from the pool when all of connections are in use, rather than
    raising a :py:class:`~redis.ConnectionError` (as the default
    :py:class:`~redis.ConnectionPool` implementation does), it
    makes the client wait ("blocks") for a specified number of seconds until
    a connection becomes available.

    Use ``max_connections`` to increase / decrease the pool size::

        >>> pool = BlockingConnectionPool(max_connections=10)

    Use ``timeout`` to tell it either how many seconds to wait for a connection
    to become available, or to block forever:

        >>> # Block forever.
        >>> pool = BlockingConnectionPool(timeout=None)

        >>> # Raise a ``ConnectionError`` after five seconds if a connection is
        >>> # not available.
        >>> pool = BlockingConnectionPool(timeout=5)
    2      c                 \    || _         || _         t                      j        d||d| d S )N)r  r  rY   )queue_classr   ra  ra   )rO   r  r   r  rp  r,  r   s         rT   ra   zBlockingConnectionPool.__init__j  sS     ' 	
-+	
 	
  	
 	
 	
 	
 	
r9   c                     |                      | j                  | _        	 	 | j                            d            n# t          $ r Y nw xY w-g | _        t          j                    | _        d S r^   )	rp  r  pool
put_nowaitr   _connectionsr   r   r   rr   s    rT   r2  zBlockingConnectionPool.resetz  s    $$T%9::			$$T****   	  9;;s   = 
A
	A
c                     | j         ,t           | j        di | j        | j         | j                  }n | j        di | j        }| j                            |           |S )zMake a fresh connection.NrY   )r}  rz  r  r,  r1  rt  rH   rY  s     rT   rK  z&BlockingConnectionPool.make_connection  sv    :!-%%??(>??TZ JJ /.HH1GHHJ  ,,,r9   rA  rB  rC  rD  Nc                 `   |                                   d}	 | j                            d| j                  }n# t          $ r t          d          w xY w||                                 }	 |                                 	 |                                rt          d          nb# t
          t          f$ rN |
                                 |                                 |                                rt          d          Y nw xY wn$# t          $ r |                     |            w xY w|S )a7  
        Get a connection, blocking for ``self.timeout`` until a connection
        is available from the pool.

        If the connection returned is ``None`` then creates a new connection.
        Because we use a last-in first-out queue, the existing connections
        (having been returned to the pool after the initial ``None`` values
        were added) will be returned before ``None`` values. This means we only
        create new connections when we need to, i.e.: the actual number of
        connections will only increase in response to demand.
        NT)blockr   zNo connection available.rH  rI  )r@  rr  r  r   r   r(   rK  r   r   r  r   r5  r>  rM  s        rT   rQ  z%BlockingConnectionPool.get_connection  sz   $ 	 
	>T4<HHJJ 	> 	> 	> ""<===	> --//J	   
B&&(( A)*?@@@A#W- B B B%%'''""$$$&&(( B)*@AAAB BB
  	 	 	LL$$$	
 s:   !: A.D
 #B' &D
 'ADD
 DD
 
!D+c                    |                                   |                     |          s0|                                 | j                            d           dS 	 | j                            |           dS # t
          $ r Y dS w xY w)z)Releases the connection back to the pool.N)r@  rW  r   rr  rs  r   rY  s     rT   r>  zBlockingConnectionPool.release  s     	##J// 	
 !!###I  &&&F	I  ,,,,, 	 	 	 DD	s   A7 7
BBc                 j    |                                   | j        D ]}|                                 dS )z(Disconnects all connections in the pool.N)r@  rt  r   rY  s     rT   r   z!BlockingConnectionPool.disconnect  sC    + 	$ 	$J!!####	$ 	$r9   r^   )rV   rW   rX   rR  rW  r   ra   r2  rK  r2   rQ  r>  r   rw  rx  s   @rT   rl  rl  H  s         F #
 
 
 
 
 
   0	 	 	 _U:  
2 2 2 
2h  *$ $ $ $ $ $ $r9   rl  )dr   r   r  r  rL   r  r   abcr   	itertoolsr   queuer   r   r   r   typingr	   r
   r   r   r   r   r   r   urllib.parser   r   r   redis.cacher   r   r   r   r   r   _parsersr   r   r   r   
auth.tokenr   backoffr    credentialsr!   r"   eventr#   r$   
exceptionsr%   r&   r'   r(   r)   r*   r+   r,   r   r-   utilsr.   r/   r0   r1   r2   r3   r4   r5   r6   rI   re   rj   rg   rc   r   objectr   __annotations__r;   r=   r[   rp   r   rW  rz  r  r  r  r  r   rS  listr  r  r   r!  rl  rY   r9   rT   <module>r     sf    				  



 



                  ( ( ( ( ( ( ( ( ( (       L L L L L L L L L L L L L L L L L L L L 4 4 4 4 4 4 4 4 4 4                J I I I I I I I I I I I & & & & & &       O O O O O O O O @ @ @ @ @ @ @ @	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	      
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  NNN
	 688E,nDEF F F F !"MM M       $0 0 0 0 0 0 0 0fN N N N N N N Nb]' ]' ]' ]' ]', ]' ]' ]'@C* C* C* C* C*# C* C* C*LS S S S S. S S Slf f f f fJ f f fR    !3   > /   # !  6 6 6r ge+,,,O O O O O O O Od
g$ g$ g$ g$ g$^ g$ g$ g$ g$ g$r9   