
    iE                       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	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/J0r0J1r1J2r2  S SK3J4r4J5r5  S SK6J7r7J8r8  S SK9J:r:J;r;J<r<  S SK=J>r>J?r?J@r@JArA  S SKBJCrCJDrD  S SKEJFrFJGrGJHrH  S SKIJJrJ  S SKKJLrLJMrMJNrNJOrO  S SKPJQrQ  S SKRJSrS  S SKTJUrU  S SKVJWrW  \(       aL  S SKJXrX  S SKYJZrZ  S SK[J\r\  S SKJ]r]  S SK^J_r_J`r`  S SKaJbrbJcrcJdrd  S SKeJfrfJgrg  S SKhJiri  S SKPJjrj  S S KkJlrlJmrm  S S!KnJoro  S"rp " S# S$5      rq " S% S&\05      rr " S' S(5      rs " S) S*5      rt " S+ S,5      rug)-    )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NetworkTimeoutNotPrimaryErrorOperationFailurePyMongoErrorWaitQueueTimeoutError_CertificateError)HelloHelloCompat)_get_timeout_detailsformat_timeout_details)_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)	SSLErrors_CancellationContext_configured_protocol_interface_raise_connection_failure)ReadPreference)_add_to_command)SERVER_TYPE)SocketChecker)CodecOptions)ObjectId)_AuthContext)AsyncClientSession)AsyncMongoClient_MongoClientErrorHandler)SnappyContextZlibContextZstdContext)_OpMsg_OpReply)ReadConcern)_ServerMode)_Address_CollationIn)WriteConcernFc                  *   \ 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)@AsyncConnections   a  Store 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
