o

����S�b����������������������@���s^��U�d�Z�ddlZddlZddlmZmZ�ddlmZmZm	Z	m
Z
mZmZm
Z
�ddlmZ�eddd�Zee
e��ed<�edd	d�Zee�ed<�ed
ed�d�Zee�ed
<�de
e�fd
d�Zdefdd�Zdefdd�Zdeddfdd�Zde
e�fdd�Zdedefdd�ZG�dd��de�Zededef�d�Zd edefd!d"�Zd#e	d$e
e�ddfd%d&�ZdS�)'zInternal helpers for CSOT.�����N)�
ContextVar�Token)�Any�Callable�MutableMapping�Optional�Tuple�TypeVar�cast)�WriteConcern�TIMEOUT)�default�RTT����������DEADLINE�inf�returnc�������������������C���s
���t��d��S��N)r����get��r���r����4/tmp/pip-target-onvjaxws/lib/python/pymongo/_csot.py�get_timeout���s���
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_deadline%���r���r����rttc�����������������C���s���t��|���d�S�r���)r����set)r���r���r���r����set_rtt)���s���r���c�������������������C���s���t���sd�S�t���t����S�r���)r���r���r����time�	monotonicr���r���r���r����	remaining-���s���r!����max_timeoutc�����������������C���s���t���}|du�r	|�S�t||��S�)z4Return the remaining timeout clamped to a max value.N)r!����min)r"����timeoutr���r���r����clamp_remaining3���s���
r%���c�������������������@���s6���e�Zd�ZdZdZdee�fdd�Zdd��Zdd	��Z	d
S�)�_TimeoutContextz�Internal timeout context manager.

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

      with pymongo.timeout(0.5):
          client.test.test.insert_one({})
    ��_timeout�_tokensr$���c�����������������C���s���||�_�d�|�_d�S�r���r'���)�selfr$���r���r���r����__init__F���s���
z_TimeoutContext.__init__c�����������������C���sZ���t��|�j�}t���}|�jrt���|�j�ntd�}t�t||��}t	�d�}|||f|�_
|�S�)Nr���r���)r���r���r(���r���r���r���r ����floatr#���r���r)���)r*����
timeout_tokenZ
prev_deadlineZ
next_deadline�deadline_token�	rtt_tokenr���r���r����	__enter__J���s���
z_TimeoutContext.__enter__c�����������������C���s8���|�j�r|�j�\}}}t�|��t�|��t�|��d�S�d�S�r���)r)���r����resetr���r���)r*����exc_type�exc_val�exc_tbr-���r.���r/���r���r���r����__exit__S���s���

�z_TimeoutContext.__exit__N)
�__name__�
__module__�__qualname__�__doc__�	__slots__r���r,���r+���r0���r5���r���r���r���r���r&���;���s����	r&����F.)�bound�funcc��������������������s ���t�������fdd��}tt|�S�)z/Apply the client's timeoutMS to this operation.c��������������������sl���t���d�u�r+|�j}|d�ur+t|�����|�g|�R�i�|��W��d�����S�1�s&w���Y����|�g|�R�i�|��S�r���)r���r(���r&���)r*����args�kwargsr$����r=���r���r����csot_wrapperb���s���

 �zapply.<locals>.csot_wrapper)�	functools�wrapsr
���r;���)r=���rA���r���r@���r����apply_���s���
rD����cmd�
write_concernc�����������������C���s>���|r|j�rdS�|j}t��dur|�dd��|r||�d<�dS�dS�)z+Apply the given write concern to a command.NZwtimeoutZwriteConcern)Zis_server_defaultZdocumentr����pop)rE���rF����wcr���r���r����apply_write_concernn���s���

�rI���) r9���rB���r����contextvarsr���r����typingr���r���r���r���r���r	���r
���Zpymongo.write_concernr���r���r,����__annotations__r���r���r���r���r���r���r!���r%����objectr&���r;���rD���rI���r���r���r���r����<module>���s&���$!