U
    gs                     @   sr  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 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+m,Z, d	dl-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5 d	dl6m7Z7 d	dl8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@ e:rhd dlAZAdZBdZCdZDdZEdZFeG ZHeee&e'e%f  eId< e:re%ZJne&ZJG dd dZKG dd dZLG dd dZMG dd deMZNG dd deNZOG d d! d!eMZPG d"d# d#eOZQG d$d% d%eNZRd&ZSd'd( ZTeUeVeVeTeTeWeUeUeTeVd)
ZXd*d+ ZYG d,d- d-ZZG d.d/ d/eZZ[dS )0    N)abstractmethod)chain)EmptyFull	LifoQueue)time)AnyCallableDictListOptionalTypeUnion)parse_qsunquoteurlparse)
CacheEntryCacheEntryStatusCacheFactoryCacheFactoryInterfaceCacheInterfaceCacheKey   )Encoder_HiredisParser_RESP2Parser_RESP3Parser)	NoBackoff)CredentialProvider"UsernamePasswordCredentialProvider)AuthenticationError$AuthenticationWrongNumberOfArgsErrorChildDeadlockedErrorConnectionError	DataError
RedisErrorResponseErrorTimeoutError)Retry)CRYPTOGRAPHY_AVAILABLEHIREDIS_AVAILABLESSL_AVAILABLEcompare_versionsensure_stringformat_error_messageget_lib_versionstr_if_bytes   *   $s   
       DefaultParserc                   @   s   e Zd ZedddZdS )HiredisRespSerializer)argsc                 G   s   g }t |d tr4t|d   |dd  }n(d|d kr\t|d  |dd  }z|t| W n0 tk
r   t	
 \}}}t||Y nX |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)selfr7   output_value	traceback rL   4/tmp/pip-unpacked-wheel-f3sx1i9r/redis/connection.pypackE   s    "zHiredisRespSerializer.packN)__name__
__module____qualname__r   rN   rL   rL   rL   rM   r6   D   s   r6   c                   @   s"   e Zd ZddddZdd ZdS )PythonRespSerializerNreturnc                 C   s   || _ || _d S N)_buffer_cutoffr>   )rG   buffer_cutoffr>   rL   rL   rM   __init__W   s    zPythonRespSerializer.__init__c              	   G   s  g }t |d tr4t|d   |dd  }n(d|d kr\t|d  |dd  }tttt| t	f}| j
}t| j|D ]|}t|}t||ks||kst |trt|tt| t	f}|| || t	}qt|tt| t	|t	f}q|| |S r8   )r;   r<   r=   r>   r?   	SYM_EMPTYjoinSYM_STARlenSYM_CRLFrV   map
memoryview
SYM_DOLLARr@   )rG   r7   rH   ZbuffrW   argZ
arg_lengthrL   rL   rM   rN   [   s@    "





zPythonRespSerializer.pack)rO   rP   rQ   rX   rN   rL   rL   rL   rM   rR   V   s   rR   c                   @   s   e Zd Zedd Zedd Zedd Zedd Zed	d
 Zedd Z	edd Z
edd Zed%ddZedd Zed&ddZed'dddddZedd Zedd  Zeeeeeef eeef f d!d"d#Zd$S )(ConnectionInterfacec                 C   s   d S rU   rL   rG   rL   rL   rM   repr_pieces   s    zConnectionInterface.repr_piecesc                 C   s   d S rU   rL   rG   callbackrL   rL   rM   register_connect_callback   s    z-ConnectionInterface.register_connect_callbackc                 C   s   d S rU   rL   re   rL   rL   rM   deregister_connect_callback   s    z/ConnectionInterface.deregister_connect_callbackc                 C   s   d S rU   rL   rG   parser_classrL   rL   rM   
set_parser   s    zConnectionInterface.set_parserc                 C   s   d S rU   rL   rc   rL   rL   rM   connect   s    zConnectionInterface.connectc                 C   s   d S rU   rL   rc   rL   rL   rM   
on_connect   s    zConnectionInterface.on_connectc                 G   s   d S rU   rL   rG   r7   rL   rL   rM   
disconnect   s    zConnectionInterface.disconnectc                 C   s   d S rU   rL   rc   rL   rL   rM   check_health   s    z ConnectionInterface.check_healthTc                 C   s   d S rU   rL   rG   commandrp   rL   rL   rM   send_packed_command   s    z'ConnectionInterface.send_packed_commandc                 O   s   d S rU   rL   rG   r7   kwargsrL   rL   rM   send_command   s    z ConnectionInterface.send_commandr   c                 C   s   d S rU   rL   rG   timeoutrL   rL   rM   can_read   s    zConnectionInterface.can_readFdisconnect_on_errorpush_requestc                C   s   d S rU   rL   )rG   disable_decodingr{   r|   rL   rL   rM   read_response   s    z!ConnectionInterface.read_responsec                 G   s   d S rU   rL   rn   rL   rL   rM   rB      s    z ConnectionInterface.pack_commandc                 C   s   d S rU   rL   rG   commandsrL   rL   rM   pack_commands   s    z!ConnectionInterface.pack_commandsrS   c                 C   s   d S rU   rL   rc   rL   rL   rM   handshake_metadata   s    z&ConnectionInterface.handshake_metadataN)T)r   )F)rO   rP   rQ   r   rd   rg   rh   rk   rl   rm   ro   rp   rs   rv   ry   r~   rB   r   propertyr   r
   bytesr<   r   rL   rL   rL   rM   rb      sF   








 	

