o
    ri                      @  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   _/var/www/html/pca-backend/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  jjr jjddn|}|d u r#td jjj	|fdi  j
|p1i 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>4   s   z,RedispyBridge.use_sentinel.<locals>.<lambda>)copyextendoptions_from_uri	locationspathreplacer   r   asyncioSentinelparsed_authsentinel
master_forr#   	slave_forr"   connection_getter)r   r   r   r   r   sentinel_configurationconnection_optionsr   r&   r   use_sentinel   s&   zRedispyBridge.use_sentinelc                   sz   | dd  }r jjjdd|i| _n jjr& jjj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'   E       z)RedispyBridge.use_basic.<locals>.<lambda>r   )
popr   r.   Redisr#   r*   emptyfrom_urlurir4   )r   r   r8   r   r   r   	use_basic8   s   

zRedispyBridge.use_basicc                   st   g } j jD ]\}}| jjjj|t|d q jjjdi i  j	d|i j
| _ fdd _d S )N)hostportstartup_nodesc                   r9   r   r:   r;   r   r   r   r'   W   r=   z+RedispyBridge.use_cluster.<locals>.<lambda>r   )r*   r+   appendr   r.   clusterClusterNodeintRedisClusterr   r0   r#   r4   )r   r   cluster_hostsrD   rE   r   r   r   use_clusterG   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]r4   Fr%   r   c                 C  s
   |  |S r   )r4   )r   r%   r   r   r   get_connectiona   s   
zRedispyBridge.get_connectionc                 C  sp   |   | j| _|   | j| _|   | j| _|   | j| _	|   | j
| _|   | j| _d S r   )rT   register_scriptSCRIPT_MOVING_WINDOWrN   SCRIPT_ACQUIRE_MOVING_WINDOWrO   SCRIPT_CLEAR_KEYSrR   SCRIPT_INCR_EXPIRErS   SCRIPT_SLIDING_WINDOWrP   SCRIPT_ACQUIRE_SLIDING_WINDOWrQ   r   r   r   r   register_scriptsd   s$   
zRedispyBridge.register_scripts   keystrexpiryrJ   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   rJ   rS   )r   r^   r`   ra   r   r   r   incry   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   )rb   rJ   rT   getr   r^   r   r   r   rd      s   
 zRedispyBridge.getc                   s$   |  |}|  |I dH  dS )z?
        :param key: the key to clear rate limits for

        N)rb   rT   deletere   r   r   r   clear   s   
zRedispyBridge.clear
int | Nonec                   s    t t| | dgI d H S )N*)r   rJ   rR   rb   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]   )rb   timerN   float)r   r^   rk   r`   	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   rw   )rP   rb   rJ   maxrn   )r   rr   rs   r`   rp   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)rb   rm   rO   r   )r   r^   rk   r`   ra   ro   acquiredr   r   r   acquire_entry   s   

zRedispyBridge.acquire_entryc                   s:   |  |}|  |}| ||g|||gI d H }t|S r   )rb   rQ   r   )r   rr   rs   rk   r`   ra   rz   r   r   r   acquire_sliding_window_entry   s   


z*RedispyBridge.acquire_sliding_window_entryrn   c                   s.   |  |}t|  |I dH dt  S )z;
        :param key: the key to get the expiry for
        Nr   )rb   rx   rT   ttlrm   re   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)rT   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 )Nri   )target_nodesr   )	rb   r#   keysr   r.   rH   rK   	ALL_NODESrf   )r   prefixr   countr^   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]   )r^   r_   r`   rJ   ra   rJ   r   rJ   )r^   r_   r   rJ   )r^   r_   r   r   )r   rh   )r^   r_   rk   rJ   r`   rJ   r   rl   )rr   r_   rs   r_   r`   rJ   r   rt   )
r^   r_   rk   rJ   r`   rJ   ra   rJ   r   r   )rr   r_   rs   r_   rk   rJ   r`   rJ   ra   rJ   r   r   )r^   r_   r   rn   )r   r   )__name__
__module____qualname__r   __annotations__propertyr   r7   rC   rM   rT   r\   rc   rd   rg   rj   rq   ry   r{   r|   r~   r   r   r   r   r   r   r	      s@   
 

 



	




r	   )
__future__r   rm   typingr   r   limits.aio.storage.redis.bridger   limits.errorsr   limits.typingr   r   redis.commandsredisr	   r   r   r   r   <module>   s    