
    gh i                       U d Z ddlmZ ddl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"d*dZ# G d de	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        t        j                  d       t        j                  t	        d             y )Nr   r   )r   setr   r   float     I/opt/movilizia/ApiFast/venv/lib/python3.12/site-packages/pymongo/_csot.py	reset_allr   #   s)    KKGGCLLLur   c                 ,    t         j                  d       S N)r   getr   r   r   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%   1   s    <<>r   c                .    t         j                  |        y r   )r   r   )rtts    r   set_rttr(   5   s    GGCLr   c                 j    t               sy t        j                         t        j                         z
  S r   )r!   r   r    time	monotonicr   r   r   	remainingr,   9   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_remainingr1   ?   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)selfr0   s     r   __init__z_TimeoutContext.__init__P   s    \`r   c                V   t         j                  | j                        }t        j	                         }| j                  r!t        j                         | j                  z   n
t        d      }t        j                  t        ||            }t        j                  d      }|||f| _
        y )Nr   r   )r   r   r5   r   r    r*   r+   r   r.   r   r6   )r7   timeout_tokenprev_deadlinenext_deadlinedeadline_token	rtt_tokens         r   	__enter__z_TimeoutContext.__enter__T   sq    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   )r6   r   resetr   r   )r7   exc_typeexc_valexc_tbr:   r=   r>   s          r   __exit__z_TimeoutContext.__exit__\   sB    <<7;||4M>9MM-(NN>*IIi 	 r   N)r0   Optional[float]returnNone)rB   r	   rC   r	   rD   r	   rH   rI   )__name__
__module____qualname____doc__r8   r?   rE   r   r   r   r3   r3   G   s    aB!r   r3   F.)boundc                     t        j                         rt        j                         d fd       }nt        j                         d fd       }t	        t
        |      S )zbApply the client's timeoutMS to this operation. Can wrap both asynchronous and synchronous methodsc                   K   t               7| j                  }|)t        |      5   | g|i | d {   cd d d        S  | g|i | d {   S 7 !# 1 sw Y   xY w7 wr   r!   r5   r3   r7   argskwargsr0   funcs       r   csot_wrapperzapply.<locals>.csot_wrapperl   s|     }$--&(1 A%)$%@%@%@@A Ad4T4V444  AA A4s7   $A)AAAA)A'A)AA$ A)c                    t               /| j                  }|!t        |      5   | g|i |cd d d        S  | g|i |S # 1 sw Y   xY wr   rR   rS   s       r   rW   zapply.<locals>.csot_wrapperv   sc    }$--&(1 ;#D:4:6:; ;.t.v..; ;s   AA)r7   r	   rT   r	   rU   r	   rH   r	   )inspectiscoroutinefunction	functoolswrapsr   rN   )rV   rW   s   ` r   applyr]   h   sX    ""4(			5 
	5 
		/ 
	/ <  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_concernrg      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   rj   ro   r7   s    r   r8   zMovingMinimum.__init__   s    $45r   c                `    |dk  rt        d|       | j                  j                  |       y )Nr   zduration cannot be negative )
ValueErrorro   append)r7   samples     r   
add_samplezMovingMinimum.add_sample   s.    A:;F8DEE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   )lenro   rl   r.   rr   s    r   r    zMovingMinimum.get   s&    t|| 00t||$$r   c                8    | j                   j                          y r   )ro   clearrr   s    r   rA   zMovingMinimum.reset   s    r   NrG   )rv   r   rH   rI   rH   r   )	rJ   rK   rL   rM   __annotations__r8   rw   r    rA   r   r   r   rn   rn      s    >6$
r   rn   rG   )rH   rF   r|   )r'   r   rH   rI   )r/   r   rH   r   )rV   rN   rH   rN   )rd   zMutableMapping[str, Any]re   zOptional[WriteConcern]rH   rI   )+rM   
__future__r   r[   rY   r*   collectionsr   
contextlibr   contextvarsr   r   typingr   r	   r
   r   r   r   r   r   pymongo.write_concernr   r   r}   r   r   r   r   r!   r#   r%   r(   r,   r1   r3   rN   r]   rg   rj   rl   rn   r   r   r   <module>r      s    ! "     - ) _ _ _2'1)T'J	$ J#E37 7(U5\J
 J-%!,S1 !< CxS)*!4
!	!
!2H
!	
!  #  #  r   