
    &gO                       S r 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J	r	J
r
Jr  SSK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JrJr  SSKJr  SSKJr  SSK J!r!  SSK"J#r#  SSK$J%r%  \	(       a  SSK&J'r'J(r(J)r)  SSK*J+r+  SSK,J-r-  Sr.S"S jr/S#S jr0 " S S5      r1 " S S\15      r2 " S S\15      r3 " S S\15      r4\5" 5       r6S$S jr7S%S jr8S&S  jr9S&S! jr:\.(       a  \Rv                  " \:5        gg)'z9Class to monitor a MongoDB server on a background thread.    )annotationsN)TYPE_CHECKINGAnyOptional)commonperiodic_executor)MovingMinimum)NetworkTimeout_OperationCancelled)Hello)_create_lock)_SDAM_LOGGER
_debug_log_SDAMStatusMessage)_shutdown_executors)_is_faas)MovingAverage)ServerDescription)_SrvResolver)
ConnectionPool_CancellationContext)TopologySettings)TopologyTc                .    SU l         SU l        SU l        g)z'PYTHON-2433 Clear error traceback info.N)__traceback____context__	__cause__errors    `C:\Suresh\moveshuttle\MDcreated\moveengine\venv\Lib\site-packages\pymongo/synchronous/monitor.py	_sanitizer"   2   s    EEEO    c                F    [        S[        R                  " 5       U -
  5      $ )zReturn the duration since the given start time.

Accounts for buggy platforms where time.monotonic() is not monotonic.
See PYTHON-4600.
g        )maxtime	monotonic)starts    r!   _monotonic_durationr)   9   s     sDNN$u,--r#   c                  P    \ 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r
g	)MonitorBaseB   c                   ^ SU4S jjn[         R                  " X4XRS9nX`l        SSU4S jjjn[        R                  " XR
                  5      m[        R                  " X5      U l        [        U 5        g)zBase class to do periodic work on a background thread.

The background thread is signaled to stop when the Topology or
this instance is freed.
c                 <   > T" 5       n U c  gU R                  5         g)NFT)_run)monitorself_refs    r!   target$MonitorBase.__init__.<locals>.targetL   s    jGLLNr#   )intervalmin_intervalr2   nameNc                D   > T" 5       nU(       a  UR                  5         g g Ngc_safe_close)dummyr0   r1   s     r!   _on_topology_gc-MonitorBase.__init__.<locals>._on_topology_gcY   s      jG%%' r#   )returnboolr8   )r;   zOptional[Topology]r>   None)	r   PeriodicExecutor	_executorweakrefrefcloseproxy	_topology	_register)	selftopologyr6   r4   r5   r2   executorr<   r1   s	           @r!   __init__MonitorBase.__init__C   s[    	 %55
 "	( 	( ;;t^^4 xA$r#   c                8    U R                   R                  5         g)zKStart monitoring, or restart after a fork.

Multiple calls have no effect.
N)rB   openrI   s    r!   rO   MonitorBase.opene   s    
 	r#   c                8    U R                   R                  5         g)zGC safe close.N)rB   rE   rP   s    r!   r:   MonitorBase.gc_safe_closel   s    r#   c                $    U R                  5         g)zGClose and stop monitoring.

open() restarts the monitor after closing.
Nr9   rP   s    r!   rE   MonitorBase.closep   s    
 	r#   c                8    U R                   R                  5         g)zWait for the monitor to stop.N)rB   joinrP   s    r!   rW   MonitorBase.joinw       r#   c                8    U R                   R                  5         g)z)If the monitor is sleeping, wake it soon.N)rB   wakerP   s    r!   request_checkMonitorBase.request_check{   rY   r#   )rB   rG   N)rJ   r   r6   strr4   intr5   floatr>   r@   )__name__
__module____qualname____firstlineno__rL   rO   r:   rE   rW   r\   __static_attributes__ r#   r!   r+   r+   B   s!     Dr#   r+   c                     ^  \ rS rSr        SU 4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 jr
SS	 jrSS
 jrSS jrSS jrSrU =r$ )Monitor   c                "  > [         TU ]  USUR                  [        R                  5        Xl        X0l        X@l        U R                  R                  R                  U l
        U R                  SL=(       a    U R                  R                  U l        SU l        SU l        [        UUUR!                  UR"                  5      5      U l        UR&                  S:X  a  SU l        gUR&                  S:X  a  SU l        g[+        5       (       + U l        g)zClass to monitor a MongoDB server on a background thread.

Pass an initial ServerDescription, a Topology, a Pool, and
TopologySettings.

The Topology is weakly referenced. The Pool must be exclusive to this
Monitor.
pymongo_server_monitor_threadNstreamTpollF)superrL   heartbeat_frequencyr   MIN_HEARTBEAT_INTERVAL_server_description_pool	_settings_pool_options_event_listeners
_listenersenabled_for_server_heartbeat_publish_cancel_context_conn_id_RttMonitor_create_pool_for_monitoraddress_rtt_monitorserver_monitoring_mode_streamr   )rI   server_descriptionrJ   pooltopology_settings	__class__s        r!   rL   Monitor.__init__   s     	+11))		
 $6 
