o
    KDiv                      @  sl   d dl mZ d dlZd dlmZmZ d dlmZ d dlm	Z	 d dl
mZmZ er,d dlZG dd deZdS )	    )annotationsN)TYPE_CHECKINGcast)RedisBridge)ConfigurationError)AsyncRedisClientCallablec                   @  s  e Zd ZU ddiZded< 	 edLddZdMddZdNddZdNddZ	ded< ded< ded< ded< ded< ded< ded < dOdPd$d%Z
dQd&d'Z	(dRdSd.d/ZdTd0d1ZdUd2d3ZdVd5d6ZdWd9d:ZdXd>d?Z	(dRdYd@dAZ	(dRdZdBdCZd[dEdFZd\dGdHZdVdIdJZdKS )]RedispyBridgemax_connections  zdict[str, float | str | bool]DEFAULT_CLUSTER_OPTIONSreturn-type[Exception] | tuple[type[Exception], ...]c                 C  s
   | j jfS N)
dependency
RedisErrorself r   Y/var/www/Datamplify/venv/lib/python3.10/site-packages/limits/aio/storage/redis/redispy.pybase_exceptions   s   
zRedispyBridge.base_exceptionsservice_name
str | Noneuse_replicasboolsentinel_kwargs$dict[str, str | float | bool] | Noneoptionsstr | float | boolNonec                   s   g }|  } jjdd } jj|d  dD ]}|d\}	}
||	t|
f q jjr9 jjddn|}|d u rCt	d j
jj|fdi  j|pQi ii  j| _ j| _ j| _ fd	d
 _d S )N@   ,:/ z'service_name' not providedr   c                   s   | rr j S  jS r   )storage_replicastoragereadonlyr   r   r   r   <lambda>8   s   z,RedispyBridge.use_sentinel.<locals>.<lambda>)copy
parsed_urinetlocfindsplitappendintpathreplacer   r   asyncioSentinelparsed_authsentinel
master_forr'   	slave_forr&   connection_getter)r   r   r   r   r   sentinel_configurationconnection_optionsseplochostportr   r*   r   use_sentinel   s,   zRedispyBridge.use_sentinelc                   sZ   | dd  }r jjjdd|i| _n jjjj jfi | _ fdd _d S )Nconnection_poolc                       j S r   r'   _r   r   r   r+   D       z)RedispyBridge.use_basic.<locals>.<lambda>r   )popr   r5   Redisr'   from_urlurir;   )r   r   rC   r   r   r   	use_basic<   s   
zRedispyBridge.use_basicc                   s    j jdd }g } j j|d  dD ]}|d\}}| jjjj|t	|d q jjj
d	d|ii  j j| _ fdd _d S )
Nr    r!   r"   r#   )r@   rA   startup_nodesc                   rD   r   rE   rF   r   r   r   r+   T   rH   z+RedispyBridge.use_cluster.<locals>.<lambda>r   )r-   r.   r/   r0   r1   r   r5   clusterClusterNoder2   RedisClusterr   r7   r'   r;   )r   r   r>   cluster_hostsr?   r@   rA   r   r   r   use_clusterF   s   zRedispyBridge.use_clusterzredis.commands.core.Scriptlua_moving_windowlua_acquire_moving_windowlua_sliding_windowlua_acquire_sliding_windowlua_clear_keyslua_incr_expirez"Callable[[bool], AsyncRedisClient]r;   Fr)   r   c                 C  s
   |  |S r   )r;   )r   r)   r   r   r   get_connection^   s   
zRedispyBridge.get_connectionc                 C  sp   |   | j| _|   | j| _|   | j| _|   | j| _	|   | j
| _|   | j| _d S r   )rZ   register_scriptSCRIPT_MOVING_WINDOWrT   SCRIPT_ACQUIRE_MOVING_WINDOWrU   SCRIPT_CLEAR_KEYSrX   SCRIPT_INCR_EXPIRErY   SCRIPT_SLIDING_WINDOWrV   SCRIPT_ACQUIRE_SLIDING_WINDOWrW   r   r   r   r   register_scriptsa   s$   
zRedispyBridge.register_scriptsr!   keystrexpiryr2   amountc                   s*   |  |}tt| |g||gI dH S )z
        increments the counter for a given rate limit key


        :param key: the key to increment
        :param expiry: amount in seconds for the key to expire in
        :param amount: the number to increment by
        N)prefixed_keyr   r2   rY   )r   rc   re   rf   r   r   r   incrv   s   
