o Sb @s^UdZddlZddlZddlmZmZddlmZmZm Z m Z m Z m Z m Z ddlmZedddZee eed<edd dZeeed<ed ed dZeeed <d e efd dZd efddZd efddZded dfddZd e efddZded efddZGdddeZe dedefdZd ed efd!d"Zd#e d$e ed dfd%d&ZdS)'zInternal helpers for CSOT.N) ContextVarToken)AnyCallableMutableMappingOptionalTupleTypeVarcast) WriteConcernTIMEOUT)defaultRTTDEADLINEinfreturncCs tdSN)r getrr4/tmp/pip-target-onvjaxws/lib/python/pymongo/_csot.py get_timeouts rcCtSr)rrrrrrget_rtt!rcCrr)rrrrrr get_deadline%rrrttcCst|dSr)rset)rrrrset_rtt)srcCstsdSttSr)rrrtime monotonicrrrr remaining-sr! max_timeoutcCst}|dur |St||S)z4Return the remaining timeout clamped to a max value.N)r!min)r"timeoutrrrclamp_remaining3s r%c@s6eZdZdZdZdeefddZddZdd Z d S) _TimeoutContextzInternal timeout context manager. Use :func:`pymongo.timeout` instead:: with pymongo.timeout(0.5): client.test.test.insert_one({}) _timeout_tokensr$cCs||_d|_dSrr')selfr$rrr__init__Fs z_TimeoutContext.__init__cCsZt|j}t}|jrt|jntd}tt||}t d}|||f|_ |S)Nrr) r rr(rrrr floatr#rr))r* timeout_tokenZ prev_deadlineZ next_deadlinedeadline_token rtt_tokenrrr __enter__Js   z_TimeoutContext.__enter__cCs8|jr|j\}}}t|t|t|dSdSr)r)r resetrr)r*exc_typeexc_valexc_tbr-r.r/rrr__exit__Ss    z_TimeoutContext.__exit__N) __name__ __module__ __qualname____doc__ __slots__rr,r+r0r5rrrrr&;s  r&F.)boundfunccs tfdd}tt|S)z/Apply the client's timeoutMS to this operation.csltdur+|j}|dur+t||g|Ri|WdS1s&wY|g|Ri|Sr)rr(r&)r*argskwargsr$r=rr csot_wrapperbs   zapply..csot_wrapper) functoolswrapsr r;)r=rArr@rapply_s rDcmd write_concerncCs>|r|jrdS|j}tdur|dd|r||d<dSdS)z+Apply the given write concern to a command.NZwtimeoutZ writeConcern)Zis_server_defaultZdocumentrpop)rErFwcrrrapply_write_concernns    rI) r9rBr contextvarsrrtypingrrrrrr r Zpymongo.write_concernr r r,__annotations__rrrrrrr!r%objectr&r;rDrIrrrrs&$ !