
    =h                       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 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)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     ]/var/www/html/phonemate/phone_mate_backend/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(   1   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-   7   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__H   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"   r1   r   r   r&   r'   floatr*   r   r2   )r3   timeout_tokenprev_deadlinenext_deadlinedeadline_token	rtt_tokens         r   	__enter__z_TimeoutContext.__enter__L   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   )r2   r   resetr   r   )r3   exc_typeexc_valexc_tbr7   r:   r;   s          r   __exit__z_TimeoutContext.__exit__U   sB    <<7;||4M>9MM-(NN>*IIi 	 r   N)r,   Optional[float])returnr/   )r?   r   r@   r   rA   r   rD   None)__name__
__module____qualname____doc__r4   r<   rB   r   r   r   r/   r/   ?   s    a!r   r/   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   r1   r/   )r3   argskwargsr,   funcs       r   csot_wrapperzapply.<locals>.csot_wrapperd   sZ    = mmG"$W-6t6v6 .-D*4*6** .-s   AA)r3   r   rN   r   rO   r   rD   r   )	functoolswrapsr   rJ   )rP   rQ   s   ` r   applyrT   a   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^   p   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   ra   rf   r3   s    r   r4   zMovingMinimum.__init__   s    $45r   c                F    |dk  ry | j                   j                  |       y )Nr   )rf   append)r3   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   )lenrf   rc   r*   ri   s    r   r   zMovingMinimum.get   s&    t|| 00t||$$r   c                8    | j                   j                          y r   )rf   clearri   s    r   r>   zMovingMinimum.reset   s    r   N)rD   rE   )rl   r6   rD   rE   rD   r6   )	rF   rG   rH   rI   __annotations__r4   rm   r   r>   r   r   r   re   re      s    >6$r   re   )rD   rC   rr   )r#   r6   rD   rE   )r+   r6   rD   r6   )rP   rJ   rD   rJ   )r[   zMutableMapping[str, Any]r\   zOptional[WriteConcern]rD   rE   )(rI   
__future__r   rR   r&   collectionsr   
contextlibr   contextvarsr   r   typingr   r	   r
   r   r   r   r   pymongo.write_concernr   r   rs   r   r6   r   r   r   r    r$   r(   r-   r/   rJ   rT   r^   ra   rc   re   r   r   r   <module>rz      s    ! "    - ) P P P .'1)T'J	$ J#E37 7(U5\J
 J-%!, !> CxS)*!
!	!
!2H
!	
!  #  #  r   