zRedispyBridge.incrc                   s,   |  |}t| jdd|I dH pdS )zC

        :param key: the key to get the counter value for
        Tr(   Nr   )rg   r2   rZ   getr   rc   r   r   r   ri      s   
 zRedispyBridge.getc                   s$   |  |}|  |I dH  dS )z?
        :param key: the key to clear rate limits for

        N)rg   rZ   deleterj   r   r   r   clear   s   
zRedispyBridge.clear
int | Nonec                   s    t t| | dgI d H S )N*)r   r2   rX   rg   r   r   r   r   	lua_reset   s   zRedispyBridge.lua_resetlimittuple[float, int]c                   sP   |  |}t }| |g|| |gI dH }|r$t|d |d fS |dfS )z
        returns the starting point and the number of entries in the moving
        window

        :param key: rate limit key
        :param expiry: expiry of entry
        :return: (previous count, previous TTL, current count, current TTL)
        Nr   r!   )rg   timerT   float)r   rc   rp   re   	timestampwindowr   r   r   get_moving_window   s   
zRedispyBridge.get_moving_windowprevious_keycurrent_keytuple[int, float, int, float]c                   s~   |  | || |g|gI d H  }r=t|d pdtdt|d p$dd t|d p.dtdt|d p7dd fS dS )Nr   r!   r         )r           r   r|   )rV   rg   r2   maxrs   )r   rw   rx   re   ru   r   r   r   get_sliding_window   s   z RedispyBridge.get_sliding_windowc                   s8   |  |}t }| |g||||gI dH }t|S )z
        :param key: rate limit key to acquire an entry in
        :param limit: amount of entries allowed
        :param expiry: expiry of the entry

        N)rg   rr   rU   r   )r   rc   rp   re   rf   rt   acquiredr   r   r   acquire_entry   s   

zRedispyBridge.acquire_entryc                   s:   |  |}|  |}| ||g|||gI d H }t|S r   )rg   rW   r   )r   rw   rx   rp   re   rf   r   r   r   r   acquire_sliding_window_entry   s   


z*RedispyBridge.acquire_sliding_window_entryrs   c                   s.   |  |}t|  |I dH dt  S )z;
        :param key: the key to get the expiry for
        Nr   )rg   r}   rZ   ttlrr   rj   r   r   r   
get_expiry   s   
"zRedispyBridge.get_expiryc                   s(   z|    I dH  W dS    Y dS )z-
        check if storage is healthy
        NTF)rZ   pingr   r   r   r   check   s   zRedispyBridge.checkc                   sT   |  d}| jj|| jjjjjdI d H }d}|D ]}|| j|I d H 7 }q|S )Nrn   )target_nodesr   )	rg   r'   keysr   r5   rO   rQ   	ALL_NODESrk   )r   prefixr   countrc   r   r   r   reset   s   
zRedispyBridge.resetN)r   r   )
r   r   r   r   r   r   r   r   r   r   )r   r   r   r   )F)r)   r   r   r   )r   r   )r!   )rc   rd   re   r2   rf   r2   r   r2   )rc   rd   r   r2   )rc   rd   r   r   )r   rm   )rc   rd   rp   r2   re   r2   r   rq   )rw   rd   rx   rd   re   r2   r   ry   )
rc   rd   rp   r2   re   r2   rf   r2   r   r   )rw   rd   rx   rd   rp   r2   re   r2   rf   r2   r   r   )rc   rd   r   rs   )r   r   )__name__
__module____qualname__r   __annotations__propertyr   rB   rM   rS   rZ   rb   rh   ri   rl   ro   rv   r~   r   r   r   r   r   r   r   r   r   r	      s@   
 

$




	




r	   )
__future__r   rr   typingr   r   limits.aio.storage.redis.bridger   limits.errorsr   limits.typingr   r   redis.commandsredisr	   r   r   r   r   <module>   s    