o
    C©Ìh  ã                   @  sv  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	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   ú\/var/www/html/Moveonwheels/move_on_wheels/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 )Ú_TimeoutContextz�Internal 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   (








