Ë
    V¼SfF  ã                   ój   — U d dl Z d dlmZmZ d dlZd„ ZdZeed<   dZ	eed<   dZ
eed	<    G d
„ d«      Zy)é    N)ÚNoReturnÚOptionalc                  ór   — t        j                  dt        «       t        j                  j	                  «       S )z†
    google.cloud.firestore_v1.rate_limiter.utcnow() is deprecated.
    Use datetime.datetime.now(datetime.timezone.utc) instead.
    zxgoogle.cloud.firestore_v1.rate_limiter.utcnow() is deprecated. Use datetime.datetime.now(datetime.timezone.utc) instead.)ÚwarningsÚwarnÚDeprecationWarningÚdatetimeÚutcnow© ó    úeC:\Users\user\Documents\project_loop\venv\Lib\site-packages\google/cloud/firestore_v1/rate_limiter.pyr
   r
      s0   € ô
 ‡M�Mð	Däôô
 ×Ñ×#Ñ#Ó%Ð%r   iô  Údefault_initial_tokensi,  Údefault_phase_lengthi@B Úmicroseconds_per_secondc                   ór   — e Zd ZdZedefdedee   defd„Zd„ Z	ddee   d	e
d
efd„Zd„ Zd
efd„Zd
efd„Zy)ÚRateLimitera  Implements 5/5/5 ramp-up via Token Bucket algorithm.

    5/5/5 is a ramp up strategy that starts with a budget of 500 operations per
    second. Additionally, every 5 minutes, the maximum budget can increase by
    50%. Thus, at 5:01 into a long bulk-writing process, the maximum budget
    becomes 750 operations per second. At 10:01, the budget becomes 1,125
    operations per second.

    The Token Bucket algorithm uses the metaphor of a bucket, or pile, or really
    any container, if we're being honest, of tokens from which a user is able
    to draw. If there are tokens available, you can do the thing. If there are not,
    you can not do the thing. Additionally, tokens replenish at a fixed rate.

    Usage:

        rate_limiter = RateLimiter()
        tokens = rate_limiter.take_tokens(20)

        if not tokens:
            queue_retry()
        else:
            for _ in range(tokens):
                my_operation()

    Args:
        initial_tokens (Optional[int]): Starting size of the budget. Defaults
            to 500.
        phase_length (Optional[int]): Number of seconds, after which, the size
            of the budget can increase by 50%. Such an increase will happen every
            [phase_length] seconds if operation requests continue consistently.
    NÚinitial_tokensÚglobal_max_tokensÚphase_lengthc                 ó4  — d| _         || _        d | _        d | _        || _        | j                  | _        | j                  �Jt        | j                  | j                  «      | _        t        | j
                  | j                  «      | _        || _        d| _        y )Nr   )	Ú_operations_this_phaseÚ_global_max_tokensÚ_startÚ_last_refillÚ_available_tokensÚ_maximum_tokensÚminÚ_phase_lengthÚ_phase)Úselfr   r   r   s       r   Ú__init__zRateLimiter.__init__G   s™   € ð ,-ˆÔ#ð
 #4ˆÔà37ˆŒØ9=ˆÔð '5ˆÔð %)×$:Ñ$:ˆÔà×"Ñ"Ð.Ü%(Ø×&Ñ&¨×(?Ñ(?ó&ˆDÔ"ô $' t×';Ñ';¸T×=TÑ=TÓ#UˆDÔ ð #/ˆÔð ˆ�r   c                 óÆ   — t         j                   j                  t         j                  j                  «      }| j                  xs || _        | j
                  xs || _        y )N)r	   ÚnowÚtimezoneÚutcr   r   )r    r
   s     r   Ú_start_clockzRateLimiter._start_clockl   sG   € Ü×"Ñ"×&Ñ&¤x×'8Ñ'8×'<Ñ'<Ó=ˆØ—k‘kÒ+ VˆŒØ ×-Ñ-Ò7°ˆÕr   ÚnumÚ