rb   c                   @   s  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feee	 ee
 ee
 ee	e	eeeee	 ee	 ee	 ee	 eedf eeg df  ee ee eeg df  d
ddZdd Zedd Zdd Zdd Zdd Zdd Zdd Zdd Zedd Zedd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Z d+d, Z!dBd.d/Z"d0d1 Z#dCd2d3Z$dDd-dd4d5d6Z%d7d8 Z&d9d: Z'epe	d;d<d=Z(e)ee*e+e+f e*e	e	f f d;d>d?Z,e,j-ee*e+e+f e*e	e	f f d@dAd?Z,dS )EAbstractConnectionz0Manages communication to and from a Redis serverr   NFutf-8stricti   zredis-pyr4   )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_packerc                 C   s  |s|r|dk	rt dt | _|| _|| _|| _|| _|| _|| _	|| _
|| _|dkr^|}|| _|| _|tkrvg }|r|t || _|s|r|dkrtt d| _nt|| _| j| ntt d| _|| _d| _|| _t|||	| _d| _d| _|| _|  |
 g | _!d| _"zHzt%|}W n6 t&k
rB   t'}Y n t(k
r^   t#d	Y nX W 5 |dk sx|dkrt#d|| _$X | )|| _*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  r4      zprotocol must be either 2 or 3zprotocol must be an integer)+r$   osgetpidpidr   r   r   r   r   r   r   r   r   r   SENTINELr@   r'   retry_on_errorr(   r   r   copydeepcopyZupdate_supported_errorsr   next_health_checkr   r   encoderr   _sock_socket_read_sizerk   _connect_callbacksrV   r#   r   intrC   DEFAULT_RESP_VERSION
ValueError_construct_command_packer_command_packer)rG   r   r   r   r   r   r   r   r   r   rj   r   r   r   r   r   r   r   r   r   r   r   prL   rL   rM   rX      s`    


zAbstractConnection.__init__c                 C   s8   d dd |  D }d| jj d| jj d| dS )N,c                 S   s   g | ]\}}| d | qS )=rL   ).0kvrL   rL   rM   
<listcomp>,  s     z/AbstractConnection.__repr__.<locals>.<listcomp><.()>)rZ   rd   	__class__rP   rO   )rG   Z	repr_argsrL   rL   rM   __repr__+  s    zAbstractConnection.__repr__c                 C   s   d S rU   rL   rc   rL   rL   rM   rd   /  s    zAbstractConnection.repr_piecesc                 C   s&   z|    W n tk
r    Y nX d S rU   )ro   	Exceptionrc   rL   rL   rM   __del__3  s    zAbstractConnection.__del__c                 C   s*   |d k	r|S t rt S t| j| jjS d S rU   )r*   r6   rR   rV   r   r>   )rG   packerrL   rL   rM   r   9  s
    z,AbstractConnection._construct_command_packerc                 C   s$   t |}|| jkr | j| 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   r@   )rG   rf   ZwmrL   rL   rM   rg   A  s    

z,AbstractConnection.register_connect_callbackc                 C   s0   z| j t| W n tk
r*   Y nX dS )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   re   rL   rL   rM   rh   M  s    z.AbstractConnection.deregister_connect_callbackc                 C   s   || j d| _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   _parserri   rL   rL   rM   rk   X  s    zAbstractConnection.set_parserc              
      s    j r
dS z" j fdd fdd}W nL tjk
rJ   tdY n0 tk
rx } zt |W 5 d}~X Y nX | _ z" j	dkr 
  n
 	  W n tk
r       Y nX dd  jD  _ jD ]}| }|r|  qdS )z5Connects to the Redis server if not already connectedNc                      s      S rU   )_connectrL   rc   rL   rM   <lambda>f  r3   z,AbstractConnection.connect.<locals>.<lambda>c                    s
     | S rU   ro   )errorrc   rL   rM   r   f  r3   zTimeout connecting to serverc                 S   s   g | ]}| r|qS rL   rL   )r   refrL   rL   rM   r   }  s      z.AbstractConnection.connect.<locals>.<listcomp>)r   r   call_with_retrysocketrx   r'   OSErrorr#   _error_messager   rm   r%   ro   r   )rG   socker   rf   rL   rc   rM   rl   `  s0    
 
 


