
    _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<   d1dZd2dZd3dZd3dZ d4dZ!d2dZ"d5dZ# G d de	e                   Z$ ed ed!ef         "          Z%d6d$Z&d7d)Z'd*Z(d+ed,<   d-Z)d+ed.<    G d/ d0          Z*dS )8z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Nonec                     t                               d            t                              d           t                              t	          d                     d S )Nr   r   )r   setr   r   float     0D:\Nbitz\venv\Lib\site-packages\pymongo/_csot.py	reset_allr   #   sA    KKGGCLLLLLur   Optional[float]c                 6    t                               d           S N)r   getr   r   r   get_timeoutr$   )   s    ;;tr   r   c                 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(   1   s    <<>>r   rttc                :    t                               |            d S r"   )r   r   )r)   s    r   set_rttr+   5   s    GGCLLLLLr   c                 |    t                      sd S t                                          t          j                    z
  S r"   )r$   r   r#   time	monotonicr   r   r   	remainingr/   9   s.    == t<<>>DN,,,,r   max_timeoutc                F    t                      }|| S t          ||           S )z4Return the remaining timeout clamped to a max value.)r/   min)r0   timeouts     r   clamp_remainingr4   ?   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({})
    r3   r    c                "    || _         d | _        d S r"   )_timeout_tokens)selfr3   s     r   __init__z_TimeoutContext.__init__P   s    \`r   r   r   c                p   t                               | j                  }t                                          }| j        rt          j                    | j        z   nt          d          }t                              t          ||                    }t                              d          }|||f| _
        d S )Nr   r   )r   r   r8   r   r#   r-   r.   r   r2   r   r9   )r:   timeout_tokenprev_deadlinenext_deadlinedeadline_token	rtt_tokens         r   	__enter__z_TimeoutContext.__enter__T   s    DM22 <@M[((4=88uUZ||!c-&G&GHHGGCLL	%~yAr   exc_typer	   exc_valexc_tbc                    | j         r[| j         \  }}}t                              |           t                              |           t                              |           d S d S r"   )r9   r   resetr   r   )r:   rC   rD   rE   r=   r@   rA   s          r   __exit__z_TimeoutContext.__exit__\   s`    < 	!7;|4M>9MM-(((NN>***IIi     		! 	!r   N)r3   r    r   r   )rC   r	   rD   r	   rE   r	   r   r   )__name__
__module____qualname____doc__r;   rB   rH   r   r   r   r6   r6   G   sb         a a a aB B B B! ! ! ! ! !r   r6   F.)boundfuncc                     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 methodsr:   r	   argskwargsr   c                   K   t                      B| j        }|9t          |          5   | g|R i | d {V cd d d            S # 1 swxY w Y    | g|R i | d {V S r"   r$   r8   r6   r:   rR   rS   r3   rP   s       r   csot_wrapperzapply.<locals>.csot_wrapperl   s      }}$-&(11 A A%)T$%@%@%@%@%@%@@@@@@@A A A A A A A A A A A A A A A Ad4T444V444444444s   A		AA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"   rU   rV   s       r   rW   zapply.<locals>.csot_wrapperv   s    }}$-&(11 ; ;#tD:4:::6::; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;4.t...v...s   AAA)r:   r	   rR   r	   rS   r	   r   r	   )inspectiscoroutinefunction	functoolswrapsr   rN   )rP   rW   s   ` r   applyr]   h   s    "4(( /				5 	5 	5 	5 	5 
		5 	5 
			/ 	/ 	/ 	/ 	/ 
		/ <   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)r^   r`   wcs      r   apply_write_concernri      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   rl   rq   r:   s    r   r;   zMovingMinimum.__init__   s    $4555r   sampler   c                j    |dk     rt          d|           | j                            |           d S )Nr   zduration cannot be negative )
ValueErrorrq   append)r:   ru   s     r   
add_samplezMovingMinimum.add_sample   s>    A::DFDDEEE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   )lenrq   rn   r2   rt   s    r   r#   zMovingMinimum.get   s-    t| 000t|$$$sr   c                8    | j                                          d S r"   )rq   clearrt   s    r   rG   zMovingMinimum.reset   s    r   NrI   )ru   r   r   r   r   r   )	rJ   rK   rL   rM   __annotations__r;   ry   r#   rG   r   r   r   rp   rp      sx         >>6 6 6 6$ $ $ $
        r   rp   rI   )r   r    r~   )r)   r   r   r   )r0   r   r   r   )rP   rN   r   rN   )r^   r_   r`   ra   r   r   )+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/   r4   r6   rN   r]   ri   rl   rn   rp   r   r   r   <module>r      s   !     " " " " " "             - - - - - - ) ) ) ) ) ) ) ) _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 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               - - - -% % % %! ! ! ! !,S1 ! ! !< GCxS)***! ! ! !4
! 
! 
! 
!                    r   