
    [j                       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<   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)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/app.qaans.com/backend/venv/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,   1   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_remainingr1   7   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({})
    r0   r   c                "    || _         d | _        d S r   )_timeout_tokens)selfr0   s     r   __init__z_TimeoutContext.__init__H   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'   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__L   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   )r6   r   resetr   r   )r7   r@   rA   rB   r:   r=   r>   s          r   __exit__z_TimeoutContext.__exit__U   s`    < 	!7;|4M>9MM-(((NN>***IIi     		! 	!r   N)r0   r   )r   r3   )r@   r   rA   r   rB   r   r   r%   )__name__
__module____qualname____doc__r8   r?   rE   r   r   r   r3   r3   ?   s^         a a a a   ! ! ! ! ! !r   r3   F.)boundfuncc                n     t          j                   d fd            }t          t          |          S )z/Apply the client's timeoutMS to this operation.r7   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   r5   r3   )r7   rN   rO   r0   rL   s       r   csot_wrapperzapply.<locals>.csot_wrapperd   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)r7   r   rN   r   rO   r   r   r   )	functoolswrapsr   rJ   )rL   rQ   s   ` r   applyrT   a   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)rU   rW   wcs      r   apply_write_concernr`   p   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   rc   rh   r7   s    r   r8   zMovingMinimum.__init__   s    $4555r   sampler   c                J    |dk     rd S | j                             |           d S )Nr   )rh   append)r7   rl   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   )lenrh   re   r/   rk   s    r   r   zMovingMinimum.get   s-    t| 000t|$$$sr   c                8    | j                                          d S r   )rh   clearrk   s    r   rD   zMovingMinimum.reset   s    r   N)r   r%   )rl   r   r   r%   r   r   )	rF   rG   rH   rI   __annotations__r8   ro   r   rD   r   r   r   rg   rg      sx         >>6 6 6 6$ $ $ $        r   rg   )r   r   rt   )r$   r   r   r%   )r-   r   r   r   )rL   rJ   r   rJ   )rU   rV   rW   rX   r   r%   )(rI   
__future__r   rR   r*   collectionsr   
contextlibr   contextvarsr   r   typingr   r	   r
   r   r   r   r   pymongo.write_concernr   r   ru   r   r   r   r   r!   r#   r(   r,   r1   r3   rJ   rT   r`   rc   re   rg   r   r   r   <module>r|      se   !     " " " " " "            - - - - - - ) ) ) ) ) ) ) ) P P P P P P P P P P P P P P P P P P . . . . . .'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   