o
    {g                     @  sh  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< d8ddZd9ddZd9ddZd:ddZ d8d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=d/d0Z&d1Z'd2ed3< d4Z(d2ed5< G d6d7 d7Z)dS )>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infreturnOptional[float]c                   C  s
   t d S N)r   get r   r   j/var/www/bot.gig.net.ua/public_html/telegram/P1/HellBot/venv/lib/python3.10/site-packages/pymongo/_csot.pyget_timeout#   s   
r   floatc                   C     t  S r   )r   r   r   r   r   r   get_rtt'      r   c                   C  r   r   )r   r   r   r   r   r   get_deadline+   r    r!   rttNonec                 C  s   t |  d S r   )r   set)r"   r   r   r   set_rtt/      r%   c                   C  s   t  sd S t t  S r   )r   r   r   time	monotonicr   r   r   r   	remaining3   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_remaining9   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__J   s   
z_TimeoutContext.__init__r   c                 C  sZ   t | j}t }| jrt | j ntd}tt||}t	d}|||f| _
| S )Nr   r   )r   r$   r/   r   r   r'   r(   r   r+   r   r0   )r1   timeout_tokenprev_deadlinenext_deadlinedeadline_token	rtt_tokenr   r   r   	__enter__N   s   
z_TimeoutContext.__enter__exc_typer   exc_valexc_tbr#   c                 C  s8   | j r| j \}}}t| t| t| d S d S r   )r0   r   resetr   r   )r1   r9   r:   r;   r3   r6   r7   r   r   r   __exit__W   s   

z_TimeoutContext.__exit__N)r,   r   )r   r.   )r9   r   r:   r   r;   r   r   r#   )__name__
__module____qualname____doc__r2   r8   r=   r   r   r   r   r.   A   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 methodsr1   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   r/   r.   r1   rE   rF   r,   rD   r   r   csot_wrapperg   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   rG   rH   rI   r   r   rJ   q   s   

 N)r1   r   rE   r   rF   r   r   r   )inspectiscoroutinefunction	functoolswrapsr   rB   )rD   rJ   r   rI   r   applyc   s   
	
rO   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)rP   rR   wcr   r   r   apply_write_concern}   s   

rZ   
   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]   ra   r1   r   r   r   r2      s   zMovingMinimum.__init__sampler   c                 C  s&   |dk rt d| | j| d S )Nr   zduration cannot be negative )
ValueErrorra   append)r1   rd   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   )lenra   r_   r+   rc   r   r   r   r      s   
zMovingMinimum.getc                 C  s   | j   d S r   )ra   clearrc   r   r   r   r<      r&   zMovingMinimum.resetN)r   r#   )rd   r   r   r#   r   r   )	r>   r?   r@   rA   __annotations__r2   rg   r   r<   r   r   r   r   r`      s   
 


r`   )r   r   rj   )r"   r   r   r#   )r*   r   r   r   )rD   rB   r   rB   )rP   rQ   rR   rS   r   r#   )*rA   
__future__r   rM   rK   r'   collectionsr   
contextlibr   contextvarsr   r   typingr   r   r	   r
   r   r   r   r   pymongo.write_concernr   r   rk   r   r   r   r   r   r!   r%   r)   r-   r.   rB   rO   rZ   r]   r_   r`   r   r   r   r   <module>   s6   (







