
    &g                       S SK Jr  S SKrS SKrS SKrS SKrS SKrS SKrS SKrS SK	J
r
JrJrJrJrJrJrJrJr  S SKJr  S SKJrJr  S SKJr  S SKJr  S SKJr  S S	KJrJ r J!r!J"r"J#r#  S S
K$J%r%J&r&J'r'J(r(J)r)J*r*J+r+J,r,J-r-  S SK.J/r/J0r0  S SK1J2r2J3r3J4r4  S SK5J6r6J7r7J8r8J9r9  S SK:J;r;J<r<  S SK=J>r>J?r?J@r@  S SKAJBrB  S SKCJDrDJErEJFrFJGrGJHrH  S SKIJJrJ  S SKKJLrL  S SKMJNrN  S SKOJPrP  S SKQJRrR  \
(       aL  S SKJSrS  S SKTJUrU  S SKVJWrW  S SKJXrX  S SKYJZrZJ[r[  S SK\J]r]J^r^J_r_  S SK`JaraJbrb  S SKcJdrd  S SKIJere  S S KfJgrgJhrh  S S!KiJjrj   S S"KkJlrlJmrmJnrnJkrk  S0S# jroS%rq " S& S'5      rr " S( S)\,5      rs " S* S+5      rt " S, S-5      ru " S. S/5      rvg! \p a    S0S$ jro NCf = f)1    )annotationsN)	TYPE_CHECKINGAnyAsyncGeneratorMappingMutableMappingNoReturnOptionalSequenceUnion)DEFAULT_CODEC_OPTIONS)_csothelpers_shared)_validate_session_write_concern)_handle_reauth)command)MAX_BSON_SIZEMAX_MESSAGE_SIZEMAX_WIRE_VERSIONMAX_WRITE_BATCH_SIZEORDERED_TYPES)	AutoReconnectConfigurationErrorDocumentTooLargeExecutionTimeoutInvalidOperationNotPrimaryErrorOperationFailurePyMongoErrorWaitQueueTimeoutError)HelloHelloCompat)_async_cond_wait_async_create_condition_async_create_lock)_CONNECTION_LOGGER_ConnectionStatusMessage
_debug_log _verbose_connection_error_reason)ConnectionCheckOutFailedReasonConnectionClosedReason)AsyncNetworkingInterfaceasync_receive_messageasync_sendall)PoolOptions)_CancellationContext_configured_protocol_interface_get_timeout_details_raise_connection_failureformat_timeout_details)ReadPreference)_add_to_command)SERVER_TYPE)SocketChecker)SSLError)CodecOptions)ObjectId)_AuthContext)AsyncClientSession)AsyncMongoClient_MongoClientErrorHandler)SnappyContextZlibContextZstdContext)_OpMsg_OpReply)ReadConcern)_ServerMode)_Address_CollationIn)WriteConcern)F_GETFDF_SETFD
FD_CLOEXECfcntlc                T    [        U [        5      n[        U [        U[        -  5        g)z8Set the close-on-exec flag on the given file descriptor.N)rM   rJ   rK   rL   )fdflagss     ^C:\Suresh\moveshuttle\MDcreated\moveengine\venv\Lib\site-packages\pymongo/asynchronous/pool.py_set_non_inheritable_non_atomicrR   n   s    b'"b'5:-.    c                    g)z6Dummy function for platforms that don't provide fcntl.N )rO   s    rQ   rR   rR   w   s    rS   Fc                  &   \ rS rSrSr        S&S jrS'S jr      S(S jrS)S jrS)S jr	S)S jr
S*S	 jrS+S
 jr      S,S jrS*S jr\\R"                  \SSSSSSSSSSSS4                                 S-S jj5       rS.S jrS/S jrS0S jrS1S jr        S2S jrS3S4S jjr      S5S jrS6S jrS)S jrS7S jr        S8S jrS9S jrS)S jr S:S jr!S;S jr"S<S  jr#S=S! jr$S=S" jr%S>S# jr&S?S$ jr'S%r(g)@AsyncConnection~   zStore a connection with some metadata.

:param conn: a raw connection object
:param pool: a Pool instance
:param address: the server's (host, port)
:param id: the id of this socket in it's pool
c                   [         R                  " U5      U l        Xl        X0l        X@l        SU l        [        R                  " 5       U l	        SU l
        SU l        [        U l        [        U l        [         U l        [$        U l        SU l        SU l        SU l        SU l        UR0                  R2                  U l        UR6                  U l        UR8                  U l        UR0                  R:                  U l        S U l        [A        5       U l!        S U l"        S U l#        S U l$        URJ                  U l&        U RL                  RO                  5       U l(        SU l)        [U        5       U l+        UR0                  U l        SU l,        S U l-        S U l.        SU l/        SU l0        SU l1        U R0                  Rd                  U l3        SU l4        URj                  U l5        [        R                  " 5       U l6        S U l7        g )NFg        )8weakrefrefpool_refconnaddressidclosedtime	monotoniclast_checkin_timeperformed_handshakeis_writabler   max_wire_versionr   max_bson_sizer   max_message_sizer   max_write_batch_sizesupports_sessionshello_ok	is_mongosop_msg_enabledopts_event_listeners	listenersenabled_for_cmapenabled_for_logging_compression_settingscompression_settingscompression_contextr8   socket_checkeroidc_token_gen_idnegotiated_mechsauth_ctxgenpool_genget_overall
generationreadyr0   cancel_contextmore_to_come
service_idserver_connection_id
pinned_txnpinned_cursoractivesocket_timeoutlast_timeoutconnect_rtt
_client_idcreation_time_cluster_time)selfr]   poolr^   r_   s        rQ   __init__AsyncConnection.__init__   s     D)	!%!1#( !& 0* 0$8!!&##33 $ 5 5#'#;#; $(II$C$C!Y] -:_045904 --335
4H4JII	"'.237!  " II44//!^^-!rS   c                z    XR                   :X  a  gXl         U R                  R                  R                  U5        g)z?Cache last timeout to avoid duplicate calls to conn.settimeout.N)r   r]   get_conn
settimeout)r   timeouts     rQ   set_conn_timeout AsyncConnection.set_conn_timeout   s/    '''#		%%g.rS   c                   [         R                  " 5       nUc7  U R                  (       d%  U R                  U R                  R
                  5        g [         R                  " 5       nUc  U R                  nX4-
  nUS:  aI  [        U R                  5      n[        U5      nSUS SUS SU 3n[        USSUSS.U R                  5      eUb  [        US-  5      US	'   U R                  U5        U$ )
Nr   z5operation would exceed time limit, remaining timeout:z.5fz <= network round trip time: 2   okerrmsgcode  	maxTimeMS)r   	remainingr   r   rn   r   get_rttr   r2   r4   r   rf   int)	r   clientcmdr   rttmax_time_mstimeout_details	formattedr   s	            rQ   apply_timeoutAsyncConnection.apply_timeout   s     //#?$$%%dii&>&>?mmo;""Cm?2499=O.?ILWUXMYuvyz}u~~  AJ  @K  LF"FB7%%	  ?";#56Cg&rS   c                8    SU l         U R                  (       a   eg NT)r   r   r   s    rQ   pin_txnAsyncConnection.pin_txn   s    %%%%%rS   c                8    SU l         U R                  (       a   eg r   )r   r   r   s    rQ   
pin_cursorAsyncConnection.pin_cursor   s    !??""?rS   c                   #    U R                  5       nU(       a  UR                  U 5      I S h  vN   g U R                  [        R                  5      I S h  vN   g  N- N7fN)r\   checkin
close_connr+   STALE)r   r   s     rQ   unpinAsyncConnection.unpin   sF     }},,t$$$//"8">">??? %?s!   ,A A'A AA A c                    U R                   R                  (       d,  U R                  (       d  U R                   R                  (       a  SU l        [
        R                  S0$ [
        R                  SSS0$ )NT   helloOk)rn   
server_apirk   load_balancedrm   r"   CMD
LEGACY_CMDr   s    rQ   	hello_cmdAsyncConnection.hello_cmd   sO     994==DII4K4K"&DOOQ''**Ay$??rS   c                B   #    U R                  S S 5      I S h  vN $  N7fr   )_hellor   s    rQ   helloAsyncConnection.hello   s     [[t,,,,s   c                  #    U R                  5       nU R                  (       + nSnU(       ak  SU l        U R                  R                  US'   U R                  (       a  U R                  R
                  US'   U R                  R                  (       a  SUS'   ObUb_  XS'   Uc   e[        US-  5      US'   SnU R                  R                  (       a(  U R                  U R                  R                  U-   5        U R                  R                  nU(       a  UR                  S	:X  a0  UR                  (       a  UR                  S
-   UR                  -   US'   SSKJn  UR                   R#                  X`R$                  5      nU(       a  UR'                  5       n	U	b  XS'   OS nU(       a  [(        R*                  " 5       n
U R-                  SUSUS9I S h  vN nU(       a  [(        R*                  " 5       W
-
  U l        [1        XS9nUR2                  U l        UR4                  U l        UR6                  U l        UR8                  U l        UR:                  U l        UR<                  S L=(       a    UR>                  U l         UR<                  U l        URB                  U l!        URD                  [F        RH                  [F        RJ                  [F        RL                  [F        RN                  [F        RP                  4;   U l)        URD                  [F        RT                  :H  U l+        URD                  [F        RX                  :H  U l-        U(       a<  U R                  (       a+  U R                  R]                  UR
                  5      nXl/        SU l0        URb                  U l2        U(       a  URf                  U l4        U(       a,  URk                  U5        URm                  5       (       a  Xl7        U R                  R                  (       aW  URp                  (       d  [s        S5      eURp                  U l8        U Rt                  Rw                  U Rp                  5      U l<        U$  GN7f)NFTr   compressionloadBalancedtopologyVersionr   maxAwaitTimeMSDEFAULT.saslSupportedMechsr   authspeculativeAuthenticateadmin)publish_eventsexhaust_allowed)	awaitablez`Driver attempted to initialize in load balancing mode, but the server does not support this mode)=r   rd   rn   metadatart   compressorsr   r   connect_timeoutr   _credentials	mechanismusernamesourcepymongo.asynchronousr   r<   from_credentialsr^   speculate_commandra   rb   r   r   r!   re   rf   rg   rh   ri   logical_session_timeout_minutesis_readablerj   rk   server_typer7   	RSPrimaryRSSecondary	RSArbiterRSOtherRSGhostis_repl
Standaloneis_standaloneMongosrl   get_compression_contextru   rm   connection_idr   sasl_supported_mechsrx   parse_responsespeculate_succeededry   r   r   r{   getr}   )r   topology_versionheartbeat_frequencyr   performing_handshaker   credsr   ry   speculative_authenticatestartdocr   ctxs                 rQ   r   AsyncConnection._hello  s}    
 nn#'#;#;;	'+D$ II..CM((%)%>%>%J%JM"yy&&&*N#)%5!"&222$'(;d(B$CC !Iyy((%%dii&?&?BU&UV		&&)+,1LL3,>,O()1((99%NH+3+E+E+G(+75M12HNN$ELL#eU^L__#~~/%7Dc/ ,, % 6 6"00 % 6 6$)$>$>!11=S%BSBS 	 ?D>c>c,((!!##!!-
 
 #..+2H2HH**k.@.@@D$=$=++CCEDUDUVC'*$"$)$7$7!$)$>$>D!##E*++-- (99""##(A  $..DO"mm//@DOW `s   GQ#Q JQ#c                   #    U R                  S 5      I S h  vN nUR                  U l        UR                  5       nUS   n[        R                  " X0R
                  5        U$  NL7fNr   )receive_messager   unpack_responser   _check_command_responserf   )r   replyunpacked_docsresponse_docs       rQ   _next_replyAsyncConnection._next_replyV  s]     **400!..--/$Q'..|=R=RS 1s   A&A$AA&TNFc                *  #    U R                  X5        [        X5      n[        U[        5      (       d  [	        U5      nUb  UR
                  (       d  U
b  [        S5      eU R                  U5        U(       a  UR                  X-X05        U R                  X+U5        U(       a  U R                  OSn[        U=(       a    UR
                  (       + 5      nU R                  (       a  U R                  U5         [        U UUU R                  UUUUUUU R                   UU R"                  UU	U
U R$                  U R                  UUUUS9I Sh  vN $  N! [&        [(        4 a    e [*         a$  nU R-                  U5      I Sh  vN     SnAgSnAff = f7f)a  Execute a command or raise an error.

:param dbname: name of the database on which to run the command
:param spec: a command document as a dict, SON, or mapping object
:param read_preference: a read preference
:param codec_options: a CodecOptions instance
:param check: raise OperationFailure if there are errors
:param allowable_errors: errors to ignore if `check` is True
:param read_concern: The read concern for this command.
:param write_concern: The write concern for this command.
:param parse_write_concern_error: Whether to parse the
    ``writeConcernError`` field in the command response.
:param collation: The collation for this command.
:param session: optional AsyncClientSession instance.
:param client: optional AsyncMongoClient for gossipping $clusterTime.
:param retryable_write: True if this command is a retryable write.
:param publish_events: Should we publish events for this command?
:param user_fields: Response fields that should be decoded
    using the TypeDecoders from codec_options, passed to
    bson._decode_all_selective.
Nz3Collation is unsupported for unacknowledged writes.)parse_write_concern_error	collationcompression_ctx
use_op_msgunacknowledgeduser_fieldsr   write_concern)validate_sessionr   
isinstancer   dictacknowledgedr   add_server_api	_apply_tosend_cluster_timerp   boolrm   _raise_if_not_writabler   rl   r^   rg   ru   r   r   BaseExceptionr3   )r   dbnamespecread_preferencecodec_optionscheckallowable_errorsread_concernr  r  r  sessionr   retryable_writer   r
  r   rp   r	  errors                       rQ   r   AsyncConnection.command^  sq    R 	f.1'I $..:D%)C)CyGX$%Z[[D!d_Ktf5&4DNN$	mNM4N4N0NO''7	8  ""*C# $ 8 8..-' /+-   0 !/2 	 	800777	8sO   C5F8AE EE FE F,F FFFFFc                .  #    U R                   b(  X R                   :  a  [        SX R                   4-  5      e [        U R                  R                  U5      I Sh  vN   g N! [
         a$  nU R                  U5      I Sh  vN     SnAgSnAff = f7f)zmSend a raw BSON message or raise ConnectionFailure.

If a network exception is raised, the socket is closed.
NzfBSON document too large (%d bytes) - the connected server supports BSON document sizes up to %d bytes.)rg   r   r.   r]   r   r  r3   )r   messagemax_doc_sizer  s       rQ   send_messageAsyncConnection.send_message  s     
 )l=O=O.O"?BNPbPbAcd 
	8		 2 2G<<< 	800777	8sL   6B#A$ A"A$ !B"A$ $
B.BBBBBBc                   #     [        XU R                  5      I Sh  vN $  N! [         a$  nU R                  U5      I Sh  vN     SnAgSnAff = f7f)zjReceive a raw BSON message or raise ConnectionFailure.

If any exception is raised, the socket is closed.
N)r-   rh   r  r3   )r   
request_idr  s      rQ   r   AsyncConnection.receive_message  sF     
	8.tAVAVWWWW 	800777	8sB   A$ "$ A$ 
AAAAAAAc                V    U(       a"  U R                   (       d  [        SSSSS.5      egg)zNRaise NotPrimaryError on unacknowledged write if this socket is not
writable.
znot primaryr   i{'  r   N)re   r   )r   r	  s     rQ   r  &AsyncConnection._raise_if_not_writable  s+     $"2"2!-]\a1bcc #3>rS   c                d   #    U R                  S5        U R                  X5      I Sh  vN   g N7f)zSend unack OP_MSG.

Can raise ConnectionFailure or InvalidDocument.

:param msg: bytes, an OP_MSG message.
:param max_doc_size: size in bytes of the largest document in `msg`.
TN)r  r$  )r   msgr#  s      rQ   unack_writeAsyncConnection.unack_write  s)      	##D)222s   &0.0c                   #    U R                  US5      I Sh  vN   U R                  U5      I Sh  vN nUR                  U5      n[        R                  " XPR
                  5        U$  NP N97f)zSend "insert" etc. command, returning response as a dict.

Can raise ConnectionFailure or OperationFailure.

:param request_id: an int.
:param msg: bytes, the command message.
r   N)r$  r   command_responser   r   rf   )r   r'  r,  r  r   results         rQ   write_commandAsyncConnection.write_command  sc      Q'''**:66''6 	..v7L7LM 	(6s   A-A)A-A+8A-+A-c           
       #    U(       a  U R                   (       a  SU l        SU l        U R                  (       GdA  U R                  R                  nU(       a  SSKJn  UR                  X US9I Sh  vN   SU l        [        R                  " 5       U R                  -
  nU R                  (       a@  U R                  c   eU R                  R                  U R                  U R                  U5        U R                   (       az  ["        R$                  " [&        R(                  5      (       aP  [+        ["        [,        R.                  U R0                  U R                  S   U R                  S   U R                  US9  gggg GN7f)	zXAuthenticate to the server if needed.

Can raise ConnectionFailure or OperationFailure.
NFr   r   )reauthenticateTr   r"  clientId
serverHost
serverPortdriverConnectionId
durationMS)rd   ry   r~   rn   r   r   r   authenticatera   rb   r   rq   rp   publish_connection_readyr^   r_   rr   r&   isEnabledForloggingDEBUGr(   r'   
CONN_READYr   )r   r5  r   r   durations        rQ   r<  AsyncConnection.authenticate  s     '' $DJzzzII**E5''N'SSSDJ~~'$*<*<<H$$~~11177dggxX'',>,K,KGMM,Z,Z&4??!__#||A#||A'+ww' -[' 
 Ts   A/F 1E=2DF c                H    U(       a  UR                   ULa  [        S5      egg)zsValidate this session before use with client.

Raises error if the client is not the one that created the session.
z>Can only use session with the AsyncMongoClient that started itN)_clientr   )r   r   r  s      rQ   r   AsyncConnection.validate_session  s-     f,&T  - rS   c                @  #    U R                   (       a  gU R                  5       I Sh  vN   U(       a  U R                  (       a@  U R                  c   eU R                  R	                  U R
                  U R                  U5        U R                  (       a  [        R                  " [        R                  5      (       aZ  [        [        [        R                  U R                  U R
                  S   U R
                  S   U R                  [!        U5      US9  gggg N7f)z$Close this connection with a reason.Nr   r   r"  r7  r8  r9  r:  reasonr  )r`   _close_connrq   rp   publish_connection_closedr^   r_   rr   r&   r>  r?  r@  r(   r'   CONN_CLOSEDr   r)   )r   rI  s     rQ   r   AsyncConnection.close_conn(  s     ;;   $$~~11188twwPVW'',>,K,KGMM,Z,Z&4@@!__#||A#||A'+ww;FC 	 -['	  	!s   &DDC4Dc                   #    U R                   (       a  gSU l         U R                  R                  5          U R                  R	                  5       I Sh  vN   g N! [
         a     gf = f7f)zClose this connection.NT)r`   r   cancelr]   close	Exceptionr   s    rQ   rJ  AsyncConnection._close_conn=  sR     ;;""$	))//### 		s:   4A,A AA A,A 
A)&A,(A))A,c                    [         (       a/  U R                  R                  U R                  R                  5      $ U R                  R                  5       $ )z?Return True if we know socket has been closed, False otherwise.)_IS_SYNCrv   socket_closedr]   r   
is_closingr   s    rQ   conn_closedAsyncConnection.conn_closedJ  s;    8&&44TYY5G5GHH99''))rS   c                6    U(       a  UR                  X5        gg)zAdd $clusterTime.N)_send_cluster_time)r   r   r  r   s       rQ   r  !AsyncConnection.send_cluster_timeQ  s     %%g7 rS   c                z    U R                   R                  (       a   [        XR                   R                  5        gg)zAdd server_api parameters.N)rn   r   r6   )r   r   s     rQ   r  AsyncConnection.add_server_api[  s&    99GYY%9%9:  rS   c                8    [         R                  " 5       U l        g r   ra   rb   rc   r   s    rQ   update_last_checkin_time(AsyncConnection.update_last_checkin_time`  s    !%!1rS   c                    Xl         g r   )re   )r   re   s     rQ   update_is_writable"AsyncConnection.update_is_writablec  s    &rS   c                F    [         R                  " 5       U R                  -
  $ )z9Seconds since this socket was last checked into its pool.r_  r   s    rQ   idle_time_seconds!AsyncConnection.idle_time_secondsf  s    ~~$"8"888rS   c                  #    U R                   (       a  S nO[        R                  nU R                  U5      I S h  vN   [	        U[
        [        [        45      (       a*  [        U R                  5      n[        U R                  XS9  g e  NO7f)Nr   )r~   r+   ERRORr   r  IOErrorOSErrorr9   r2   rn   r3   r^   )r   r  rI  detailss       rQ   r3   )AsyncConnection._raise_connection_failurej  sh       ::F+11Foof%%%egw9::*4995G%dllES 	&s   9BBABc                4    U R                   UR                   :H  $ r   )r]   r   others     rQ   __eq__AsyncConnection.__eq__  s    yyEJJ&&rS   c                    X:X  + $ r   rU   rp  s     rQ   __ne__AsyncConnection.__ne__  s      rS   c                ,    [        U R                  5      $ r   )hashr]   r   s    rQ   __hash__AsyncConnection.__hash__  s    DIIrS   c                    SR                  [        U R                  5      U R                  =(       a    S=(       d    S[	        U 5      5      $ )NzAsyncConnection({}){} at {}z CLOSED )formatreprr]   r`   r_   r   s    rQ   __repr__AsyncConnection.__repr__  s8    ,33OKK%I+tH
 	
rS   ),r   r   r   r^   ry   r   r`   ru   rt   r]   r   r   rq   rr   r}   rk   r_   rl   r   r   re   rc   r   rp   r   rg   rh   rf   ri   r   rx   rw   rm   rn   rd   r   r   r{   r\   r~   r   r   rv   rj   )r]   r,   r   Poolr^   ztuple[str, int]r_   r   )r   Optional[float]returnNone)r   r>   r   z"Optional[MutableMapping[str, Any]]r  r  r  r  )r  dict[str, Any])r  r!   )r   zOptional[Any]r   Optional[int]r  zHello[dict[str, Any]])"r  strr  MutableMapping[str, Any]r  rF   r  r:   r  r  r  z#Optional[Sequence[Union[str, int]]]r  zOptional[ReadConcern]r  zOptional[WriteConcern]r  r  r  zOptional[_CollationIn]r  Optional[AsyncClientSession]r   Optional[AsyncMongoClient]r  r  r   r  r
  zOptional[Mapping[str, Any]]r   r  r  r  )r"  bytesr#  r   r  r  )r'  r  r  zUnion[_OpReply, _OpMsg])r	  r  r  r  )r,  r  r#  r   r  r  )r'  r   r,  r  r  r:   r  r  )F)r5  r  r  r  )r   r  r  r  r  r  )rI  zOptional[str]r  r  r  r  )r   r  r  r  r   r  r  r  )r   r  r  r  )re   r  r  r  )r  float)r  r  r  r	   )rq  r   r  r  r  r   )r  r  ))__name__
__module____qualname____firstlineno____doc__r   r   r   r   r   r   r   r   r   r  r   r5   PRIMARYr   r   r$  r   r  r-  r2  r<  r  r   rJ  rW  r  r  r`  rc  rf  r3   rr  ru  ry  r  __static_attributes__rU   rS   rQ   rW   rW   ~   sa   7"&7" 7" !	7"
 7"r/&-O	>&#@@-S'S +S 
	Sj 
 (6'='=&;@D.204*/,004-1 %#37 %#W8W8 'W8 %	W8
 $W8 W8 >W8 ,W8 .W8 $(W8 *W8 .W8 +W8 W8 W8  1!W8" #W8$ 
%W8 W8r8"	8d	3$):F	$ D0;W	**8)8 .8 +	8
 
8;
2'98'!
rS   rW   c                      \ rS rSrSrSrg)_PoolClosedErrori  zRInternal error raised when a thread tries to get a connection from a
closed pool.
rU   N)r  r  r  r  r  r  rU   rS   rQ   r  r    s    rS   r  c                  F    \ rS rSrS	S jrS
S jrSS jrSS jrSS jrSr	g)_PoolGenerationi  c                P    [         R                  " [        5      U l        SU l        g r   )collectionsdefaultdictr   _generations_generationr   s    rQ   r   _PoolGeneration.__init__  s    1<1H1H1MrS   c                >    Uc  U R                   $ U R                  U   $ )z,Get the generation for the given service_id.r  r  r   r   s     rQ   r   _PoolGeneration.get  s%    ###  ,,rS   c                    U R                   $ )z"Get the Pool's overall generation.)r  r   s    rQ   r|   _PoolGeneration.get_overall  s    rS   c                    U =R                   S-  sl         Uc+  U R                   H  nU R                  U==   S-  ss'   M     gU R                  U==   S-  ss'   g)z2Increment the generation for the given service_id.r   Nr  r  s     rQ   inc_PoolGeneration.inc  sW    A"//
!!*-2- 0 j)Q.)rS   c                (    XR                  U5      :g  $ )z?Return if the given generation for a given service_id is stale.)r   r   rz   r   s      rQ   stale_PoolGeneration.stale  s    hhz***rS   r  Nr  )r   Optional[ObjectId]r  r   r  )r   r  r  r  rz   r   r   r  r  r  )
r  r  r  r  r   r   r|   r  r  r  rU   rS   rQ   r  r    s    - /+rS   r  c                       \ rS rSrSrSrSrSrg)	PoolStatei  r         rU   N)r  r  r  r  PAUSEDREADYCLOSEDr  rU   rS   rQ   r  r    s    FEFrS   r  c                  R   \ rS rSr  S       SS jjrSS jr\SS j5       r   S         SS jjrSS jr	 S     SS jjr
SS	 jrSS
 jrSS jrS S jrS!S"S jjr\R"                   S!   S#S jj5       rS$S jr S!     S%S jjrS&S jrS'S jrS(S jrSS jrSrg))r  i  Nc                *   UR                   (       a  [        R                  U l        O[        R                  U l        SU l        [        R                  " 5       U l        [        5       U l
        [        5       U l        [        U R                  5      U l        SU l        SU l        SU l        [%        5       U l        [(        R*                  " 5       U l        Xl        X l        X0l        U R2                  =(       a?    U R0                  R4                  SL=(       a     U R0                  R4                  R6                  U l        U R2                  U l        [        U R                  5      U l        SU l        U R0                  R>                  U l        U R>                  (       d  [A        S5      U l        [        U R                  5      U l        U R0                  RB                  U l"        SU l#        X@l$        U R6                  (       a]  U R0                  R4                  c   eU R0                  R4                  RK                  U R.                  U R0                  RL                  5        U R8                  (       a  [N        RP                  " [R        RT                  5      (       a[  [W        [N        4[X        RZ                  U RH                  U R.                  S   U R.                  S   S.U R0                  RL                  D6  SU l.        [        5       U l/        SU l0        SU l1        g)z
:param address: a (hostname, port) tuple
:param options: a PoolOptions instance
:param handshake: whether to call hello for each new AsyncConnection
r   r   Ninfr"  r7  r8  r9  )2pause_enabledr  r  stater  _check_interval_secondsr  dequeconnssetactive_contextsr%   lockr$   _max_connecting_condactive_socketsnext_connection_idre   r  rz   osgetpidpidr^   rn   	handshakero   rq   rr   	size_condrequestsmax_pool_sizer  max_connecting_max_connecting_pendingr   publish_pool_creatednon_default_optionsr&   r>  r?  r@  r(   r'   POOL_CREATEDoperation_count_Pool__pinned_socketsncursorsntxns)r   r^   optionsr  	client_ids        rQ   r   Pool.__init__  s=      "))DJ"DJ ()$ )4(9(9(;
:=%&(	$;DII$F!"#+/
 #$99;	" NN <		**$6<		**;; 	
 $(>> 
 1;!YY44!!!&uD %<DII$F!#yy77#  99--999II&&;;dii;; ##(:(G(G(V(V"0==<<?<<? ))// %& 7:e
rS   c           	       #    U R                    IS h  vN   U R                  [        R                  :w  a  [        R                  U l        U R                  (       aH  U R
                  R                  c   eU R
                  R                  R                  U R                  5        U R                  (       al  [        R                  " [        R                  5      (       aC  [        [        [        R                   U R"                  U R                  S   U R                  S   S9  S S S 5      IS h  vN   g  GN N! , IS h  vN  (       d  f       g = f7f)Nr   r   r  )r  r  r  r  rq   rn   ro   publish_pool_readyr^   rr   r&   r>  r?  r@  r(   r'   
POOL_READYr   r   s    rQ   r~   
Pool.ready  s     999zzY__,&__
((9955AAAII..AA$,,O++0B0O0OPWP]P]0^0^* 8 C C!%#'<<?#'<<? 99999sE   ED2ED
D7!E,D5-E5E7E=E >E
Ec                <    U R                   [        R                  :H  $ r   )r  r  r  r   s    rQ   r`   Pool.closed,  s    zzY----rS   c           	       #    U R                   nU R                   IS h  vN   U R                  (       a   S S S 5      IS h  vN   g U R                  R                  (       aC  U(       a<  U R                  R
                  (       d!  U R                   [        R                  soPl         U R                  R                  U5        [        R                  " 5       nU R                  U:w  a  X`l        SU l        SU l        Uc'  U R                  [         R"                  " 5       sopl        Oy[         R"                  " 5       n[         R"                  " 5       n	U R                   H7  n
U
R$                  U:X  a  UR'                  U
5        M&  U	R'                  U
5        M9     UnXl        U(       a  [        R(                  U l         U R*                  R-                  5         U R                  R-                  5         U(       a#  U R.                   H  nUR1                  5         M     S S S 5      IS h  vN   U R                  R2                  nU(       a  W H*  n
U
R5                  [6        R8                  5      I S h  vN   M,     U R:                  (       a   Uc   eUR=                  U R>                  5        U R@                  (       an  [B        RD                  " [F        RH                  5      (       aD  [K        [B        [L        R8                  U RN                  U R>                  S   U R>                  S   S9  g g g U[        R                  :w  a  U R:                  (       a   Uc   eURQ                  U R>                  UUS9  U R@                  (       am  [B        RD                  " [F        RH                  5      (       aD  [K        [B        [L        RR                  U RN                  U R>                  S   U R>                  S   US9  W H*  n
U
R5                  [6        RT                  5      I S h  vN   M,     g  GN GN GN! , IS h  vN  (       d  f       GN= f GN N07f)Nr   r   r  )r   interrupt_connections)r"  r7  r8  r9  	serviceId)+r  r  r`   rn   r  r   r  r  rz   r  r  r  r  r  r  r  r  r  r   appendr  r  
notify_allr  rO  ro   r   r+   POOL_CLOSEDrq   publish_pool_closedr^   rr   r&   r>  r?  r@  r(   r'   r   publish_pool_clearedPOOL_CLEAREDr   )r   rP  pauser   r  	old_statenewpidsocketsdiscardkeepr]   contextrp   s                rQ   _resetPool._reset0  s      JJ	>>>{{ ">> yy&&59P9P(,

I4D4D%	:HHLL$YY[Fxx6!!&'#'($!&*jj+2C2C2E#-8->->-@*5*;*;*= JJD*4t,D)	 '
 "!
&--
%%002NN%%'$#33GNN$  4? ">D II..	 oo&<&H&HIII  $$ ,,,--dll;'',>,K,KGMM,Z,Z&4@@!__#||A#||A -[' I,,,(($00022#-.C 3 
 ++0B0O0OPWP]P]0^0^* 8 E E!%#'<<?#'<<?",  oo&<&B&BCCC  M ">>>>P J> Ds   QPQP!QPQGP!Q!P"AQ*P<+F#QP?
QQQ!P9'P*(P94	Q?Qc                   #    Xl         U R                   ISh  vN   U R                   H  nUR                  U R                   5        M      SSS5      ISh  vN   g NC N! , ISh  vN  (       d  f       g= f7f)zHUpdates the is_writable attribute on all sockets currently in the
Pool.
N)re   r  r  rc  )r   re   _sockets      rQ   rc  Pool.update_is_writable  sG      '999::**4+;+;< & 99999sD   A;AA;/A!A;AA;A;!A8'A*(A84A;c                @   #    U R                  SXS9I S h  vN   g  N7f)NF)rP  r   r  r  )r   r   r  s      rQ   reset
Pool.reset  s'      kkJ  
 	
 	
   c                @   #    U R                  SSS9I S h  vN   g  N7f)NF)rP  r  r  r   s    rQ   reset_without_pausePool.reset_without_pause  s     kkUk333r  c                >   #    U R                  SS9I S h  vN   g  N7f)NT)rP  r  r   s    rQ   rP  
Pool.close  s     kkk%%%s   c                8    U R                   R                  X5      $ r   )rz   r  r  s      rQ   stale_generationPool.stale_generation  s    xx~~c..rS   c           	       #    U R                    ISh  vN   U R                  [        R                  :w  a   SSS5      ISh  vN   gSSS5      ISh  vN   U R                  R
                  Gb  / nU R                    ISh  vN   U R                  (       a  U R                  S   R                  5       U R                  R
                  :  aq  UR                  U R                  R                  5       5        U R                  (       a7  U R                  S   R                  5       U R                  R
                  :  a  Mq  SSS5      ISh  vN   U H*  nUR                  [        R                  5      I Sh  vN   M,      U R                   ISh  vN   [        U R                  5      U R                  -   U R                  R                   :  a   SSS5      ISh  vN   gU R"                  U R                  R                   :  a   SSS5      ISh  vN   gU =R"                  S-  sl        SSS5      ISh  vN   Sn U R$                   ISh  vN   U R&                  U R(                  :  a   SSS5      ISh  vN   U(       aT  U R$                   ISh  vN   U =R&                  S-  sl        U R$                  R+                  5         SSS5      ISh  vN   U R                   ISh  vN   U =R"                  S-  sl        U R                  R+                  5         SSS5      ISh  vN   gU =R&                  S-  sl        SnSSS5      ISh  vN   U R-                  5       I Sh  vN nSnU R                    ISh  vN   U R.                  R1                  5       U:w  a  SnU(       d@  U R                  R3                  U5        U R4                  R7                  UR8                  5        SSS5      ISh  vN   U(       a  UR                  [        R:                  5      I Sh  vN    U(       aT  U R$                   ISh  vN   U =R&                  S-  sl        U R$                  R+                  5         SSS5      ISh  vN   U R                   ISh  vN   U =R"                  S-  sl        U R                  R+                  5         SSS5      ISh  vN   g U(       aT  U R$                   ISh  vN   U =R&                  S-  sl        U R$                  R+                  5         SSS5      ISh  vN   U R                   ISh  vN   U =R"                  S-  sl        U R                  R+                  5         SSS5      ISh  vN   GM   GNz GNN GN@! , ISh  vN  (       d  f       GNV= f GN/ GNk! , ISh  vN  (       d  f       GN= f GN] GNF GN GN GN! , ISh  vN  (       d  f       GN= f GN GN GNm GN1! , ISh  vN  (       d  f       GNG= f GN: GN! , ISh  vN  (       d  f       g= f GN! , ISh  vN  (       d  f       GN	= f GN GN GNq! , ISh  vN  (       d  f       GN= f GNa GNG GN! , ISh  vN  (       d  f       GN!= f GN GN! , ISh  vN  (       d  f       g= f GN GN! , ISh  vN  (       d  f       GN= f GN GNh! , ISh  vN  (       d  f       GN~= f! U(       aq  U R$                   ISh  vN    U =R&                  S-  sl        U R$                  R+                  5         SSS5      ISh  vN    O! , ISh  vN  (       d  f       O= fU R                   ISh  vN    U =R"                  S-  sl        U R                  R+                  5         SSS5      ISh  vN    f ! , ISh  vN  (       d  f       f = f= f7f)zRemoves stale sockets then adds new ones if pool is too small and
has not been reset. The `reference_generation` argument specifies the
`generation` at the point in time this operation was requested on the
pool.
NTr   F)r  r  r  r  rn   max_idle_time_secondsr  rf  r  popr   r+   IDLEr  lenr  min_pool_sizer  r  r  r  notifyconnectrz   r|   
appendleftr  r  r   r   )r   reference_generationclose_connsr]   incrementedr   s         rQ   remove_stale_socketsPool.remove_stale_sockets  s     999zzY__, 9999 99**6KyyyJJ

288:TYY=\=\\&&tzz~~'78 JJ

288:TYY=\=\\ !y $oo&<&A&ABBB $ ~~~tzz?T%8%88DII<S<SS &~~ ==DII$;$;; &~~ " &~  K,444 }}(<(<<	 54* #888*1188:  98  >>>MMQ&MNN))+ *>>- MMQ&M"&K 54 "\\^+"
999 xx++-1EE%)
%

--d3,,44T5H5HI %9 //*@*F*FGGG#888*1188:  98  >>>MMQ&MNN))+ *>>  #888*1188:  98  >>>MMQ&MNN))+ *>K  9999 !yyy C &~~~~ 5.  9888 *>>>7 5444 ,$999 H
  9888 *>>>	  9888 *>>> #888*1188:  988888  >>>MMQ&MNN))+ *>>>>>s#  _8V_8 V_8V_8V._8V1_8B6V7>_8	V4
+_85W6_8W_8<W!_8W_8 %W!_8W_8W!+_86W7_8>\ W<\ Y.\ 9W?:\ >_8X_80X	_8X_8)X#*_8-0X)_8(X&)_8.Y\ Y\ (Y!)\ ?Y$ \ A(Y*+\ 6Y'7-\ $Z%\ *_8Z_80Z5_8 Z_8Z)_80Z/	_8Z,_8_82[	3_860[&_81[2_8[*_8
0[0:_8[-_8_8_8V.VV.)	_84_87W=W >W
	_8_8_8_8_8!W9'W*(W94_8<\ ?\ _8_8X XX 	_8&_8)Y /X20Y <_8\ YYY	\ $\ '\ *Z0Y31Z=	\ _8_8Z&ZZ&!	_8,_8/[5Z86[_8_8['[['"	_8-_80\6[97\_8_5#\&
$_5(0]*_5#]&$_5*^0]31^=_5^
_50_	_5__5_2!_$"_2._55_8c                x
  #    U R                    ISh  vN   U R                  nU =R                  S-  sl        [        5       nU R                  R	                  U5        SSS5      ISh  vN   U R
                  R                  nU R                  (       a!  Uc   eUR                  U R                  W5        U R                  (       am  [        R                  " [        R                  5      (       aD  [        [        [         R"                  U R$                  U R                  S   U R                  S   WS9   ['        U R                  U R
                  5      I Sh  vN n[C        XPU R                  W5      nU R                    ISh  vN   U R                  R	                  URD                  5        U R                  R+                  W5        SSS5      ISh  vN   WRF                  (       a  URD                  RI                  5          U RJ                  (       a)  URM                  5       I Sh  vN   URN                  U l'        U(       a  URQ                  USS9  URS                  5       I Sh  vN   U(       a7  URV                  RX                  R[                  UR\                  5      I Sh  vN   U$  GN GNH! , ISh  vN  (       d  f       GN^= f GNw! [(         Ga  nU R                    ISh  vN    U R                  R+                  W5        SSS5      ISh  vN    O! , ISh  vN  (       d  f       O= fU R                  (       a0  Uc   eUR-                  U R                  W[.        R0                  5        U R                  (       a  [        R                  " [        R                  5      (       ak  [        [        [         R2                  U R$                  U R                  S   U R                  S   W[5        [.        R0                  5      [.        R0                  S9  [7        U[8        [:        [<        45      (       a)  [?        U R
                  5      n[A        U R                  XgS9  e SnAff = f GN GN! , ISh  vN  (       d  f       GN= f GNj GN-! [(         a    U R                    ISh  vN    U R                  R+                  URD                  5        SSS5      ISh  vN    O! , ISh  vN  (       d  f       O= fURU                  [.        R0                  5      I Sh  vN    e f = f GN7f)	zConnect to Mongo and return a new AsyncConnection.

Can raise ConnectionFailure.

Note that the pool does not keep a reference to the socket -- you
must call checkin() when you're done with it.
Nr   r   r"  r7  r8  r9  r:  rH  ri  F)completed_handshake)/r  r  r0   r  addrn   ro   rq   publish_connection_createdr^   rr   r&   r>  r?  r@  r(   r'   CONN_CREATEDr   r1   r  r  rK  r+   rj  rL  r)   r  rk  rl  r9   r2   r3   rW   r   	cancelledrO  r  r   re   contribute_socketr<  r   r   	_topologyreceive_cluster_timer   )	r   handlerconn_idtmp_contextrp   networking_interfacer  rm  r]   s	            rQ   r
  Pool.connect  s     999--G##q(#.0K  $$[1 9 II..	  (((00wG##(:(G(G(V(V"0==<<?<<?#*	)GVZV_V_)`#` 6 34<<Q999  $$T%8%89  ((5 9   &&(	~~jjl""#'#3#3 ))$E)J##%%% ..**??@R@RSSSS 999, $a 	yyy$$,,[9 !yyyyy$$ ,,,33LL'+A+G+G '',>,K,KGMM,Z,Z&4@@!__#||A#||A'.;<R<X<XY066	 %'7H!=>>.tyy9)$,,W/	4 999 #
 & 	yyy$$,,T-@-@A !yyyyy//"8">">???		 Ts  T:J.T:AJ4T:)J1*C
T:5#K KK 'T:Q2T:AQ8	T:Q5/T:$R )R*?R )R*R .9T:'T7(T:1T:4K:J=;KT:K Q/Q*-K0.Q*2L Q*LQ* L7	&L)'L7	3D7Q**Q//T:5T:8R>R?RT:R R T43R64T48&S0T4)S,*T40T	6S97T	)T4,T/-T44T:c           
    :  #    U R                   R                  n[        R                  " 5       nU R                  (       a   Uc   eUR                  U R                  5        U R                  (       al  [        R                  " [        R                  5      (       aC  [        [        [        R                  U R                  U R                  S   U R                  S   S9  U R!                  X1S9I Sh  vN n[        R                  " 5       U-
  nU R                  (       a,  Uc   eUR#                  U R                  UR$                  U5        U R                  (       ax  [        R                  " [        R                  5      (       aO  [        [        [        R&                  U R                  U R                  S   U R                  S   UR$                  US9   U R(                   ISh  vN   U R*                  R-                  UR.                  5        SSS5      ISh  vN   U7v   UR2                  (       aV  U R(                   ISh  vN   U R@                  R-                  U5        U =RB                  S-  sl!        SSS5      ISh  vN   gUR4                  (       aV  U R(                   ISh  vN   U R@                  R-                  U5        U =RD                  S-  sl"        SSS5      ISh  vN   gUR<                  (       a  U R?                  U5      I Sh  vN   gg GN- GN< GN
! , ISh  vN  (       d  f       GN = f! [0         a    UR2                  =(       d    UR4                  nU(       a2  [6        R8                  " 5       u  pxn	UR;                  Xx5      I Sh  vN    U(       d+  UR<                  (       a  U R?                  U5      I Sh  vN    e f = f GN GNY! , ISh  vN  (       d  f       g= f GNO GN! , ISh  vN  (       d  f       g= f GN7f)a  Get a connection from the pool. Use with a "with" statement.

Returns a :class:`AsyncConnection` object wrapping a connected
:class:`socket.socket`.

This method should always be used in a with-statement::

    with pool.get_conn() as connection:
        connection.send_message(msg)
        data = connection.receive_message(op_code, request_id)

Can raise ConnectionFailure or OperationFailure.

:param handler: A _MongoClientErrorHandler.
Nr   r   r  r  r6  )#rn   ro   ra   rb   rq   $publish_connection_check_out_startedr^   rr   r&   r>  r?  r@  r(   r'   CHECKOUT_STARTEDr   	_get_connpublish_connection_checked_outr_   CHECKOUT_SUCCEEDEDr  r  r  r   r  r   r   sysexc_infohandler   r   r  r  r  )
r   r  rp   checkout_started_timer]   rB  pinnedexc_typeexc_val_s
             rQ   checkoutPool.checkout,  s    & II..	 $ 0  (((::4<<H##(:(G(G(V(V"0AA<<?<<? ^^$9^KK>>#&;;  (((44T\\477HU##(:(G(G(V(V"0CC<<?<<?#'77#	yyy$$(()<)<= !yJ  ??yyy%%))$/

a
 !yy yyy%%))$/" !yy [[,,t$$$ W L" !yyy  	
 __:(:(:F (+||~$1nnX777dkkll4(((	 !yyy !yyy %s  C,P.L/C"PL? "L#L? &&L$L? L!	L? !!POP1O7PO&P)O8*P-1O>P)O;**PPPL? !L? $L<*L-+L<7L? ?AON1OOOPPO5$O'%O51P;P>PPPPc                   U R                   [        R                  :w  GaK  U(       Ga  [        R                  " 5       U-
  nU R
                  (       aX  U R                  R                  c   eU R                  R                  R                  U R                  [        R                  U5        U R                  (       a}  [        R                  " [        R                   5      (       aT  [#        [        [$        R&                  U R(                  U R                  S   U R                  S   S[        R                  US9  [+        U R                  5      n[-        U R                  [/        S5      US9  g g )Nr   r   <An error occurred while trying to establish a new connectionr"  r7  r8  r9  rI  r  r;  zconnection pool pausedri  )r  r  r  ra   rb   rq   rn   ro   #publish_connection_check_out_failedr^   r*   
CONN_ERRORrr   r&   r>  r?  r@  r(   r'   CHECKOUT_FAILEDr   r2   r3   r   )r   r*  
emit_eventrB  rm  s        rQ   _raise_if_not_readyPool._raise_if_not_ready{  s   ::(>>+.CC((9955AAAII..RR&D&O&OQY ++0B0O0OPWP]P]0^0^* 8 H H!%#'<<?#'<<?]<GG#+	 +4995G%m,DEW^+ )rS   c                T  #    U R                   [        R                  " 5       :w  a  U R                  5       I Sh  vN   U R                  (       Ga  [
        R                  " 5       U-
  nU R                  (       aX  U R                  R                  c   eU R                  R                  R                  U R                  [        R                  U5        U R                  (       a}  [        R                   " ["        R$                  5      (       aT  ['        [        [(        R*                  U R,                  U R                  S   U R                  S   S[        R                  US9  [/        S5      eU R0                   ISh  vN   U =R2                  S-  sl        SSS5      ISh  vN   [4        R6                  " 5       (       a  [4        R8                  " 5       nOJU R                  R:                  (       a-  [
        R                  " 5       U R                  R:                  -   nOSnU R<                   ISh  vN   U R?                  USS9  U R@                  U RB                  :  d  U(       a  U[
        R                  " 5       -
  OSn[E        U R<                  U5      I Sh  vN (       dE  U R@                  U RB                  :  a  U R<                  RG                  5         U RI                  U5        U R?                  USS9  U R@                  U RB                  :  d  M  U =R@                  S-  sl         SSS5      ISh  vN   SnS	nS	n U R0                   ISh  vN   U =RJ                  S-  sl%        SnSSS5      ISh  vN   UGc  U RL                   ISh  vN   U R?                  US	S9  U RN                  (       d  U RP                  U RR                  :  d  U(       a  U[
        R                  " 5       -
  OSn[E        U RL                  U5      I Sh  vN (       dX  U RN                  (       d  U RP                  U RR                  :  a  U RL                  RG                  5         SnU RI                  U5        U R?                  US	S9  U RN                  (       d  U RP                  U RR                  :  d  M   U RN                  RU                  5       nSSS5      ISh  vN   U(       a$  U RY                  U5      I Sh  vN (       a  SnGM  Ol U R[                  US
9I Sh  vN nU RL                   ISh  vN   U =RP                  S-  sl(        U RL                  RG                  5         SSS5      ISh  vN   Uc  GM  SUl3        U$  GN? GN GN! , ISh  vN  (       d  f       GN= f GNn GN GNp! , ISh  vN  (       d  f       GN= f GNr GNN! , ISh  vN  (       d  f       GNd= f GNS GN! [V         a    U =RP                  S-  sl(         GNGf = f GN@! , ISh  vN  (       d  f       GNV= f GN> GN GN N! , ISh  vN  (       d  f       N= f! U RL                   ISh  vN    U =RP                  S-  sl(        U RL                  RG                  5         SSS5      ISh  vN    f ! , ISh  vN  (       d  f       f = f= f! [\         Ga    U(       a(  UR_                  [`        Rb                  5      I Sh  vN    U R<                   ISh  vN    U =R@                  S-  sl         U(       a  U =RJ                  S-  sl%        U R<                  RG                  5         SSS5      ISh  vN    O! , ISh  vN  (       d  f       O= fU(       Gd  [
        R                  " 5       U-
  nU R                  (       aX  U R                  R                  c   eU R                  R                  R                  U R                  [        Rd                  U5        U R                  (       a}  [        R                   " ["        R$                  5      (       aT  ['        [        [(        R*                  U R,                  U R                  S   U R                  S   S[        Rd                  US9  e f = f7f)z=Get or create a AsyncConnection. Can raise ConnectionFailure.Nr   r   zConnection pool was closedr3  z?Attempted to check out a connection from closed connection poolT)r7  Fr!  r2  )4r  r  r  r  r`   ra   rb   rq   rn   ro   r4  r^   r*   r  rr   r&   r>  r?  r@  r(   r'   r6  r   r  r  r  r   get_timeoutget_deadlinewait_queue_timeoutr  r8  r  r  r#   r	  _raise_wait_queue_timeoutr  r  r  r  r  popleft
IndexError	_perishedr
  r  r   r+   rj  r5  r   )	r   r*  r  rB  deadliner   r]   r  emitted_events	            rQ   r$  Pool._get_conn  s     88ryy{"**,,,;;;~~'*??H$$yy11===		**NNLL"@"L"Lh '',>,K,KGMM,Z,Z&4DD!__#||A#||A78DD'	 #Q  999  A%  9 ))+HYY))~~'$))*F*FFHH>>>$$%:t$L}}t'9'999A(T^^%55t-dnngFFF }}t'9'99--/223HI(()>4(P }}t'9'99 MMQM "> @	yyy##q(#" !y ,  444,,-Bu,U#zzT]]T=Q=Q-QAI(T^^-="=t%5d6O6OQX%YYY  $zzT]]T=Q=Q-Q $ 9 9 @ @ B,0M ::;PQ001FSX0Y  $zzT]]T=Q=Q-Q+#zz113 54" !^^D111#  2?%)\\'\%BB#'#<#<#< MMQ.M 55<<> $=#<9 ,| o -0 999 " G	 ">>>$ !yyy 5  Z & +*+ 5444$ 2
  C#<#<#<#<4#<#<#< MMQ.M 55<<> $=#<#<#<#<#<  	oo&<&B&BCCC~~~"''1,'%%'	 &~~~~~ !>>+.CC((9955AAAII..RR&D&O&OQY ++0B0O0OPWP]P]0^0^* 8 H H!%#'<<?#'<<?]<GG#+	 9	s  7b(U8E b(:U;;b(>Vb(U> Bb(.V/b(2A%V%VA8V%V%'b(2V"3
b(>[ W [ W*[ 5W6[ W![ A6XW$	BX(W'[ X[ -X+.[ >Y X.Y [ %X1&[ )0X6[ $X4%[ /
b(;b(>b(VV
V	b(V%"b(%V=+V.,V=8b( [ [ WWW	[ $X'X
X	X

X[ X(XX(#	[ .Y 1[ 4[ 6Y<X?=Y	[ [!Y$
"[&0Z([!Z$"[(Z?.Z1/Z?;[[ 4b%9[<:b%\b%A]2 b%+].,b%2^		8];9^		D b%%b(c                
  #    UR                   nUR                  nSUl        SUl         SUl        U R                  R	                  U5        U R
                  R                  nU R                   ISh  vN   U R                  R	                  UR                  5        SSS5      ISh  vN   U R                  (       a+  Uc   eUR                  U R                  UR                  5        U R                  (       aw  [        R                   " ["        R$                  5      (       aN  ['        [        [(        R*                  U R,                  U R                  S   U R                  S   UR                  S9  U R.                  [0        R2                  " 5       :w  a  U R5                  5       I Sh  vN   GO2U R6                  (       a)  UR9                  [:        R<                  5      I Sh  vN   GOUR6                  (       a  U R                  (       a:  Uc   eUR?                  U R                  UR                  [:        R@                  5        U R                  (       a  [        R                   " ["        R$                  5      (       au  ['        [        [(        RB                  U R,                  U R                  S   U R                  S   UR                  [E        [:        R@                  5      [:        R@                  S9  OSnU R                   ISh  vN   U RG                  URH                  URJ                  5      (       a  SnOiURM                  5         URO                  [Q        U RR                  5      5        U RT                  RW                  U5        U RX                  R[                  5         SSS5      ISh  vN   U(       a'  UR9                  [:        R\                  5      I Sh  vN   U R^                   ISh  vN   U(       a  U =R`                  S-  sl0        OU(       a  U =Rb                  S-  sl1        U =Rd                  S-  sl2        U =Rf                  S-  sl3        U =Rh                  S-  sl4        U R^                  R[                  5         SSS5      ISh  vN   g GN% GN! , ISh  vN  (       d  f       GN	= f GN GN GN GN! , ISh  vN  (       d  f       GN+= f GN N NR! , ISh  vN  (       d  f       g= f7f)zvReturn the connection to the pool, or if it's closed discard it.

:param conn: The connection to check into the pool.
FNr   r   r  rH  T)5r   r   r   r  r  rn   ro   r  r  r   rq   publish_connection_checked_inr^   r_   rr   r&   r>  r?  r@  r(   r'   	CHECKEDINr   r  r  r  r  r`   r   r+   r  rK  rj  rL  r)   r   r}   r   r`  rc  r  re   r  r  r  r	  r   r  r  r  r  r  r  )r   r]   txncursorrp   r   s         rQ   r   Pool.checkin  sA    
 oo##"%%d+II..	999  (()<)<= 9  (((33DLL$''J##(:(G(G(V(V"0::<<?<<?#'77 88ryy{"**,,,{{oo&<&H&HIII(($00077dgg/E/K/K ++0B0O0OPWP]P]0^0^* 8 D D!%#'<<?#'<<?+/77?@V@\@\]4::	 #
999 ,,T__dooNN%)
557//T5E5E0FG

--d31188: %9 //*@*F*FGGG>>>

a
"MMQM1$  A% NN!!# ">>k 999 - J* %999 H!>>>s  A/S?1R2S?5&RS?&R'C>S?%R7&9S?R: D$S?R=S?BS S?+S ,-S?SS?.S!/S?2BS%S?S#S?S?R4"R%#R4/	S?:S?=S? S?S	S
S	S?!S?#S?%S<+S.,S<8S?c                `  #    UR                  5       nU R                  R                  bA  X R                  R                  :  a(  UR                  [        R
                  5      I Sh  vN   gU R                  b\  U R                  S:X  d  X R                  :  a=  UR                  5       (       a(  UR                  [        R                  5      I Sh  vN   gU R                  UR                  UR                  5      (       a(  UR                  [        R                  5      I Sh  vN   gg N N[ N
7f)aY  Return True and close the connection if it is "perished".

This side-effecty function checks if this socket has been idle for
for longer than the max idle time, or if the socket has been closed by
some external network error, or if the socket's generation is outdated.

Checking sockets lets us avoid seeing *some*
:class:`~pymongo.errors.AutoReconnect` exceptions on server
hiccups, etc. We only check if the socket was closed by an external
error if it has been > 1 second since the socket was checked into the
pool, to keep performance reasonable - we can't avoid AutoReconnects
completely anyway.
NTr   F)rf  rn   r  r   r+   r  r  rW  rj  r   r}   r   r   )r   r]   rf  s      rQ   rA  Pool._perisheda  s      !224 II++7!II$C$CC//"8"="=>>>''3((A-1BEaEa1a!!oo&<&B&BCCC  $//BB//"8">">??? ? D @s8   A#D.%D(&A(D.D*AD.!D,"D.*D.,D.c                   U R                   R                  n[        R                  " 5       U-
  nU R                  (       a0  Uc   eUR                  U R                  [        R                  U5        U R                  (       a}  [        R                  " [        R                  5      (       aT  [        [        [        R                   U R"                  U R                  S   U R                  S   S[        R                  US9  [$        R&                  " 5       =(       d    U R                   R(                  nU R                   R*                  (       al  U R,                  U R.                  -
  U R0                  -
  n[3        SR5                  U R                   R6                  U R.                  U R0                  UU5      5      e[3        SU R                   R6                   SU 35      e)Nr   r   zBWait queue timeout elapsed without a connection becoming availabler3  zTimeout waiting for connection from the connection pool. maxPoolSize: {}, connections in use by cursors: {}, connections in use by transactions: {}, connections in use by other operations: {}, timeout: {}zMTimed out while checking out a connection from connection pool. maxPoolSize: z, timeout: )rn   ro   ra   rb   rq   r4  r^   r*   TIMEOUTrr   r&   r>  r?  r@  r(   r'   r6  r   r   r;  r=  r   r  r  r  r    r}  r  )r   r*  rp   rB  r   	other_opss         rQ   r>  Pool._raise_wait_queue_timeout  sp   II..	>>#&;;  (((99<DDh ##(:(G(G(V(V"0@@<<?<<?[4<<#	 ##%E)E)E99""++dmm;djjHI'7 8>vII++MMJJ8	  $ II334KyJ
 	
rS   c                d    [         (       a%  U R                   H  nUR                  S 5        M     g g r   )rT  r  r   )r   r]   s     rQ   __del__Pool.__del__  s(     8

% # rS   )__pinned_socketsr  r   r  r  r  r  r  r^   r  rq   rr   rz   r  re   r  r  r  r  r  r  rn   r  r  r  r  )TN)r^   rG   r  r/   r  r  r  r  r  r  )TNF)
rP  r  r  r  r   r  r  r  r  r  )re   zOptional[bool]r  r  )NF)r   r  r  r  r  r  r  )r  r   r  r  r   )r  "Optional[_MongoClientErrorHandler]r  rW   )r  rU  r  z%AsyncGenerator[AsyncConnection, None])r*  r  r7  r  r  r  )r*  r  r  rU  r  rW   )r]   rW   r  r  )r]   rW   r  r  )r*  r  r  r	   )r  r  r  r  r   r~   propertyr`   r  rc  r  r  rP  r   r  r
  
contextlibasynccontextmanagerr/  r8  r$  r   rA  r>  rR  r  rU   rS   rQ   r  r    s^   
 (,TT T 	T
 &Tl" . . )-&+ODOD OD '	OD
  $OD 
ODb= TY
,
LP
	
4&/=,~Qf ##<@L%9L%	.L% $L%\6 [_%*5W	BI$V"H%
N&rS   r  )rO   r   r  r  )w
__future__r   r  rW  r?  r  r'  ra   rZ   typingr   r   r   r   r   r	   r
   r   r   bsonr   pymongor   r   #pymongo.asynchronous.client_sessionr   pymongo.asynchronous.helpersr   pymongo.asynchronous.networkr   pymongo.commonr   r   r   r   r   pymongo.errorsr   r   r   r   r   r   r   r   r    pymongo.hellor!   r"   pymongo.lockr#   r$   r%   pymongo.loggerr&   r'   r(   r)   pymongo.monitoringr*   r+   pymongo.network_layerr,   r-   r.   pymongo.pool_optionsr/   pymongo.pool_sharedr0   r1   r2   r3   r4   pymongo.read_preferencesr5   pymongo.server_apir6   pymongo.server_typer7   pymongo.socket_checkerr8   pymongo.ssl_supportr9   r:   bson.objectidr;   pymongo.asynchronous.authr<   r=   !pymongo.asynchronous.mongo_clientr>   r?   pymongo.compression_supportr@   rA   rB   pymongo.messagerC   rD   pymongo.read_concernrE   rF   pymongo.typingsrG   rH   pymongo.write_concernrI   rM   rJ   rK   rL   rR   ImportErrorrT  rW   r  r  r  r  rU   rS   rQ   <module>rw     s5   #    	 
  
 
 
 ' ) O 7 0 
 
 
 - 
  a ` ,  4 . + 0 (!&6F\ 
 10462E99/ V
 V
r| + +> n& n&c  EE	Es   3E: :FF