zAbstractConnection.connectc                 C   s   d S rU   rL   rc   rL   rL   rM   r     s    zAbstractConnection._connectc                 C   s   d S rU   rL   rc   rL   rL   rM   _host_error  s    zAbstractConnection._host_errorc                 C   s   t |  |S rU   )r.   r   )rG   	exceptionrL   rL   rM   r     s    z!AbstractConnection._error_messagec                 C   s  | j |  | j }d}| js(| js(| jrD| jp:t| j| j}| }|r| jdkrt| j t	r~| 
t |j| j _| j |  t|dkrd|d g}| jd| jdf|  |  | _n|r*| jd|dd	i z|  }W n2 tk
r   | jd|d
 d	d |  }Y nX t|dkrtdn| jdkrt| j t	rd| 
t |j| j _| j |  | d| j |  | _| jd| jkr| jd| jkrtd| jr| dd| j t|  dkrtdz&| jr| ddd| j |   W n tk
r   Y nX z&| jr@| ddd| j |   W n tk
rX   Y nX | jr| d| j t|  dkrtddS )z=Initialize the connection, authenticate and select a databaseN)r4   2r   defaultr   ZHELLOAUTHrp   Frp   OKzInvalid Username or Passwords   protoprotozInvalid RESP versionCLIENTZSETNAMEzError setting client nameZSETINFOzLIB-NAMEzLIB-VERZSELECTzInvalid Database)r   )r   rm   r   r   r   r   Zget_credentialsr   r;   r   rk   r   ZEXCEPTION_CLASSESr\   rv   r~   r   r!   r0   r    getr#   r   r   r&   r   r   )rG   parserZ	auth_argsZcred_providerZauth_responserL   rL   rM   rm     st    





zAbstractConnection.on_connectc              	   G   s   | j   | j}d| _|dkr"dS t | jkrZz|tj W n t	t
fk
rX   Y nX z|  W n t	k
rz   Y nX dS )z!Disconnects from the Redis serverN)r   Zon_disconnectr   r   r   r   shutdownr   	SHUT_RDWRr   rC   close)rG   r7   Z	conn_sockrL   rL   rM   ro     s    
zAbstractConnection.disconnectc                 C   s*   | j ddd t|  dkr&tddS )z Send PING, expect PONG in returnZPINGFr   ZPONGz#Bad response from PING health checkN)rv   r0   r~   r#   rc   rL   rL   rM   
_send_ping  s    zAbstractConnection._send_pingc                 C   s   |    dS )z Function to call when PING failsNr   )rG   r   rL   rL   rM   _ping_failed  s    zAbstractConnection._ping_failedc                 C   s(   | j r$t | jkr$| j| j| j dS )z3Check the health of the connection with a PING/PONGN)r   r   r   r   r   r   r   rc   rL   rL   rM   rp     s    zAbstractConnection.check_healthTc              
   C   s   | j s|   |r|   z*t|tr,|g}|D ]}| j | q0W n tjk
rj   |   t	dY n t
k
r } zV|   t|jdkrd|jd  }}n|jd }|jd }td| d| dW 5 d}~X Y n tk
r   |    Y nX dS )	z2Send an already packed command to the Redis serverzTimeout writing to socketr   UNKNOWNr   zError z while writing to socket. r   N)r   rl   rp   r;   r<   sendallr   rx   ro   r'   r   r\   r7   r#   BaseException)rG   rr   rp   itemr   errnoerrmsgrL   rL   rM   rs     s,    


