o
    'h                     @  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   I/var/www/html/olx_land/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   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$   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_tbc                 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__V   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   rF   rG   r,   rE   r   r   csot_wrapperf   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   rH   rI   rJ   r   r   rK   p   s   

 N)r1   r   rF   r   rG   r   r   r   )inspectiscoroutinefunction	functoolswrapsr   rC   )rE   rK   r   rJ   r   applyb   s   
	
rP   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)rQ   rS   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^   rb   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 )
ValueErrorrb   append)r1   re   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   )lenrb   r`   r+   rd   r   r   r   r      s   
zMovingMinimum.getc                 C  s   | j   d S r   )rb   clearrd   r   r   r   r<      r&   zMovingMinimum.resetNr>   )re   r   r   r#   r   r   )	r?   r@   rA   rB   __annotations__r2   rh   r   r<   r   r   r   r   ra      s   
 


ra   )r   r   rk   )r"   r   r   r#   )r*   r   r   r   )rE   rC   r   rC   )rQ   rR   rS   rT   r   r#   )*rB   
__future__r   rN   rL   r'   collectionsr   
contextlibr   contextvarsr   r   typingr   r   r	   r
   r   r   r   r   pymongo.write_concernr   r   rl   r   r   r   r   r   r!   r%   r)   r-   r.   rC   rP   r[   r^   r`   ra   r   r   r   r   <module>   s6   (







