
    nh                       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rddlmZ  ed	d
      Zded	<    edd
      Zded<    ed ed      
      Zded<   d%dZd&dZd&dZd'dZ d%dZ!d(dZ" G d de	      Z# ededef         Z$d)dZ%	 	 	 	 	 	 d*dZ&dZ'ded <   d!Z(ded"<    G d# d$      Z)y)+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infc                 ,    t         j                  d       S N)r   get     s/var/www/html/SchoolMeal/SchoolMeal/pds_admin_SchoolMeal/Backend/venv/lib/python3.12/site-packages/pymongo/_csot.pyget_timeoutr   #   s    ;;tr   c                 *    t         j                         S r   )r   r   r   r   r   get_rttr   '   s    779r   c                 *    t         j                         S r   )r   r   r   r   r   get_deadliner!   +   s    <<>r   c                .    t         j                  |        y r   )r   set)rtts    r   set_rttr%   /   s    GGCLr   c                 j    t               sy t        j                         t        j                         z
  S r   )r   r   r   time	monotonicr   r   r   	remainingr)   3   s"    =<<>DNN,,,r   c                6    t               }|| S t        ||       S )z4Return the remaining timeout clamped to a max value.)r)   min)max_timeouttimeouts     r   clamp_remainingr.   9   s!    kGw$$r   c                  (    e Zd ZdZddZddZddZy)	_TimeoutContextzInternal timeout context manager.

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

      with pymongo.timeout(0.5):
          client.test.test.insert_one({})
    c                     || _         d | _        y r   )_timeout_tokens)selfr-   s     r   __init__z_TimeoutContext.__init__J   s    \`r   c                V   t         j                  | j                        }t        j	                         }| j                  r!t        j                         | j                  z   n
t        d      }t        j                  t        ||            }t        j                  d      }|||f| _
        y )Nr   r   )r   r#   r2   r   r   r'   r(   floatr+   r   r3   )r4   timeout_tokenprev_deadlinenext_deadlinedeadline_token	rtt_tokens         r   	__enter__z_TimeoutContext.__enter__N   sq    DMM2 <@MM(4==8uUZ|!c-&GHGGCL	%~yAr   c                    | j                   rP| j                   \  }}}t        j                  |       t        j                  |       t        j                  |       y y r   )r3   r   resetr   r   )r4   exc_typeexc_valexc_tbr8   r;   r<   s          r   __exit__z_TimeoutContext.__exit__V   sB    <<7;||4M>9MM-(NN>*IIi 	 r   N)r-   Optional[float]returnNone)r@   r	   rA   r	   rB   r	   rF   rG   )__name__
__module____qualname____doc__r5   r=   rC   r   r   r   r0   r0   A   s    aB!r   r0   F.)boundc                     t        j                         rt        j                         d fd       }nt        j                         d fd       }t	        t
        |      S )zbApply the client's timeoutMS to this operation. Can wrap both asynchronous and synchronous methodsc                   K   t               7| j                  }|)t        |      5   | g|i | d {   cd d d        S  | g|i | d {   S 7 !# 1 sw Y   xY w7 wr   r   r2   r0   r4   argskwargsr-   funcs       r   csot_wrapperzapply.<locals>.csot_wrapperf   s|     }$--&(1 A%)$%@%@%@@A Ad4T4V444  AA A4s7   $A)AAAA)A'A)AA$ A)c                    t               /| j                  }|!t        |      5   | g|i |cd d d        S  | g|i |S # 1 sw Y   xY wr   rP   rQ   s       r   rU   zapply.<locals>.csot_wrapperp   sc    }$--&(1 ;#D:4:6:; ;.t.v..; ;s   AA)r4   r	   rR   r	   rS   r	   rF   r	   )inspectiscoroutinefunction	functoolswrapsr   rL   )rT   rU   s   ` r   applyr[   b   sX    ""4(			5 
	5 
		/ 
	/ <  r   c                    |r|j                   ry|j                  }t               |j                  dd       |r|| d<   yy)z+Apply the given write concern to a command.NwtimeoutwriteConcern)is_server_defaultdocumentr   pop)cmdwrite_concernwcs      r   apply_write_concernre   |   sE     M;;			B} 
z4 	 N 
r   
   int_MAX_RTT_SAMPLES   _MIN_RTT_SAMPLESc                  <    e Zd ZU dZded<   d	dZd
dZddZd	dZy)MovingMinimumz4Tracks a minimum RTT within the last 10 RTT samples.zDeque[float]samplesc                .    t        t              | _        y )N)maxlen)r   rh   rm   r4   s    r   r5   zMovingMinimum.__init__   s    $45r   c                `    |dk  rt        d|       | j                  j                  |       y )Nr   zduration cannot be negative )
ValueErrorrm   append)r4   samples     r   
add_samplezMovingMinimum.add_sample   s.    A:;F8DEEF#r   c                f    t        | j                        t        k\  rt        | j                        S y)z7Get the min, or 0.0 if there aren't enough samples yet.r   )lenrm   rj   r+   rp   s    r   r   zMovingMinimum.get   s&    t|| 00t||$$r   c                8    | j                   j                          y r   )rm   clearrp   s    r   r?   zMovingMinimum.reset   s    r   NrE   )rt   r7   rF   rG   rF   r7   )	rH   rI   rJ   rK   __annotations__r5   ru   r   r?   r   r   r   rl   rl      s    >6$
r   rl   )rF   rD   rz   )r$   r7   rF   rG   )r,   r7   rF   r7   )rT   rL   rF   rL   )rb   zMutableMapping[str, Any]rc   zOptional[WriteConcern]rF   rG   )*rK   
__future__r   rY   rW   r'   collectionsr   
contextlibr   contextvarsr   r   typingr   r	   r
   r   r   r   r   r   pymongo.write_concernr   r   r{   r   r7   r   r   r   r!   r%   r)   r.   r0   rL   r[   re   rh   rj   rl   r   r   r   <module>r      s    ! "     - ) _ _ _2'1)T'J	$ J#E37 7(U5\J
 J-%!, !< CxS)*!4
!	!
!2H
!	
!  #  #  r   