
    +h1                       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<   d0dZd1dZd1dZd2dZd0dZ d3dZ! G d de          Z" eded ef         !          Z#d4d#Z$d5d(Z%d)Z&d*ed+<   d,Z'd*ed-<    G d. d/          Z(dS )6z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infreturnOptional[float]c                 6    t                               d           S N)r   get     Q/var/www/html/e360mart/e360mart_env/lib/python3.11/site-packages/pymongo/_csot.pyget_timeoutr   "   s    ;;tr   floatc                 4    t                                           S r   )r   r   r   r   r   get_rttr"   &   s    7799r   c                 4    t                                           S r   )r   r   r   r   r   get_deadliner$   *   s    <<>>r   rttNonec                :    t                               |            d S r   )r   set)r%   s    r   set_rttr)   .   s    GGCLLLLLr   c                 |    t                      sd S t                                          t          j                    z
  S r   )r   r   r   time	monotonicr   r   r   	remainingr-   2   s.    == t<<>>DN,,,,r   max_timeoutc                F    t                      }|| S t          ||           S )z4Return the remaining timeout clamped to a max value.)r-   min)r.   timeouts     r   clamp_remainingr2   8   s&    kkGw$$$r   c                  *    e Zd ZdZddZddZddZdS )_TimeoutContextzInternal timeout context manager.

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

      with pymongo.timeout(0.5):
          client.test.test.insert_one({})
    r1   r   c                "    || _         d | _        d S r   )_timeout_tokens)selfr1   s     r   __init__z_TimeoutContext.__init__I   s    \`r   r   c                p   t                               | j                  }t                                          }| j        rt          j                    | j        z   nt          d          }t                              t          ||                    }t                              d          }|||f| _
        | S )Nr   r   )r   r(   r6   r   r   r+   r,   r    r0   r   r7   )r8   timeout_tokenprev_deadlinenext_deadlinedeadline_token	rtt_tokens         r   	__enter__z_TimeoutContext.__enter__M   s    DM22 <@M[((4=88uUZ||!c-&G&GHHGGCLL	%~yAr   exc_typer	   exc_valexc_tbr&   c                    | j         r[| j         \  }}}t                              |           t                              |           t                              |           d S d S r   )r7   r   resetr   r   )r8   rA   rB   rC   r;   r>   r?   s          r   __exit__z_TimeoutContext.__exit__V   s`    < 	!7;|4M>9MM-(((NN>***IIi     		! 	!r   N)r1   r   )r   r4   )rA   r	   rB   r	   rC   r	   r   r&   )__name__
__module____qualname____doc__r9   r@   rF   r   r   r   r4   r4   @   s^         a a a a   ! ! ! ! ! !r   r4   F.)boundfuncc                n     t          j                   d fd            }t          t          |          S )z/Apply the client's timeoutMS to this operation.r8   r	   argskwargsr   c                    t                      <| j        }|3t          |          5   | g|R i |cd d d            S # 1 swxY w Y    | g|R i |S r   )r   r6   r4   )r8   rO   rP   r1   rM   s       r   csot_wrapperzapply.<locals>.csot_wrappere   s    == mG"$W-- 7 746t666v667 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7tD*4***6***s   AAA)r8   r	   rO   r	   rP   r	   r   r	   )	functoolswrapsr   rK   )rM   rR   s   ` r   applyrU   b   sJ     _T+ + + + + + <   r   cmdMutableMapping[str, Any]write_concernOptional[WriteConcern]c                    |r|j         rdS |j        }t                      |                    dd           |r|| d<   dS dS )z+Apply the given write concern to a command.NwtimeoutwriteConcern)is_server_defaultdocumentr   pop)rV   rX   wcs      r   apply_write_concernra   q   sc      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dS )MovingMinimumz4Tracks a minimum RTT within the last 10 RTT samples.zDeque[float]samplesr   r&   c                :    t          t                    | _        d S )N)maxlen)r   rd   ri   r8   s    r   r9   zMovingMinimum.__init__   s    $4555r   sampler    c                J    |dk     rd S | j                             |           d S )Nr   )ri   append)r8   rm   s     r   
add_samplezMovingMinimum.add_sample   s.    A:: FF#####r   c                h    t          | j                  t          k    rt          | j                  S dS )z7Get the min, or 0.0 if there aren't enough samples yet.r   )lenri   rf   r0   rl   s    r   r   zMovingMinimum.get   s-    t| 000t|$$$sr   c                8    | j                                          d S r   )ri   clearrl   s    r   rE   zMovingMinimum.reset   s    r   N)r   r&   )rm   r    r   r&   r   r    )	rG   rH   rI   rJ   __annotations__r9   rp   r   rE   r   r   r   rh   rh      sx         >>6 6 6 6$ $ $ $        r   rh   )r   r   ru   )r%   r    r   r&   )r.   r    r   r    )rM   rK   r   rK   )rV   rW   rX   rY   r   r&   ))rJ   
__future__r   rS   r+   collectionsr   
contextlibr   contextvarsr   r   typingr   r	   r
   r   r   r   r   r   pymongo.write_concernr   r   rv   r   r    r   r   r"   r$   r)   r-   r2   r4   rK   rU   ra   rd   rf   rh   r   r   r   <module>r}      ss   !     " " " " " "            - - - - - - ) ) ) ) ) ) ) ) _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 3222222'1z)T'J'J'J J J J J#E3777 7 7 7 7(jUU5\\JJJ J J J J            - - - -% % % %! ! ! ! !, ! ! !> GCxS)***! ! ! !
! 
! 
! 
!                    r   