*..66GGt3d8d8d?C'+'--.@.H.HI

 33x?DL55? DL'z>DLr#   c                L    U R                   nU(       a  UR                  5         gg)ztCancel any concurrent hello check.

Note: this is called from a weakref.proxy callback and MUST NOT take
any locks.
N)rz   cancel)rI   contexts     r!   cancel_checkMonitor.cancel_check   s#     && NN	 r#   c                    U R                   R                  5         U R                  R                  (       a  U R                   R	                  5         gg)z1Start an _RttMonitor that periodically runs ping.N)r   rO   rB   _stoppedrE   rP   s    r!   _start_rtt_monitorMonitor._start_rtt_monitor   s:    
 	 >>""##% #r#   c                    U R                   R                  5         U R                  R                  5         U R	                  5         g r8   )rB   rE   r   r:   r   rP   s    r!   r:   Monitor.gc_safe_close   s0    '')r#   c                    [         R                  " U R                  R                  5       U R                  R                  5       SS9  g )NT)return_exceptions)asynciogatherrB   rW   r   rP   s    r!   rW   Monitor.join   s/    t~~**,d.?.?.D.D.FZ^_r#   c                x    U R                  5         U R                  R                  5         U R                  5         g r8   )r:   r   rE   _reset_connectionrP   s    r!   rE   Monitor.close   s.    ! 	 r#   c                8    U R                   R                  5         g r8   )rs   resetrP   s    r!   r   Monitor._reset_connection   s    

r#   c                    U R                   n U R                  5       U l         U R                  R                  U R                   U R                   R                  [        U R                   R                  [         5      S9  U R"                  (       a`  U R                   R                  (       aE  U R                   R$                  (       a*  U R'                  5         U R                  R                  5         U R                   R                  (       a+  UR                  (       a  U R                  R                  5         U R                  R                  (       a  U R                  R                  5         g g ! [         a  n[        U5        [	        U R                   R
                  US9U l         UR                  (       a  U R                  R                  5          S nAU R                  R                  (       a  U R                  R                  5         g g S nAff = f! [(         a    U R                  5          Nf = f! U R                  R                  (       a  U R                  R                  5         f f = f)Nr   )
reset_poolinterrupt_connections)rr   _check_serverr   r"   r   r~   is_server_type_knownrB   
skip_sleepr   r   rE   rG   	on_changer    
isinstancer
   r   topology_versionr   ReferenceError)rI   prev_sdexcs      r!   r/   Monitor._run   s   '	*..G+/+=+=+?( NN$$((3399&01I1I1O1OQ_&` %  ||((==,,=='')))+''--'2N2N))+
 ~~&&!!'') 'E ' 
