
    FCf1                       U d Z ddlm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 ddlmZmZmZmZmZmZmZmZ erddlmZ  e
d	d
      Zded	<    e
dd
      Zded<    e
d ed      
      Zded<   d%dZd&dZd&dZd'dZd%dZ d(dZ! G d de      Z" ededef         Z#d)dZ$	 	 	 	 	 	 d*dZ%dZ&ded <   d!Z'ded"<    G d# d$      Z(y)+zInternal helpers for CSOT.    )annotationsN)deque)AbstractContextManager)
ContextVarToken)TYPE_CHECKINGAnyCallableDequeMutableMappingOptionalTypeVarcast)WriteConcernTIMEOUT)defaultzContextVar[Optional[float]]RTT        zContextVar[float]DEADLINEinfc                 ,    t         j                  d       S N)r   get     M/var/www/highfloat_scraper/venv/lib/python3.12/site-packages/pymongo/_csot.pyget_timeoutr   "   s    ;;tr   c                 *    t         j                         S r   )r   r   r   r   r   get_rttr   &   s    779r   c                 *    t         j                         S r   )r   r   r   r   r   get_deadliner!   *   s    <<>r   c                .    t         j                  |        y r   )r   set)rtts    r   set_rttr%   .   s    GGCLr   c                 j    t               sy t        j                         t        j                         z
  S r   )r   r   r   time	monotonicr   r   r   	remainingr)   2   s"    =<<>DNN,,,r   c                6    t               }|| S t        ||       S )z4Return the remaining timeout clamped to a max value.)r)   min)max_timeouttimeouts     r   clamp_remainingr.   8   s!    kGw$$r   c                  (    e Zd ZdZddZddZddZy)	_TimeoutContextzInternal timeout context manager.

    Use :func:`pymongo.timeout` instead::

      with pymongo.timeout(0.5):
          client.test.test.insert_one({})
    c                     || _         d | _        y r   )_timeout_tokens)selfr-   s     r   __init__z_TimeoutContext.__init__I   s    \`r   c                X   t         j                  | j                        }t        j	                         }| j                  r!t        j                         | j                  z   n
t        d      }t        j                  t        ||            }t        j                  d      }|||f| _
        | S )Nr   r   )r   r#   r2   r   r   r'   r(   floatr+   r   r3   )r4   timeout_tokenprev_deadlinenext_deadlinedeadline_token	rtt_tokens         r   	__enter__z_TimeoutContext.__enter__M   sv    DMM2 <@MM(4==8uUZ|!c-&GHGGCL	%~yAr   c                    | j                   rP| j                   \  }}}t        j                  |       t        j                  |       t        j                  |       y y r   )r3   r   resetr   r   )r4   exc_typeexc_valexc_tbr8   r;   r<   s          r   __exit__z_TimeoutContext.__exit__V   sB    <<7;||4M>9MM-(NN>*IIi 	 r   N)r-   Optional[float])returnr0   )r@   r	   rA   r	   rB   r	   rE   None)__name__
__module____qualname____doc__r5   r=   rC   r   r   r   r0   r0   @   s    a!r   r0   F.)boundc                `     t        j                         d fd       }t        t        |      S )z/Apply the client's timeoutMS to this operation.c                    t               /| j                  }|!t        |      5   | g|i |cd d d        S  | g|i |S # 1 sw Y   xY wr   )r   r2   r0   )r4   argskwargsr-   funcs       r   csot_wrapperzapply.<locals>.csot_wrappere   sc    = mmG"$W- 76t6v67 7D*4*6**7 7s   AA)r4   r	   rO   r	   rP   r	   rE   r	   )	functoolswrapsr   rK   )rQ   rR   s   ` r   applyrU   b   s/     __T+ + <  r   c                    |r|j                   ry|j                  }t               |j                  dd       |r|| d<   yy)z+Apply the given write concern to a command.NwtimeoutwriteConcern)is_server_defaultdocumentr   pop)cmdwrite_concernwcs      r   apply_write_concernr_   q   sE     M;;			B} 
z4 	 N 
r   
   int_MAX_RTT_SAMPLES   _MIN_RTT_SAMPLESc                  <    e Zd ZU dZded<   d	dZd
dZddZd	dZy)MovingMinimumz4Tracks a minimum RTT within the last 10 RTT samples.zDeque[float]samplesc                .    t        t              | _        y )N)maxlen)r   rb   rg   r4   s    r   r5   zMovingMinimum.__init__   s    $45r   c                F    |dk  ry | j                   j                  |       y )Nr   )rg   append)r4   samples     r   
add_samplezMovingMinimum.add_sample   s     A: F#r   c                f    t        | j                        t        k\  rt        | j                        S y)z7Get the min, or 0.0 if there aren't enough samples yet.r   )lenrg   rd   r+   rj   s    r   r   zMovingMinimum.get   s&    t|| 00t||$$r   c                8    | j                   j                          y r   )rg   clearrj   s    r   r?   zMovingMinimum.reset   s    r   N)rE   rF   )rm   r7   rE   rF   rE   r7   )	rG   rH   rI   rJ   __annotations__r5   rn   r   r?   r   r   r   rf   rf      s    >6$r   rf   )rE   rD   rs   )r$   r7   rE   rF   )r,   r7   rE   r7   )rQ   rK   rE   rK   )r\   zMutableMapping[str, Any]r]   zOptional[WriteConcern]rE   rF   ))rJ   
__future__r   rS   r'   collectionsr   
contextlibr   contextvarsr   r   typingr   r	   r
   r   r   r   r   r   pymongo.write_concernr   r   rt   r   r7   r   r   r   r!   r%   r)   r.   r0   rK   rU   r_   rb   rd   rf   r   r   r   <module>r{      s    ! "    - ) _ _ _2'1)T'J	$ J#E37 7(U5\J
 J-%!, !> CxS)*!
!	!
!2H
!	
!  #  #  r   