allow_lessÚreturnc                 ó  — | j                  «        | j                  «        | j                  «        |rdn|}| j                  |k\  rBt	        | j                  |«      }| xj                  |z  c_        | xj
                  |z  c_        |S y)zCReturns the number of available tokens, up to the amount requested.é   r   )r&   Ú_check_phaseÚ_refillr   r   r   )r    r'   r(   Úminimum_tokensÚ_num_to_takes        r   Útake_tokenszRateLimiter.take_tokensq   sw   € à×ÑÔØ×ÑÔØ�‰Œá(™¨cˆà×!Ñ! ^Ò3Ü˜t×5Ñ5°sÓ;ˆLØ×"Ò" lÑ2Õ"Ø×'Ò'¨<Ñ7Õ'ØÐØr   c                 óp  — t         j                   j                  t         j                  j                  «      | j                  z
  }|j
                  | j                  z  }|| j                  k(  ry| j                  }d| _        | j                  }|| _        |r!| j                  |kD  r| j                  «        yyy)ac  Increments or decrements [_phase] depending on traffic.

        Every [_phase_length] seconds, if > 50% of available traffic was used
        during the window, increases [_phase], otherwise, decreases [_phase].

        This is a no-op unless a new [_phase_length] number of seconds since the
        start was crossed since it was last called.
        Nr   )
r	   r#   r$   r%   r   Úsecondsr   r   r   Ú_increase_maximum_tokens)r    ÚageÚexpected_phaseÚoperations_last_phaseÚprevious_phases        r   r,   zRateLimiter._check_phase€   s¢   € ô ×Ñ×!Ñ!¤(×"3Ñ"3×"7Ñ"7Ó8¸4¿;¹;ÑFð 	ð "Ÿk™k¨T×-?Ñ-?Ñ?ˆð ˜TŸ[™[Ò(Øà%)×%@Ñ%@ÐØ&'ˆÔ#à"Ÿk™kˆØ$ˆŒñ ! T§[¡[°>Ò%AØ×)Ñ)Õ+ð &BÐ r   c                 ó¢   — t        | j                  dz  «      | _        | j                  �&t        | j                  | j                  «      | _        y y )Ng      ø?)Úroundr   r   r   )r    s    r   r3   z$RateLimiter._increase_maximum_tokens    sF   € Ü$ T×%9Ñ%9¸CÑ%?Ó@ˆÔØ×"Ñ"Ð.Ü#& t×';Ñ';¸T×=TÑ=TÓ#UˆDÕ ð /r   c                 óŒ  — t         j                   j                  t         j                  j                  «      }|| j                  z
  }|r||| _        |j
                  dk\  r| j                  | _        y|j                  t        z  }t        || j                  z  «      }t        | j                  | j                  |z   «      | _        yy)zUReplenishes any tokens that should have regenerated since the last
        operation.r+   N)r	   r#   r$   r%   r   r2   r   r   Úmicrosecondsr   r9   r   )r    r#   Útime_since_last_refillÚ_percent_of_maxÚ
new_tokenss        r   r-   zRateLimiter._refill¥   s¸   € ô "*×!2Ñ!2×!6Ñ!6´x×7HÑ7H×7LÑ7LÓ!MˆØ58¸4×;LÑ;LÑ5LÐá!Ø #ˆDÔð &×-Ñ-°Ò2Ø)-×)=Ñ)=�Õ&ð +×7Ñ7Ô:QÑQð  ô #(¨¸$×:NÑ:NÑ(NÓ"O�
ô *-Ø×(Ñ(Ø×*Ñ*¨ZÑ7ó*�Õ&ð# "r   )r+   F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   Úintr   r!   r&   Úboolr0   r,   r   r3   r-   r   r   r   r   r   &   s|   „ ñðD 5Ø+/Ø0ñ	#àð#ð $ C™=ð#ð ó	#òJ8ñ
˜x¨™}ð ¸dð Èsó ò,ð@V¨(ó Vð
˜ô r   r   )r	   Útypingr   r   r   r
   r   rC   Ú__annotations__r   r   r   r   r   r   ú<module>rG      sD   ðô ß %Û ò
&ð "Ð ˜Ó !Ø"Ð �cÓ "Ø&Ð ˜Ó &÷Yò Yr   