#+<,,44C,( // NN--/0 ~~&&!!'') 'E
<  	JJL	 ~~&&!!'') 'sM   H E- DH -
H7AHH HH H0-H3 /H00H3 38I+c                b   SU l         [        R                  " 5       n U R                  5       $ ! [         a    e [
         Gak  n[        U5        U R                  nUR                  n[        U5      n[        U R                  =(       a    UR                  =(       a    UR                  5      nU R                  (       a+  U R                  c   eU R                  R!                  XEX&5        ["        R$                  " [&        R(                  5      (       aJ  [+        ["        [,        R.                  U R0                  R2                  US   US   UUS-  UU R                   S9	  U R5                  5         [7        U[8        5      (       a  e U R:                  R=                  5         [?        XBS9s SnA$ SnAff = f)zNCall hello or read the next streaming response.

Returns a ServerDescription.
Nr        )message
topologyId
serverHost
serverPortawaited
durationMSfailuredriverConnectionIdr   ) r{   r&   r'   _check_oncer   	Exceptionr"   rr   r~   r)   r?   r   r   r   ry   rw   publish_server_heartbeat_failedr   isEnabledForloggingDEBUGr   r   HEARTBEAT_FAILrG   _topology_idr   r   r   r   r   r   )rI   r(   r    sdr~   durationr   s          r!   r   Monitor._check_server   sP   
  	;##%% 	 	;e))BjjG*51H4<<[B,C,C[H[H[\G}}222??SXb((77 .==#~~::&qz&qz#'$!'+}}
 ""$%!455##%$W::5	;s   . F.E F)#F.)F.c                   U R                   R                  nU R                   n[        U R                  R                  =(       a2    U R
                  =(       a    UR                  =(       a    UR                  5      nU R                  (       a*  U R                  c   eU R                  R                  X5        U R                  (       a+  U R                  R                  (       a  U R                  5         U R                  R                  5        n[        R                   " ["        R$                  5      (       aP  ['        [        [(        R*                  U R,                  R.                  UR0                  UR2                  US   US   US9  UR4                  U l        UR0                  U l        U R9                  U5      u  pVUR:                  (       d  U R<                  R?                  U5        U R<                  RA                  5       u  px[C        XXxS9nU R                  (       a5  U R                  c   eU R                  RE                  XXUR:                  5        [        R                   " ["        R$                  5      (       a_  ['        [        [(        RF                  U R,                  R.                  UR0                  UR2                  US   US   UUS-  URH                  S9
  UsSSS5        $ ! , (       d  f       g= f)zVA single attempt to call hello.

Returns a ServerDescription, or raises an exception.
Nr   r   )r   r   r   serverConnectionIdr   r   r   )min_round_trip_timer   )	r   r   r   r   r   r   r   r   reply)%rr   r~   r?   rs   connsr   r   r   ry   rw    publish_server_heartbeat_startedrz   	cancelledr   checkoutr   r   r   r   r   r   HEARTBEAT_STARTrG   r   idserver_connection_idcancel_contextr{   _check_with_socket	awaitabler   
add_samplegetr   "publish_server_heartbeat_succeededHEARTBEAT_SUCCESSdocument)	rI   r~   r   r   connresponseround_trip_timeavg_rttmin_rtts	            r!   r   Monitor._check_once#  s-   
 **22%% JJaa"2I2IabNaNa
 ==??...OO<<WND$8$8$B$B""$ZZ  "d((77 .>>#~~::'+ww'+'@'@&qz&qz#	 $(#6#6D  GGDM(,(?(?(E%H%%!!,,_=#00446G"7g[B}}222BBh8J8J ((77 .@@#~~::'+ww'+'@'@&qz&qz#.5"++ Q #""s   GK&&
K4c                   [         R                  " 5       nUR                  (       a  [        UR	                  5       SS9nOU R
                  (       ag  UR                  (       aV  U R                  R                  (       a;  UR                  U R                  R                  U R                  R                  5      nOUR                  SS5      n[        U5      nX44$ )zSReturn (Hello, round_trip_time).

Can raise ConnectionFailure or OperationFailure.
T)r   N)r&   r'   more_to_comer   _next_replyr   performed_handshakerr   r   _hellort   rp   r)   )rI   r   r(   r   r   s        r!   r   Monitor._check_with_socket`  s    
  T--/4@HLLT55$:R:R:c:c {{((9922H {{4.H&u-!!r#   )	rz   r{   rw   rs   ry   r   rr   rt   r   )r   r   rJ   r   r   r   r   r   ra   )r>   r   )r   r   r>   ztuple[Hello, float])rb   rc   rd   re   rL   r   r   r:   rW   rE   r   r/   r   r   r   rf   __classcell__r   s   @r!   ri   ri      sj    &*-&* &* 	&*
 ,&*P&
`!(*T%;N;z" "r#   ri   c                  @   ^  \ rS rSrSU 4S jjrSS jrSS jrSrU =r$ )	
SrvMonitorix  c                \  > [         TU ]  US[        R                  UR                  5        X l        U R
                  R                  U l        [        U R
                  R                  [        5      (       d   eU R
                  R                  U l        [        R                  " 5       U l        g)zClass to poll SRV records on a background thread.

Pass a Topology and a TopologySettings.

The Topology is weakly referenced.
pymongo_srv_polling_threadN)ro   rL   r   MIN_SRV_RESCAN_INTERVALrp   rt   _seeds	_seedlistr   fqdnr^   _fqdnr&   r'   _startup_time)rI   rJ   r   r   s      r!   rL   SrvMonitor.__init__y  s     	(**11		
 +..$..--s3333..--
!^^-r#   c                6   [         R                  " 5       U R                  [        R                  -   :  a  g U R                  5       nU(       a-  Xl         U R                  R                  U R                  5        g g ! [         a    U R                  5          g f = fr8   )r&   r'   r   r   r   _get_seedlistr   rG   on_srv_updater   rE   )rI   seedlists     r!   r/   SrvMonitor._run  su    >>d0063Q3QQQ%%'%N,,T^^<  " 

s   %A; ;BBc                    [        U R                  U R                  R                  R                  U R                  R
                  5      nUR                  5       u  p#[        U5      S:X  a  [        e U R                  R                  [        U[        R                  5      5        U$ ! [         a    U R                  5          gf = f)zHPoll SRV records for a seedlist.

Returns a list of ServerDescriptions.
r   N)r   r   rt   pool_optionsconnect_timeoutsrv_service_nameget_hosts_and_min_ttllenr   rB   update_intervalr%   r   r   r\   )rI   resolverr   ttls       r!   r   SrvMonitor._get_seedlist  s    
	#

++;;//H
 %::<MH8}! " NN**3sF4R4R+STO  	
  	s   A0B( (CC)r   r   rt   r   )rJ   r   r   r   ra   )r>   zOptional[list[tuple[str, Any]]])	rb   rc   rd   re   rL   r/   r   rf   r   r   s   @r!   r   r   x  s    .& r#   r   c                  h   ^  \ rS rSrS
U 4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 jr
S	rU =r$ )r|   i  c                   > [         TU ]  USUR                  [        R                  5        X0l        [        5       U l        [        5       U l	        [        5       U l        g)zLMaintain round trip times for a server.

The Topology is weakly referenced.
pymongo_server_rtt_threadN)ro   rL   rp   r   rq   rs   r   _moving_averager	   _moving_minr   _lock)rI   rJ   r   r   r   s       r!   rL   _RttMonitor.__init__  sN    
 	'11))		
 
,(?!^
r#   c                X    U R                  5         U R                  R                  5         g r8   )r:   rs   r   rP   s    r!   rE   _RttMonitor.close  s      	

r#   c                    U R                      U R                  R                  U5        U R                  R                  U5        SSS5        g! , (       d  f       g= f)zAdd a RTT sample.N)r   r   r   r   )rI   samples     r!   r   _RttMonitor.add_sample  s;    ZZ  ++F3''/ ZZs   7A
Ac                    U R                      U R                  R                  5       U R                  R                  5       4sSSS5        $ ! , (       d  f       g= f)zBGet the calculated average, or None if no samples yet and the min.N)r   r   r   r   rP   s    r!   r   _RttMonitor.get  s9    ZZ''++-t/?/?/C/C/EE ZZs   4A
Ac                    U R                      U R                  R                  5         U R                  R                  5         SSS5        g! , (       d  f       g= f)zReset the average RTT.N)r   r   r   r   rP   s    r!   r   _RttMonitor.reset  s7    ZZ  &&(""$ ZZs   5A
Ac                     U R                  5       nU R                  U5        g ! [         a    U R                  5          g [         a    U R
                  R                  5          g f = fr8   )_pingr   r   rE   r   rs   r   )rI   rtts     r!   r/   _RttMonitor._run  sP    
	 **,COOC  	JJL 	JJ	s   !$ A& #A&%A&c                   U R                   R                  5        nU R                  R                  (       a  [	        S5      e[
        R                  " 5       nUR                  5         [        U5      sSSS5        $ ! , (       d  f       g= f)z)Run a "hello" command and return the RTT.z_RttMonitor closedN)	rs   r   rB   r   r   r&   r'   hellor)   )rI   r   r(   s      r!   r  _RttMonitor._ping  sV    ZZ  "d~~&& 455NN$EJJL&u- #""s   AA;;
B	)r   r   r   rs   )rJ   r   r   r   r   r   ra   )r  r`   r>   r@   )r>   ztuple[Optional[float], float])r>   r`   )rb   rc   rd   re   rL   rE   r   r   r   r/   r  rf   r   r   s   @r!   r|   r|     s,    $"0F
%. .r#   r|   c                d    [         R                  " U [        5      n[        R	                  U5        g r8   )rC   rD   _unregister	_MONITORSadd)r0   rD   s     r!   rH   rH     s    
++g{
+CMM#r#   c                .    [         R                  U 5        g r8   )r  remove)monitor_refs    r!   r  r    s    [!r#   c                     [         c  g [        [         5      n U  H#  nU" 5       nU(       d  M  UR                  5         M%     S ng r8   )r  listr:   )monitorsrD   r0   s      r!   _shutdown_monitorsr    sA     IH %7!!# 
 Gr#   c                 V    [         n U (       a  U " 5         [        n U (       a  U " 5         g g r8   )r  r   )shutdowns    r!   _shutdown_resourcesr    s!    !H
"H
 r#   )r    r   r>   r@   )r(   r`   r>   r`   )r0   r+   r>   r@   )r  z"weakref.ReferenceType[MonitorBase]r>   r@   ra   )<__doc__
__future__r   r   atexitr   r&   rC   typingr   r   r   pymongor   r   pymongo._csotr	   pymongo.errorsr
   r   pymongo.hellor   pymongo.lockr   pymongo.loggerr   r   r   pymongo.periodic_executorr   pymongo.pool_optionsr   pymongo.read_preferencesr   pymongo.server_descriptionr    pymongo.synchronous.srv_resolverr   pymongo.synchronous.poolr   r   r   pymongo.synchronous.settingsr   pymongo.synchronous.topologyr   _IS_SYNCr"   r)   r+   ri   r   r|   setr  rH   r  r  r  registerrg   r#   r!   <module>r5     s    @ "      / / - ' >  % G G 9 ) 2 8 9 
 >5.; ;|u"k u"p9 9x=.+ =.F E	
"  
OO'( r#   