&z&AbstractConnection.send_packed_commandc                 O   s"   | j | jj| |ddd dS )z+Pack and send a command to the Redis serverrp   Tr   N)rs   r   rN   r   rt   rL   rL   rM   rv   *  s    

zAbstractConnection.send_commandc              
   C   sn   | j }|s|   |  }z| j|W S  tk
rh } z"|   td| d|j W 5 d}~X Y nX dS )z8Poll the socket to see if there's data that can be read.Error while reading from z: N)	r   rl   r   r   ry   r   ro   r#   r7   )rG   rx   r   
host_errorr   rL   rL   rM   ry   1  s    zAbstractConnection.can_readrz   c             
   C   s   |   }z2| jdkr*ts*| jj||d}n| jj|d}W n tjk
rj   |rX|   td| Y nd t	k
r } z&|r|   t
d| d|j W 5 d}~X Y n" tk
r   |r|    Y nX | jrt | j | _t|trz|W 5 ~X |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   rx   ro   r'   r   r#   r7   r   r   r   r   r;   r&   )rG   r}   r{   r|   r   responser   rL   rL   rM   r~   ?  s:    	 
z AbstractConnection.read_responsec                 G   s   | j j| S )r9   )r   rN   rn   rL   rL   rM   rB   m  s    zAbstractConnection.pack_commandc           	      C   s   g }g }d}| j }|D ]}| jj| D ]r}t|}||ksL||ksLt|trh|r`|t| d}g }||kszt|tr|| q&|| ||7 }q&q|r|t| |S )z.Pack multiple commands into the Redis protocolr   )	rV   r   rN   r\   r;   r_   r@   rY   rZ   )	rG   r   rH   piecesbuffer_lengthrW   cmdchunkZchunklenrL   rL   rM   r   q  s0    
z AbstractConnection.pack_commandsrS   c                 C   s   | j S rU   )r   rc   rL   rL   rM   get_protocol  s    zAbstractConnection.get_protocolc                 C   s   | j S rU   Z_handshake_metadatarc   rL   rL   rM   r     s    z%AbstractConnection.handshake_metadatarJ   c                 C   s
   || _ d S rU   r   )rG   rJ   rL   rL   rM   r     s    )T)r   )F).rO   rP   rQ   __doc__r   r5   r/   r   r   r<   floatboolr   r   r	   r   rX   r   r   rd   r   r   rg   rh   rk   rl   r   r   r   rm   ro   r   r   rp   rs   rv   ry   r~   rB   r   r   r   r
   r   r   setterrL   rL   rL   rM   r      s   
Z
#

Z

 .(r   c                       s:   e Zd ZdZd fdd	Zd	d
 Zdd Zdd Z  ZS )
Connectionz4Manages TCP communication to and from a Redis server	localhost  FNr   c                    s8   || _ t|| _|| _|pi | _|| _t jf | d S rU   )hostr   portsocket_keepalivesocket_keepalive_optionssocket_typesuperrX   )rG   r   r   r   r   r   ru   r   rL   rM   rX     s    	

zConnection.__init__c                 C   s6   d| j fd| jfd| jfg}| jr2|d| jf |S )Nr   r   r   r   )r   r   r   r   r@   rG   r   rL   rL   rM   rd     s    zConnection.repr_piecesc                 C   s  d}t | j| j| jt jD ]}|\}}}}}d}zt  |||}|t jt jd | j	r|t j
t jd | j D ]\}	}
|t j|	|
 qv|| j || || j |W   S  tk
r } z|}|dk	r|  W 5 d}~X Y qX q|dk	r|tddS )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   rl   r   r   r   )rG   errresfamilysocktyper   	canonnameZsocket_addressr   r   r   rI   rL   rL   rM   r     s6       


zConnection._connectc                 C   s   | j  d| j S )N:)r   r   rc   rL   rL   rM   r     s    zConnection._host_error)r   r   FNr   	rO   rP   rQ   r   rX   rd   r   r   __classcell__rL   rL   r   rM   r     s        )r   c                   @   s  e Zd ZdZdZdZeeej	dddZ
dd Zd	d
 Zdd Zdd Zdd Zdd Zdd Zdd Zd7ddZdd Zd8ddZd9ddd d!d"Zd#d$ Zd%d& Zeeeeef eeef f d'd(d)Zd*d+ Zd,d- Z ed.d/d0d1Z!d2d3 Z"e#eee$e#e  f  d4d5d6Z%d.S ):CacheProxyConnections   fooz7.4.0Zredis)conncache	pool_lockc                 C   s`   t  | _|| _| jj| _| jj| _| jj| _|| _|| _t	
 | _d | _d | _| | j d S rU   )r   r   r   _connr   r   r   