:param is_sdam: SDAM connections do not call hello on creation
c                   [         R                  " U5      U l        Xl        X0l        X@l        XPl        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R2                  R4                  U l        UR8                  U l        UR:                  U l        UR2                  R<                  U l        S U l         [C        5       U l"        S U l#        S U l$        S U l%        URL                  U l'        U RN                  RQ                  5       U l)        SU l*        [W        5       U l,        UR2                  U l        SU l-        S U l.        S U l/        SU l0        SU l1        SU l2        U R2                  Rf                  U l4        SU l5        URl                  U l6        [        R                  " 5       U l7        S U l8        g )NFg        )9weakrefrefpool_refconnaddressidis_sdam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_contextr;   socket_checkeroidc_token_gen_idnegotiated_mechsauth_ctxgenpool_genget_overall
generationreadyr5   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)selfrS   poolrT   rU   rV   s         P/home/ubuntu/wiki/venv/lib/python3.13/site-packages/pymongo/asynchronous/pool.py__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//!^^-!    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~   rS   get_conn
settimeout)r   timeouts     r   set_conn_timeout AsyncConnection.set_conn_timeout   s/    '''#		%%g.r   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   	remainingrw   r   re   r}   get_rttr   r%   r&   r   r]   int)	r   clientcmdr   rttmax_time_mstimeout_details	formattedr   s	            r   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&r   c                8    SU l         U R                  (       a   eg NT)rz   r{   r   s    r   pin_txnAsyncConnection.pin_txn   s    %%%%%r   c                8    SU l         U R                  (       a   eg r   )r{   rz   r   s    r   
pin_cursorAsyncConnection.pin_cursor   s    !??""?r   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)rR   checkin
close_connr/   STALE)r   r   s     r   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)re   
server_apirb   load_balancedrd   r$   CMD
LEGACY_CMDr   s    r   	hello_cmdAsyncConnection.hello_cmd   sO     994==DII4K4K"&DOOQ''**Ay$??r   c                B   #    U R                  S S 5      I S h  vN $  N7fr   )_hellor   s    r   helloAsyncConnection.hello   s     [[t,,,,s   c                  #    U R                  5       nU R                  (       + nSnSUS'   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)NFTbackpressurer   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   r[   re   metadatark   compressorsr   r   connect_timeoutr   _credentials	mechanismusernamesourcepymongo.asynchronousr   r>   from_credentialsrT   speculate_commandrX   rY   r   r   r#   r\   r]   r^   r_   r`   logical_session_timeout_minutesis_readablera   rb   server_typer:   	RSPrimaryRSSecondary	RSArbiterRSOtherRSGhostis_repl
Standaloneis_standaloneMongosrc   get_compression_contextrl   rd   connection_idry   sasl_supported_mechsro   parse_responsespeculate_succeededrp   rx   r   rr   getrt   )r   topology_versionheartbeat_frequencyr   performing_handshaker   credsr   rp   speculative_authenticatestartdocr   ctxs                 r   r   AsyncConnection._hello   s    
 nn#'#;#;;	"N'+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_messagerw   unpack_responser   _check_command_responser]   )r   replyunpacked_docsresponse_docs       r   _next_replyAsyncConnection._next_replyO  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_timerg   boolrd   _raise_if_not_writabler   rc   rT   r^   rl   r   r   BaseExceptionr7   )r   dbnamespecread_preferencecodec_optionscheckallowable_errorsread_concernr  r   r   sessionr   retryable_writer   r  r   rg   r  errors                       r   r   AsyncConnection.commandW  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.)r^   r   r2   rS   r   r  r7   )r   messagemax_doc_sizer  s       r   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)r1   r_   r  r7   )r   
request_idr  s      r   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)r\   r   )r   r  s     r   r  &AsyncConnection._raise_if_not_writable  s+     $"2"2!-]\a1bcc #3>r   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      r   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   r]   )r   r!  r&  r  r   results         r   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)r[   rp   ru   re   r   r   r   authenticaterX   rY   r   rh   rg   publish_connection_readyrT   rU   ri   r*   isEnabledForloggingDEBUGr,   r+   
CONN_READYr   )r   r/  r   r   durations        r   r6  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      r   r   AsyncConnection.validate_session  s-     f,&T  - r   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  r1  r2  r3  r4  reasonr  )rW   _close_connrh   rg   publish_connection_closedrT   rU   ri   r*   r8  r9  r:  r,   r+   CONN_CLOSEDr   r-   )r   rC  s     r   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)rW   rv   cancelrS   close	Exceptionr   s    r   rD  AsyncConnection._close_conn6  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_SYNCrm   socket_closedrS   r   
is_closingr   s    r   conn_closedAsyncConnection.conn_closedC  s;    8&&44TYY5G5GHH99''))r   c                6    U(       a  UR                  X5        gg)zAdd $clusterTime.N)_send_cluster_time)r   r   r  r   s       r   r  !AsyncConnection.send_cluster_timeJ  s     %%g7 r   c                z    U R                   R                  (       a   [        XR                   R                  5        gg)zAdd server_api parameters.N)re   r   r9   )r   r   s     r   r
  AsyncConnection.add_server_apiT  s&    99GYY%9%9:  r   c                8    [         R                  " 5       U l        g r   rX   rY   rZ   r   s    r   update_last_checkin_time(AsyncConnection.update_last_checkin_timeY  s    !%!1r   c                    Xl         g r   )r\   )r   r\   s     r   update_is_writable"AsyncConnection.update_is_writable\  s    &r   c                F    [         R                  " 5       U R                  -
  $ )z9Seconds since this socket was last checked into its pool.rY  r   s    r   idle_time_seconds!AsyncConnection.idle_time_seconds_  s    ~~$"8"888r   c                "  #    U R                   (       a  S nO[        R                  nU R                  U5      I S h  vN   [	        U[
        [        /[        Q75      (       a*  [        U R                  5      n[        U R                  XS9  g e  NQ7f)Nr   )ru   r/   ERRORr   r  IOErrorOSErrorr4   r%   re   r7   rT   )r   r  rC  detailss       r   r7   )AsyncConnection._raise_connection_failurec  sk       ::F+11Foof%%%egw;;<<*4995G%dllES 	&s   9BBABc                4    U R                   UR                   :H  $ r   )rS   r   others     r   __eq__AsyncConnection.__eq__  s    yyEJJ&&r   c                    X:X  + $ r    rj  s     r   __ne__AsyncConnection.__ne__  s      r   c                ,    [        U R                  5      $ r   )hashrS   r   s    r   __hash__AsyncConnection.__hash__  s    DIIr   c                    SR                  [        U R                  5      U R                  =(       a    S=(       d    S[	        U 5      5      $ )NzAsyncConnection({}){} at {}z CLOSED )formatreprrS   rW   rU   r   s    r   __repr__AsyncConnection.__repr__  s8    ,33OKK%I+tH
 	
r   )-r   r   r|   rT   rp   rv   rW   rl   rk   rS   r   r   rh   ri   rt   rb   rU   rc   r   rV   r   r\   rZ   r~   rg   r   r^   r_   r]   r`   rw   ro   rn   rd   re   r[   r{   rz   rr   rR   ru   ry   rx   rm   ra   )
rS   r0   r   PoolrT   ztuple[str, int]rU   r   rV   r  )r   Optional[float]returnNone)r   zAsyncMongoClient[Any]r   z"Optional[MutableMapping[str, Any]]r~  r}  r~  r  )r~  dict[str, Any])r~  Hello[dict[str, Any]])r   zOptional[Any]r   Optional[int]r~  r  )"r  strr  MutableMapping[str, Any]r  rH   r  CodecOptions[Mapping[str, Any]]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[Any]]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)r/  r  r~  r  )r   r  r  r  r~  r  )rC  zOptional[str]r~  r  r~  r  )r   r  r  r  r   r  r~  r  )r   r  r~  r  )r\   r  r~  r  )r~  float)r  r  r~  r	   )rk  r   r~  r  r~  r   )r~  r  ))__name__
__module____qualname____firstlineno____doc__r   r   r   r   r   r   r   r   r   r   r   r8   PRIMARYr   r   r  r   r  r'  r,  r6  r  r   rD  rQ  r  r
  rZ  r]  r`  r7   rl  rp  rt  rz  __static_attributes__ro  r   r   rM   rM   s   sk   9"&9" 9" !	9"
 9" 9"v/+2T	>&#@@-T'T +T 
	Tl 
 (6'='=9N@D.204*/,00426 %#37 %#W8W8 'W8 %	W8
 7W8 W8 >W8 ,W8 .W8 $(W8 *W8 .W8 0W8 W8 W8  1!W8" #W8$ 
%W8 W8r8"	8d	3$):Y	$ D5@\	**8)8 .8 0	8
 
8;
2'98'!
r   rM   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.
ro  N)r  r  r  r  r  r  ro  r   r   r  r    s    r   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    r   r   _PoolGeneration.__init__  s    1<1H1H1Mr   c                >    Uc  U R                   $ U R                  U   $ )z,Get the generation for the given service_id.r  r  r   rx   s     r   r   _PoolGeneration.get  s%    ###  ,,r   c                    U R                   $ )z"Get the Pool's overall generation.)r  r   s    r   rs   _PoolGeneration.get_overall  s    r   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     r   inc_PoolGeneration.inc  sW    A"//
!!*-2- 0 j)Q.)r   c                (    XR                  U5      :g  $ )z?Return if the given generation for a given service_id is stale.)r   r   rq   rx   s      r   stale_PoolGeneration.stale  s    hhz***r   r  Nr  )rx   Optional[ObjectId]r~  r   r  )rx   r  r~  r  rq   r   rx   r  r~  r  )
r  r  r  r  r   r   rs   r  r  r  ro  r   r   r  r    s    - /+r   r  c                       \ rS rSrSrSrSrSrg)	PoolStatei  r         ro  N)r  r  r  r  PAUSEDREADYCLOSEDr  ro  r   r   r  r    s    FEFr   r  c                  \   \ 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 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        SU l!        U R0                  RD                  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 is_sdam: whether to call hello for each new AsyncConnection
r   r   Ninfr  r1  r2  r3  )2pause_enabledr  r  stater  _check_interval_secondsr  dequeconnssetactive_contextsr)   lockr(   _max_connecting_condactive_socketsnext_connection_idr\   r  rq   osgetpidpidrT   re   rV   rf   rh   ri   	size_condrequestsmax_pool_sizer  _pendingmax_connecting_max_connectingr   publish_pool_creatednon_default_optionsr*   r8  r9  r:  r,   r+   POOL_CREATEDoperation_count_Pool__pinned_socketsncursorsntxns)r   rT   optionsrV   	client_ids        r   r   Pool.__init__  sD      "))DJ"DJ ()$ :E9J9J9L
:=%&(	$;DII$F!"#+/
 #$99;	  <		**$6<		**;; 	
 (,||#3 
 1;!YY44!!!&uD %<DII$F!#yy77#  99--999II&&;;dii;; ##(:(G(G(V(V"0==<<?<<? ))// %& 7:e
r   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  rh   re   rf   publish_pool_readyrT   ri   r*   r8  r9  r:  r,   r+   
POOL_READYr   r   s    r   ru   
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    r   rW   Pool.closed#  s    zzY----r   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(       Ga8  [4        (       dL  [6        R8                  " W V
s/ s H!  oR;                  [<        R>                  5      PM#     sn
SS06I S h  vN   O0W H*  n
U
R;                  [<        R>                  5      I S h  vN   M,     U R@                  (       a   Uc   eURC                  U RD                  5        U RF                  (       an  [H        RJ                  " [L        RN                  5      (       aD  [Q        [H        [R        R>                  U RT                  U RD                  S   U RD                  S   S9  g g g U[        R                  :w  a  U R@                  (       a   Uc   eURW                  U RD                  UUS9  U RF                  (       am  [H        RJ                  " [L        RN                  5      (       aD  [Q        [H        [R        RX                  U RT                  U RD                  S   U RD                  S   US9  [4        (       dL  [6        R8                  " W V
s/ s H!  oR;                  [<        RZ                  5      PM#     sn
SS06I S h  vN   g W H*  n
U
R;                  [<        RZ                  5      I S h  vN   M,     g  GN GN GN! , IS h  vN  (       d  f       GN= fs  sn
f  GN[ GN1s  sn
f  Nj N?7f)Nr   return_exceptionsTr   r  )rx   interrupt_connections)r  r1  r2  r3  	serviceId).r  r  rW   re   r  r   r  r  rq   r  r  r  r  r  r  r  r  r  rx   appendr  r  
notify_allr  rI  rf   rN  asynciogatherr   r/   POOL_CLOSEDrh   publish_pool_closedrT   ri   r*   r8  r9  r:  r,   r+   r   publish_pool_clearedPOOL_CLEAREDr   )r   rJ  pauserx   r  	old_statenewpidsocketsdiscardkeeprS   contextrg   s                r   _resetPool._reset'  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..	 8nnV]^V]doo&<&H&HIV]^&*  
 $D//*@*L*LMMM $$$ ,,,--dll;'',>,K,KGMM,Z,Z&4@@!__#||A#||A -[' I,,,(($00022#-.C 3 
 ++0B0O0OPWP]P]0^0^* 8 E E!%#'<<?#'<<?", 8nnPWXPWoo&<&B&BCPWX&*  
 $D//*@*F*FGGG $e ">>>>R _ N@ Y Hs   S?SS?SS?S
S?GSS?!S"AS?#(S+
S?S0,S?S3FS?(S6
S?S;,S?=S=>
S?
S?S?S(SS(#S?3S?6S?=S?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)r\   r  r  r]  )r   r\   _sockets      r   r]  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)rJ  rx   r  r  )r   rx   r  s      r   reset
Pool.reset  s'      kkJ  
 	
 	
   c                @   #    U R                  SSS9I S h  vN   g  N7f)NF)rJ  r  r  r   s    r   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)rJ  r  r   s    r   rJ  
Pool.close  s     kkk%%%s   c                8    U R                   R                  X5      $ r   )rq   r  r  s      r   stale_generationPool.stale_generation  s    xx~~c..r   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be  / 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   [        (       dL  [        R                  " U Vs/ s H!  o3R                  [        R                  5      PM#     snSS06I Sh  vN   O0U 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*                  R1                  5         SSS5      ISh  vN   U R                    ISh  vN   U =R(                  S-  sl        U R                   R1                  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 R3                  5       I Sh  vN nSnU R                    ISh  vN   U R4                  R7                  5       U:w  a  SnU(       d@  U R                  R9                  U5        U R:                  R=                  UR>                  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*                  R1                  5         SSS5      ISh  vN   U R                    ISh  vN   U =R(                  S-  sl        U R                   R1                  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*                  R1                  5         SSS5      ISh  vN   U R                    ISh  vN   U =R(                  S-  sl        U R                   R1                  5         SSS5      ISh  vN   GM   GN GN GN! , ISh  vN  (       d  f       GN= f GN GN! , ISh  vN  (       d  f       GN= fs  snf  GN GNe GNN GN GN GN! , ISh  vN  (       d  f       GN= f GN GN GNu GN9! , ISh  vN  (       d  f       GNO= f GNB 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Ny! , ISh  vN  (       d  f       GN= f GNi GNO 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Np! , 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*                  R1                  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                   R1                  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.
Nr  Tr   F)!r  r  r  r  re   max_idle_time_secondsr  r`  r  poprN  r  r  r   r/   IDLEr  lenr  min_pool_sizer  r  r  r  notifyconnectrq   rs   
appendleftr  r  rv   r   )r   reference_generationclose_connsrS   incrementedr   s         r   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 8nnOZ[{too&<&A&AB{[&*  
 (D//*@*E*EFFF ( ~~~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 \ G &~~~~ 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))+ *>>>>>sA  aW$a W-aW'aW*.aXaB6X>a	X
#a-(X)
aX. ,aX1a&X4'a*<Y &a1X72a7%Y a'X:(a-Y aX=a]* %Y&]* )Z%]* Y]* a,Y!-a00Y' a+Y$,a Za0Z4a?Z aZ%]* 'Z"(]* ?[  ]* []* A([	]* [-]* ;[$<]* a['a0[-a[*a,\-a00\ a+\,a2a	\(
a0\.=a\+	a]	a!0]a]a'a*a-X3W64X 	aaX&XX&!a1a4a7a:a=a YY	Ya]* ]* !a$a'Y?-Y0.Y?:	aaZZZa"]* %Z=+Z.,Z=8	]* ]* ]* 	[![[!	]* 'a*a-\3[64\ 	aa\%\\%!a+a.]4\75]	aa]']]'"a*a^
a0_	7a_a	_ __ a3_6
4a80`:(a3`64a:a aaaac                   U R                   (       d  [        U5      [        [        4;  a  g [	        U[        5      (       d   e[	        UR
                  [        [        [        R                  45      (       a:  [	        UR
                  [        R                  [        R                  45      (       d  g UR                  S5        UR                  S5        g )NSystemOverloadedErrorRetryableError)rV   typer   r   r  	__cause__r"   r4   socketgaierrorsslSSLEOFErrorSSLZeroReturnError_add_error_label)r   r  s     r   _handle_connection_errorPool._handle_connection_error  s     <<4;}n.MM%////eoo(99foo'VWW eooAWAW/XYY67/0r   c                
  #    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[E        XPU R                  WU RF                  5      nU R                    ISh  vN   U R                  R	                  URH                  5        U R                  R+                  W5        SSS5      ISh  vN   WRJ                  (       a  URH                  RM                  5         Sn	 U RF                  (       d+  URO                  5       I Sh  vN   Sn	URP                  U l(        U(       a  URS                  USS	9  URU                  5       I Sh  vN   U(       a7  URX                  RZ                  R]                  UR^                  5      I Sh  vN   U$  GN GNW! , ISh  vN  (       d  f       GNm= f GN! [(         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  U R7                  U5        [9        U[:        [<        /[>        Q75      (       a)  [A        U R
                  5      n[C        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N GN@! [(         a  n
U R                    ISh  vN    U R                  R+                  URH                  5        SSS5      ISh  vN    O! , ISh  vN  (       d  f       O= fU	(       d  U R7                  U
5        URW                  [.        R0                  5      I Sh  vN    e Sn
A
f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  r1  r2  r3  r4  rB  rc  FT)completed_handshake)0r  r  r5   r  addre   rf   rh   publish_connection_createdrT   ri   r*   r8  r9  r:  r,   r+   CONN_CREATEDr   r6   r  r  rE  r/   rd  rF  r-   r  r  re  rf  r4   r%   r7   rM   rV   rv   	cancelledrI  r   r\   contribute_socketr6  r   r   	_topologyreceive_cluster_timer   )r   handlerconn_idtmp_contextrg   networking_interfacer  rg  rS   completed_helloes              r   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<<RVR^R^_999  $$T%8%89  ((5 9   &&(	<<jjl"""&#'#3#3 ))$E)J##%%% ..**??@R@R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	 ))%0%'7!?Y!?@@.tyy9)$,,W/	4 999 # & 	yyy$$,,T-@-@A !yyyyy"--a0//"8">">???	 Ts  U8J=U8AKU8)K *C
U85#K! KK! 2U8RU8ARU8R 1U8$R; 6R57AR; 8R89R; =9U86U57U8 U8K	K
KU8K! !R,R<K?=RL/R(L+)R/M	5L86M	E
RRU8U8R2 R#!R2-U85R; 8R; ;
U2U-SU-&T U-TU-T)	TT)	%AU-&U)'U--U22U8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'  r0  )#re   rf   rX   rY   rh   $publish_connection_check_out_startedrT   ri   r*   r8  r9  r:  r,   r+   CHECKOUT_STARTEDr   	_get_connpublish_connection_checked_outrU   CHECKOUT_SUCCEEDEDr  r  r   rv   r  rz   r{   sysexc_infohandler|   r   r  r  r  )
r   r'  rg   checkout_started_timerS   r<  pinnedexc_typeexc_val_s
             r   checkoutPool.checkoutH  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  r1  r2  r3  rC  r  r5  zconnection pool pausedrc  )r  r  r  rX   rY   rh   re   rf   #publish_connection_check_out_failedrT   r.   
CONN_ERRORri   r*   r8  r9  r:  r,   r+   CHECKOUT_FAILEDr   r%   r7   r   )r   r8  
emit_eventr<  rg  s        r   _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^+ )r   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 closedrA  z?Attempted to check out a connection from closed connection poolT)rE  Fr/  r@  )4r  r  r  r  rW   rX   rY   rh   re   rf   rB  rT   r.   r  ri   r*   r8  r9  r:  r,   r+   rD  r   r  r  r  r   get_timeoutget_deadlinewait_queue_timeoutr  rF  r  r  r'   r  _raise_wait_queue_timeoutr  r  r  r  r  popleft
IndexError	_perishedr	  r  r   r/   rd  rC  r|   )	r   r8  r'  r<  deadliner   rS   r  emitted_events	            r   r2  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  rB  T)5rz   r{   r|   r  r  re   rf   r  r  rv   rh   publish_connection_checked_inrT   rU   ri   r*   r8  r9  r:  r,   r+   	CHECKEDINr   r  r  r  r  rW   r   r/   r  rE  rd  rF  r-   r  rt   rx   rZ  r]  r  r\   r  r
  r  r  r   r  r  r  r  r  r  )r   rS   txncursorrg   r   s         r   r   Pool.checkin2  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                  nUbH  US:X  d  X#:  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)aX  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)r`  re   r  r   r/   r  r  rQ  rd  r  rt   rx   r   )r   rS   r`  check_interval_secondss       r   rO  Pool._perished}  s      !224 II++7!II$C$CC//"8"="=>>>!%!=!=!-"a'+<+U!!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 availablerA  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: )re   rf   rX   rY   rh   rB  rT   r.   TIMEOUTri   r*   r8  r9  r:  r,   r+   rD  r   r   rI  rK  r   r  r  r  r!   rx  r  )r   r8  rg   r<  r   	other_opss         r   rL  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
 	
r   c                d    [         (       a%  U R                   H  nUR                  S 5        M     g g r   )rN  r  r   )r   rS   s     r   __del__Pool.__del__  s(     8

% # r   )__pinned_socketsr  r   r  r  r  r  r  rT   r  rh   ri   rq   rV   r\   r  r  r  r  r  r  re   r  r  r  r  )FN)rT   rI   r  r3   rV   r  r  r  r  r  )TNF)
rJ  r  r  r  rx   r  r  r  r~  r  )r\   zOptional[bool]r~  r  )NF)rx   r  r  r  r~  r  r  )r  r   r~  r  )r  r  r~  r  r   )r'  "Optional[_MongoClientErrorHandler]r~  rM   )r'  rd  r~  z%AsyncGenerator[AsyncConnection, None])r8  r  rE  r  r~  r  )r8  r  r'  rd  r~  rM   )rS   rM   r~  r  )rS   rM   r~  r  )r8  r  r~  r	   )r  r  r  r  r   ru   propertyrW   r  r]  r  r  rJ  r  r  r  r	  
contextlibasynccontextmanagerr=  rF  r2  r   rO  rL  ra  r  ro  r   r   r|  r|    sd   
 (,TT T 	T
 &Tl" . . )-&+[H[H [H '	[H
  $[H 
[Hz= TY
,
LP
	
4&/C,J1Un ##<@L%9L%	.L% $L%\6 [_%*5W	BI$V#J%
N&r   r|  )v
__future__r   r  r  rf  r9  r  r  r  r5  rX   rP   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    r!   r"   pymongo.hellor#   r$   pymongo.helpers_sharedr%   r&   pymongo.lockr'   r(   r)   pymongo.loggerr*   r+   r,   r-   pymongo.monitoringr.   r/   pymongo.network_layerr0   r1   r2   pymongo.pool_optionsr3   pymongo.pool_sharedr4   r5   r6   r7   pymongo.read_preferencesr8   pymongo.server_apir9   pymongo.server_typer:   pymongo.socket_checkerr;   r<   bson.objectidr=   pymongo.asynchronous.authr>   r?   !pymongo.asynchronous.mongo_clientr@   rA   pymongo.compression_supportrB   rC   rD   pymongo.messagerE   rF   pymongo.read_concernrG   rH   pymongo.typingsrI   rJ   pymongo.write_concernrK   rN  rM   r  r  r  r|  ro  r   r   <module>r     s   #     	  
 
  
 
 
 ' ) O 7 0     - O 
  a ` ,  4 . + 0!&6F\ 
 10462 Z
 Z
z| + +> T& T&r   