o
    8Bi                     @  sr  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rEddlmZ ed	dd
Zded	< eddd
Zded< ededd
Zded< d:ddZd;ddZd<ddZd<ddZ d=ddZ!d;d d!Z"d>d#d$Z#G d%d& d&e	Z$ed'ed(ef d)Z%d?d+d,Z&d@d1d2Z'd3Z(d4ed5< d6Z)d4ed7< G d8d9 d9Z*dS )Az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                   C  s&   t d  td ttd d S )Nr   r   )r   setr   r   float r   r   M/var/www/html/Trade-python/venv/lib/python3.10/site-packages/pymongo/_csot.py	reset_all#   s   

r   Optional[float]c                   C  s
   t d S N)r   getr   r   r   r   get_timeout)   s   
r    r   c                   C     t  S r   )r   r   r   r   r   r   get_rtt-      r"   c                   C  r!   r   )r   r   r   r   r   r   get_deadline1   r#   r$   rttc                 C  s   t |  d S r   )r   r   )r%   r   r   r   set_rtt5      r&   c                   C  s   t  sd S t t  S r   )r    r   r   time	monotonicr   r   r   r   	remaining9   s   r*   max_timeoutc                 C  s   t  }|du r	| S t|| S )z4Return the remaining timeout clamped to a max value.N)r*   min)r+   timeoutr   r   r   clamp_remaining?   s   
r.   c                   @  s.   e Zd ZdZdddZddd	ZdddZdS )_TimeoutContextzInternal timeout context manager.

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

      with pymongo.timeout(0.5):
          client.test.test.insert_one({})
    r-   r   c                 C  s   || _ d | _d S r   )_timeout_tokens)selfr-   r   r   r   __init__P   s   
z_TimeoutContext.__init__r   r   c                 C  sZ   t | j}t }| jrt | j ntd}tt||}t	d}|||f| _
d S )Nr   r   )r   r   r0   r   r   r(   r)   r   r,   r   r1   )r2   timeout_tokenprev_deadlinenext_deadlinedeadline_token	rtt_tokenr   r   r   	__enter__T   s   
z_TimeoutContext.__enter__exc_typer   exc_valexc_tbc                 C  s8   | j r| j \}}}t| t| t| d S d S r   )r1   r   resetr   r   )r2   r:   r;   r<   r4   r7   r8   r   r   r   __exit__\   s   

z_TimeoutContext.__exit__N)r-   r   r   r   )r:   r   r;   r   r<   r   r   r   )__name__
__module____qualname____doc__r3   r9   r>   r   r   r   r   r/   G   s
    

r/   F.)boundfuncc                   sF   t  rt d
 fdd}nt d
 fdd}tt|S )zbApply the client's timeoutMS to this operation. Can wrap both asynchronous and synchronous methodsr2   r   argskwargsr   c                   sz   t  d u r/| j}|d ur/t|  | g|R i |I d H W  d    S 1 s*w   Y   | g|R i |I d H S r   r    r0   r/   r2   rG   rH   r-   rF   r   r   csot_wrapperl   s   

 zapply.<locals>.csot_wrapperc                   sl   t  d u r+| j}|d ur+t|  | g|R i |W  d    S 1 s&w   Y   | g|R i |S r   rI   rJ   rK   r   r   rL   v   s   

 N)r2   r   rG   r   rH   r   r   r   )inspectiscoroutinefunction	functoolswrapsr   rD   )rF   rL   r   rK   r   applyh   s   
	
rQ   cmdMutableMapping[str, Any]write_concernOptional[WriteConcern]c                 C  s>   |r|j rdS |j}t dur|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)rR   rT   wcr   r   r   apply_write_concern   s   

r\   
   int_MAX_RTT_SAMPLES   _MIN_RTT_SAMPLESc                   @  sB   e Zd ZU dZded< dddZdd
dZdddZdddZdS )MovingMinimumz4Tracks a minimum RTT within the last 10 RTT samples.zDeque[float]samplesr   r   c                 C  s   t td| _d S )N)maxlen)r   r_   rc   r2   r   r   r   r3      s   zMovingMinimum.__init__sampler   c                 C  s&   |dk rt d| | j| d S )Nr   zduration cannot be negative )
ValueErrorrc   append)r2   rf   r   r   r   
add_sample   s   zMovingMinimum.add_samplec                 C  s   t | jtkrt| jS dS )z7Get the min, or 0.0 if there aren't enough samples yet.r   )lenrc   ra   r,   re   r   r   r   r      s   
zMovingMinimum.getc                 C  s   | j   d S r   )rc   clearre   r   r   r   r=      r'   zMovingMinimum.resetNr?   )rf   r   r   r   r   r   )	r@   rA   rB   rC   __annotations__r3   ri   r   r=   r   r   r   r   rb      s   
 


rb   r?   )r   r   rl   )r%   r   r   r   )r+   r   r   r   )rF   rD   r   rD   )rR   rS   rT   rU   r   r   )+rC   
__future__r   rO   rM   r(   collectionsr   
contextlibr   contextvarsr   r   typingr   r   r	   r
   r   r   r   r   pymongo.write_concernr   r   rm   r   r   r   r   r    r"   r$   r&   r*   r.   r/   rD   rQ   r\   r_   ra   rb   r   r   r   r   <module>   s8   (