_pool_lock_cache	threadingLock_cache_lock_current_command_cache_keyZ_current_optionsrg   _enable_tracking_callback)rG   r  r  r  rL   rL   rM   rX     s    




zCacheProxyConnection.__init__c                 C   s
   | j  S rU   )r  rd   rc   rL   rL   rM   rd     s    z CacheProxyConnection.repr_piecesc                 C   s   | j | d S rU   )r  rg   re   rL   rL   rM   rg     s    z.CacheProxyConnection.register_connect_callbackc                 C   s   | j | d S rU   )r  rh   re   rL   rL   rM   rh     s    z0CacheProxyConnection.deregister_connect_callbackc                 C   s   | j | d S rU   )r  rk   ri   rL   rL   rM   rk     s    zCacheProxyConnection.set_parserc                 C   s   | j   | j jdd }|d kr2| j jdd }| j jdd }|d krZ| j jdd }|d ksj|d krrtdt|}t|}|| jkst|| jdkrt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  rl   r   r   r#   r-   DEFAULT_SERVER_NAMEr,   MIN_ALLOWED_VERSION)rG   Zserver_nameZ
server_verrL   rL   rM   rl     s$    
zCacheProxyConnection.connectc                 C   s   | j   d S rU   )r  rm   rc   rL   rL   rM   rm     s    zCacheProxyConnection.on_connectc              	   G   s,   | j  | j  W 5 Q R X | jj|  d S rU   )r  r  flushr  ro   rn   rL   rL   rM   ro     s    zCacheProxyConnection.disconnectc                 C   s   | j   d S rU   )r  rp   rc   rL   rL   rM   rp   #  s    z!CacheProxyConnection.check_healthTc                 C   s   | j | d S rU   )r  rs   rq   rL   rL   rM   rs   &  s    z(CacheProxyConnection.send_packed_commandc              
   O   s,  |    | j@ | jt|d ddsJd | _| jj|| W 5 Q R  d S W 5 Q R X |dd krjt	dt|d t
|dd| _| j | j| jr| j| j}|j| jkr| j  |j r|jjdd qW 5 Q R X W 5 Q R  d S | jt| j| jtj| jd W 5 Q R X | jj|| d S )	Nr   rL   )rr   Z
redis_keyskeyszCannot create cache key.Tr|   )	cache_keycache_valuestatusconnection_ref)_process_pending_invalidationsr  r  Zis_cachabler   r  r  rv   r   r   r=   r$  r  ry   r~   setr   DUMMY_CACHE_VALUEr   IN_PROGRESS)rG   r7   ru   entryrL   rL   rM   rv   +  s:     
z!CacheProxyConnection.send_commandr   c                 C   s   | j |S rU   )r  ry   rw   rL   rL   rM   ry   [  s    zCacheProxyConnection.can_readFrz   c             
   C   s   | j Z | jd k	r\| j| jd k	r\| j| jjtjkr\t| j| jj	W  5 Q R  S W 5 Q R X | j
j|||d}| j x | jd kr|W  5 Q R  S |d kr| j| jg |W  5 Q R  S | j| j}|d k	rtj|_||_	| j| W 5 Q R X |S )N)r}   r{   r|   )r  r  r  r   r#  r   r(  r   r   r"  r  r~   Zdelete_by_cache_keysZVALIDr&  )rG   r}   r{   r|   r   Zcache_entryrL   rL   rM   r~   ^  s:    
z"CacheProxyConnection.read_responsec                 G   s   | j j| S rU   )r  rB   rn   rL   rL   rM   rB     s    z!CacheProxyConnection.pack_commandc                 C   s   | j |S rU   )r  r   r   rL   rL   rM   r     s    z"CacheProxyConnection.pack_commandsrS   c                 C   s   | j jS rU   )r  r   rc   rL   rL   rM   r     s    z'CacheProxyConnection.handshake_metadatac                 C   s   | j   d S rU   )r  r   rc   rL   rL   rM   r     s    zCacheProxyConnection._connectc                 C   s   | j   d S rU   )r  r   rc   rL   rL   rM   r     s    z CacheProxyConnection._host_errorN)r  rT   c                 C   s(   | ddd |  |j| j d S )Nr   ZTRACKINGON)rv   r~   r   Zset_invalidation_push_handler_on_invalidation_callback)rG   r  rL   rL   rM   r    s    z.CacheProxyConnection._enable_tracking_callbackc                 C   s   |   r| jjdd q d S )NTr   )ry   r  r~   rc   rL   rL   rM   r%    s    z3CacheProxyConnection._process_pending_invalidations)datac              	   C   s>   | j . |d d kr | j  n| j|d  W 5 Q R X d S )Nr   )r  r  r  Zdelete_by_redis_keys)rG   r,  rL   rL   rM   r+    s    z.CacheProxyConnection._on_invalidation_callback)T)r   )F)&rO   rP   rQ   r'  r  r  rb   r   r  r  rX   rd   rg   rh   rk   rl   rm   ro   rp   rs   rv   ry   r~   rB   r   r   r   r
   r   r<   r   r   r   r  r%  r   r   r+  rL   rL   rL   rM   r    s@   
0
  )(r  c                       s6   e Zd ZdZd fdd	Z fddZd	d
 Z  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                    s   t std|| _|| _|dkr(tj}n:t|trbtjtjtj	d}||krZtd| || }|| _
|| _|| _|| _|| _|| _|	| _|
| _|| _|| _|| _|| _t jf | 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: )r+   r%   keyfilecertfilessl	CERT_NONEr;   r<   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_ciphersr   rX   )rG   Zssl_keyfileZssl_certfileZssl_cert_reqsZssl_ca_certsZssl_ca_datassl_check_hostnameZssl_ca_pathZssl_passwordr=  r>  r?  r@  rA  rB  ru   Z	CERT_REQSr   rL   rM   rX     s:    (
zSSLConnection.__init__c              	      s>   t   }z| |W S  ttfk
r8   |   Y nX dS )zN
        Wrap the socket with SSL support, handling potential errors.
        N)r   r   _wrap_socket_with_sslr   r%   r   rG   r   r   rL   rM   r     s    
zSSLConnection._connectc           
      C   s  t  }| j|_| j|_| js$| jr:|j| j| j| jd | j	dk	sX| j
dk	sX| jdk	rn|j| j	| j
| jd | jdk	r| j|_| jr|| j | jdkrtdkrtd| jr| jrtd|j|| jd}| jrvd	dl}d
dlm} | jdkr"|j|jj}|| j || j n| j}| || j! |j"|t## }|$  |%| j| j&f |'  |(  |S | jdkrtrd
dlm)} ||| j| j&| j	}	|	* r|S t+d|S )z
        Wraps the socket with SSL support.

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

        Returns:
            An SSL wrapped socket.
        )r2  r1  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),r3  create_default_contextr;  r7  verify_moder2  r1  load_cert_chainr<  r8  r:  r9  load_verify_locationsrA  minimum_versionrB  set_ciphersr=  r)   r%   r>  wrap_socketr   OpenSSLocsprJ  r?  SSLContextSSLv23_METHODuse_certificate_fileuse_privatekey_fileset_ocsp_client_callbackr@  r   r   request_ocsprl   r   do_handshaker   rK  Zis_validr#   )
rG   r   contextsslsockrS  rJ  Z
staple_ctxconrK  orL   rL   rM   rD    sp    
  
 
z#SSLConnection._wrap_socket_with_ssl)NNr.  NNFNNFFNNNN)rO   rP   rQ   r   rX   r   rD  r  rL   rL   r   rM   r-    s$                 Hr-  c                       s:   e Zd ZdZd fdd	Zdd Zdd	 Zd
d Z  ZS )UnixDomainSocketConnectionz4Manages UDS communication to and from a Redis server Nc                    s   t  jf | || _|| _d S rU   )r   rX   pathr   )rG   rc  r   ru   r   rL   rM   rX   U  s    z#UnixDomainSocketConnection.__init__c                 C   s.   d| j fd| jfg}| jr*|d| jf |S )Nrc  r   r   )rc  r   r   r@   r   rL   rL   rM   rd   Z  s    z&UnixDomainSocketConnection.repr_piecesc                 C   s\   t  t jt j}|| j z|| j W n tk
rJ   |   Y nX || j	 |S )z&Create a Unix domain socket connection)
r   AF_UNIXr   r  r   rl   rc  r   r   r   rE  rL   rL   rM   r   `  s    z#UnixDomainSocketConnection._connectc                 C   s   | j S rU   )rc  rc   rL   rL   rM   r   m  s    z&UnixDomainSocketConnection._host_error)rb  Nr  rL   rL   r   rM   ra  R  s
   ra  )0FFALSENNOc                 C   s6   | d ks| dkrd S t | tr.|  tkr.dS t| S )Nrb  F)r;   r<   upperFALSE_STRINGSr   r   rL   rL   rM   to_boolt  s
    rl  )
r   r   r   r   r   r   max_connectionsr   rC  rx   c              
   C   s  |  ds&|  ds&|  ds&tdt| } i }t| j D ]v\}}|r@t|dkr@t|d }t	|}|rz||||< W q t
tfk
r   td| dY qX q@|||< q@| jrt| j|d< | jrt| j|d	< | jd
kr| jrt| j|d< t|d< n| jr"t| j|d< | jr8t| j|d< | jrd|krztt| jdd|d< W n ttfk
r   Y nX | jdkrt|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   unixrc  connection_classr   r   r   /rb  Zrediss)
startswithr   r   r   queryr  r\   r   URL_QUERY_ARGUMENT_PARSERSr   rC   r   r   schemerc  ra  hostnamer   r   replaceAttributeErrorr-  )urlru   namerJ   r   rL   rL   rM   	parse_url  sR    


rz  c                   @   s   e Zd ZdZedd Zeddfee ee	 dddZ
eefdd	d
Zdd ZddddZddddZeddddZedddZddddZdddddZdedddZd)edd d!d"Zddd#d$Zd%dd&d'd(ZdS )*ConnectionPoola  
    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``.
    c                 K   s0   t |}d|kr|d |d< || | f |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.
        ro  )rz  update)clsrx  ru   Zurl_optionsrL   rL   rM   from_url  s
    )
zConnectionPool.from_urlN)rm  cache_factoryc                 K   s   |pd}t |tr|dk r"td|| _|| _|| _d | _|| _|dsT|dr|ddkrjt	d| jd}|d k	rt |t
std	|| _n.| jd k	r| j | _nt| jd | _|dd  |dd  t | _|   d S )
Nl        r   z,"max_connections" must be a positive integerZcache_configr  r   )r   r   z4Client caching is only supported with RESP version 3z#Cache must implement CacheInterface)r;   r   r   ro  connection_kwargsrm  r  Z_cache_factoryr   r%   r   Z	get_cacher   popr  r  
_fork_lockreset)rG   ro  rm  r  r  r  rL   rL   rM   rX     s2    





zConnectionPool.__init__rS   c              	   C   s2   dt | j dt | j dt| jf | j dS )Nr   r   r   r   )typerP   rO   reprro  r  rc   rL   rL   rM   r   4  s    0zConnectionPool.__repr__c                 C   s   | 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   rc   rL   rL   rM   r   :  s    zConnectionPool.get_protocolc                 C   s,   t  | _d| _g | _t | _t | _	d S )Nr   )
r  r  _lock_created_connections_available_connectionsr&  _in_use_connectionsr   r   r   rc   rL   rL   rM   r  B  s
    
zConnectionPool.resetc                 C   sP   | j t krL| jjdd}|s$tz| j t kr<|   W 5 | j  X d S )N   )rx   )r   r   r   r  acquirer"   releaser  )rG   ZacquiredrL   rL   rM   	_checkpidS  s    #zConnectionPool._checkpidr   )command_namerT   c              	   O   s   |    | j> z| j }W n tk
r:   |  }Y nX | j| W 5 Q R X zd|  z|	 rx| j
dkrxtdW n8 ttfk
r   |  |  |	 rtdY nX W n  tk
r   | |  Y nX |S )zGet a connection from the poolNConnection has dataConnection not ready)r  r  r  r  
IndexErrormake_connectionr  addrl   ry   r  r#   r   ro   r   r  rG   r  r  options
connectionrL   rL   rM   get_connection  s*    
zConnectionPool.get_connectionc                 C   s,   | j }t|d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   )rG   ru   rL   rL   rM   get_encoder  s    


zConnectionPool.get_encoderrb   c                 C   sT   | j | jkrtd|  j d7  _ | jdk	rFt| jf | j| j| jS | jf | jS )zCreate a new connectionzToo many connectionsr   N)r  rm  r#   r  r  ro  r  r  rc   rL   rL   rM   r    s    
  zConnectionPool.make_connection)r  rT   c              	   C   s   |    | jh z| j| W n tk
r4   Y nX | |rN| j| n$|  jd8  _|	  W 5 Q R  dS W 5 Q R X dS )z(Releases the connection back to the poolr   N)
r  r  r  r   KeyErrorowns_connectionr  r@   r  ro   rG   r  rL   rL   rM   r    s    
zConnectionPool.releasec                 C   s   |j | j kS rU   )r   r  rL   rL   rM   r    s    zConnectionPool.owns_connectionT)inuse_connectionsrT   c              	   C   sJ   |    | j2 |r$t| j| j}n| j}|D ]}|  q.W 5 Q R X 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  r  r   r  r  ro   )rG   r  connectionsr  rL   rL   rM   ro     s     zConnectionPool.disconnectc                 C   s   |    dS )z-Close the pool, disconnecting all connectionsNr   rc   rL   rL   rM   r     s    zConnectionPool.closer(   )r   rT   c                 C   s8   | j d|i | jD ]
}||_q| jD ]
}||_q(d S )Nr   )r  r|  r  r   r  )rG   r   r  rL   rL   rM   	set_retry  s
    

zConnectionPool.set_retry)T)rO   rP   rQ   r   classmethodr~  r   r   r   r   rX   r<   r   r   r  r  r  r   r  r  r  r  r   ro   r   r  rL   rL   rL   rM   r{    s,   
22/!	r{  c                       sR   e Zd ZdZddeef fdd	Zdd Zdd	 Zd
d Z	dd Z
dd Z  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                    s(   || _ || _t jf ||d| d S )N)ro  rm  )queue_classrx   r   rX   )rG   rm  rx   ro  r  r  r   rL   rM   rX     s    zBlockingConnectionPool.__init__c                 C   sN   |  | j| _z| jd  W q tk
r6   Y q:Y qX qg | _t | _d S rU   )	r  rm  pool
put_nowaitr   _connectionsr   r   r   rc   rL   rL   rM   r  #  s    zBlockingConnectionPool.resetc                 C   sD   | j dk	r&t| jf | j| j | j}n| jf | j}| j| |S )zMake a fresh connection.N)r  r  ro  r  r  r  r@   r  rL   rL   rM   r  ;  s    
  z&BlockingConnectionPool.make_connectionc              	   O   s   |    d}z| jjd| jd}W n tk
r>   tdY nX |dkrP|  }zZ|  z| rltdW n8 tt	fk
r   |
  |  | rtdY nX W n  tk
r   | |  Y nX |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)blockrx   zNo connection available.r  r  )r  r  r   rx   r   r#   r  rl   ry   r   ro   r   r  r  rL   rL   rM   r  F  s,    
z%BlockingConnectionPool.get_connectionc                 C   sT   |    | |s*|  | jd dS z| j| W n tk
rN   Y nX dS )z)Releases the connection back to the pool.N)r  r  ro   r  r  r   r  rL   rL   rM   r  z  s    
zBlockingConnectionPool.releasec                 C   s    |    | jD ]}|  qdS )z(Disconnects all connections in the pool.N)r  r  ro   r  rL   rL   rM   ro     s    
z!BlockingConnectionPool.disconnect)rO   rP   rQ   r   r   r   rX   r  r  r  r  ro   r  rL   rL   r   rM   r    s   #4r  )\r   r   r   r3  rD   r  r   abcr   	itertoolsr   queuer   r   r   r   typingr   r	   r
   r   r   r   r   urllib.parser   r   r   Zredis.cacher   r   r   r   r   r   _parsersr   r   r   r   backoffr   credentialsr   r   
exceptionsr    r!   r"   r#   r$   r%   r&   r'   r   r(   utilsr)   r*   r+   r,   r-   r.   r/   r0   rA   r[   r`   r]   rY   r   objectr   __annotations__r5   r6   rR   rb   r   r   r  r-  ra  rk  rl  r   r   listrs  rz  r{  r  rL   rL   rL   rM   <module>   s~    $ 	(
(3E   PF I *	9  0