
    M}bgD                       d Z ddlmZ ddlZddlZddlZddlmZmZm	Z	m
Z
mZ ddlmZmZ ddlmZ ddlmZmZmZmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddl m!Z! ddl"m#Z# erddlm$Z$m%Z%m&Z& ddl'm(Z( ddl)m*Z* d&dZ+ G d d          Z, G d de,          Z- G d de,          Z. G d de,          Z/ e0            Z1d'd Z2d(d#Z3d)d$Z4d)d%Z5 ej6        e5           dS )*z9Class to monitor a MongoDB server on a background thread.    )annotationsN)TYPE_CHECKINGAnyMappingOptionalcast)commonperiodic_executor)MovingMinimum)NetworkTimeoutNotPrimaryErrorOperationFailure_OperationCancelled)Hello)_create_lock)_shutdown_executors)_is_faas)MovingAverage)ServerDescription)_SrvResolver)
ConnectionPool_CancellationContext)TopologySettings)Topologyerror	ExceptionreturnNonec                0    d| _         d| _        d| _        dS )z'PYTHON-2433 Clear error traceback info.N)__traceback____context__	__cause__r   s    b/home/e360mart.nyusoft.in/public_html/e360mart_env/lib/python3.11/site-packages/pymongo/monitor.py	_sanitizer&   )   s    EEEOOO    c                  @    e Zd Zdd	ZddZddZddZdddZddZdS )MonitorBasetopologyr   namestrintervalintmin_intervalfloatc                    d
fd}t          j        ||||          }|| _        ddfd	}t          j        | |j                  t          j        ||          | _        t          |            dS )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.
        r   boolc                 L                 } | dS |                                   dS )NFT)_run)monitorself_refs    r%   targetz$MonitorBase.__init__.<locals>.target:   s*    hjjGuLLNNN4r'   )r-   r/   r7   r+   NdummyOptional[Topology]r   c                L                 }|r|                                  d S d S Ngc_safe_close)r8   r5   r6   s     r%   _on_topology_gcz-MonitorBase.__init__.<locals>._on_topology_gcG   s:     hjjG (%%'''''( (r'   )r   r2   r;   )r8   r9   r   r   )	r
   PeriodicExecutor	_executorweakrefrefcloseproxy	_topology	_register)	selfr*   r+   r-   r/   r7   executorr>   r6   s	           @r%   __init__zMonitorBase.__init__1   s    	 	 	 	 	 	 %5Ld
 
 
 "	( 	( 	( 	( 	( 	( 	( ;tX^44 xAA$r'   r   r   c                8    | j                                          dS )z[Start monitoring, or restart after a fork.

        Multiple calls have no effect.
        N)r@   openrG   s    r%   rK   zMonitorBase.openS   s    
 	r'   c                8    | j                                          dS )zGC safe close.N)r@   rC   rL   s    r%   r=   zMonitorBase.gc_safe_closeZ   s    r'   c                .    |                                   dS )zWClose and stop monitoring.

        open() restarts the monitor after closing.
        Nr<   rL   s    r%   rC   zMonitorBase.close^   s    
 	r'   NtimeoutOptional[int]c                :    | j                             |           dS )zWait for the monitor to stop.N)r@   join)rG   rO   s     r%   rR   zMonitorBase.joine   s    G$$$$$r'   c                8    | j                                          dS )z)If the monitor is sleeping, wake it soon.N)r@   wakerL   s    r%   request_checkzMonitorBase.request_checki   s    r'   )r*   r   r+   r,   r-   r.   r/   r0   r   r   r;   )rO   rP   r   r   )	__name__
__module____qualname__rI   rK   r=   rC   rR   rU    r'   r%   r)   r)   0   s               D         % % % % %     r'   r)   c                  h     e Zd Zd fd	ZddZddZddZddZddZddZ	ddZ
ddZddZ xZS )Monitorserver_descriptionr   r*   r   poolr   topology_settingsr   c                   t                                          |d|j        t          j                   || _        || _        || _        | j        j        j	        | _
        | j
        duo| j
        j        | _        d| _        t          |||                    |j                            | _        |j        dk    r	d| _        dS |j        dk    r	d| _        dS t)                       | _        dS )a   Class 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)superrI   heartbeat_frequencyr	   MIN_HEARTBEAT_INTERVAL_server_description_pool	_settings_pool_options_event_listeners
_listenersenabled_for_server_heartbeat_publish_cancel_context_RttMonitor_create_pool_for_monitoraddress_rtt_monitorserver_monitoring_mode_streamr   )rG   r]   r*   r^   r_   	__class__s        r%   rI   zMonitor.__init__o   s     	+1)		
 	
 	
 $6 
*.6Gt3d8d?C'--.@.HII
 

 3x??DLLL5?? DLLL'zz>DLLLr'   r   r   c                D    | j         }|r|                                 dS dS )zCancel any concurrent hello check.

        Note: this is called from a weakref.proxy callback and MUST NOT take
        any locks.
        N)ro   cancel)rG   contexts     r%   cancel_checkzMonitor.cancel_check   s6     & 	 NN		 	r'   c                    | j                                          | j        j        r| j                                          dS dS )z1Start an _RttMonitor that periodically runs ping.N)rs   rK   r@   _stoppedrC   rL   s    r%   _start_rtt_monitorzMonitor._start_rtt_monitor   sK    
 	   >" 	&##%%%%%	& 	&r'   c                    | j                                          | j                                         |                                  d S r;   )r@   rC   rs   r=   rz   rL   s    r%   r=   zMonitor.gc_safe_close   sC    '')))r'   c                    |                                   | j                                         |                                  d S r;   )r=   rs   rC   _reset_connectionrL   s    r%   rC   zMonitor.close   sC    !!! 	     r'   c                8    | j                                          d S r;   )rh   resetrL   s    r%   r   zMonitor._reset_connection   s    
r'   c                   	 | j         }	 |                                 | _         ng# t          $ rZ}t          |           t	          | j         j        |          | _         |j        r| j                                         Y d }~d S d }~ww xY w| j	        
                    | j         | j         j        t          | j         j        t                               | j        rE| j         j        r9| j         j        r-|                                  | j                                         | j         j        r"|j        r| j                                         d S d S d S # t"          $ r |                                  Y d S w xY w)Nr$   )
reset_poolinterrupt_connections)rg   _check_serverr   r&   r   rr   is_server_type_knownr@   
skip_sleeprE   	on_changer   
isinstancer   ru   topology_versionr}   ReferenceErrorrC   )rG   prev_sdexcs      r%   r4   zMonitor._run   s   $	.G+/+=+=+?+?((& 
 
 
#+<,4C, , ,( / 0 N--///
 N$$(39&01I1OQ_&`&` %    | ,(=,,=, '')))))+++'- ,'2N ,))+++++, , , ,  	 	 	JJLLLLLL	s8   E $ E 
BAB=E BCE E43E4c                (   t          j                    }	 	 |                                 S # t          t          f$ r_}t          t          t          t          f         |j	                  }| j
                            |                    d                      d}~ww xY w# t          $ r  t          $ r}t          |           | j        }|j        }t          j                    |z
  }| j        rHt'          | j        o|j        o|j                  }| j        J | j                            ||||           |                                  t5          |t6                    r | j                                         t=          ||          cY d}~S d}~ww xY w)z^Call hello or read the next streaming response.

        Returns a ServerDescription.
        z$clusterTimeNr$   )time	monotonic_check_oncer   r   r   r   r,   r   detailsrE   receive_cluster_timegetr   r   r&   rg   rr   rn   r2   ru   r   r   rl   publish_server_heartbeat_failedr   r   r   rs   r   r   )	rG   startr   r   r   sdrr   durationawaiteds	            r%   r   zMonitor._check_server   s   
   	;'')))$o6   wsCx0#+>>33GKK4O4OPPP	
  	 	 	 	; 	; 	;e)BjG~''%/H} ct|_0G_BL_``222??SXZabbb""$$$%!455 ##%%%$WE:::::::::	;s4   * BABBB F1CFFFc                   | j         j        }| j        rY| j        J | j         }t	          | j        j        o| j        o|j        o|j	                  }| j        
                    ||           | j        r | j        j        r|                                  | j                                        5 }|j        | _        |                     |          \  }}|j        s| j                            |           | j                                        \  }}t+          ||||          }| j        r+| j        J | j                            ||||j                   |cddd           S # 1 swxY w Y   dS )zfA single attempt to call hello.

        Returns a ServerDescription, or raises an exception.
        N)min_round_trip_time)rg   rr   rn   rl   r2   rh   connsru   r   r    publish_server_heartbeat_startedro   	cancelledr   checkoutcancel_context_check_with_socket	awaitablers   
add_sampler   r   "publish_server_heartbeat_succeeded)	rG   rr   r   r   connresponseround_trip_timeavg_rttmin_rtts	            r%   r   zMonitor._check_once  s   
 *2= 	O?...)B 
  (L(+( '	 G O<<WgNNN 	%D$8$B 	%""$$$Z  "" 	d#'#6D (,(?(?(E(E%Ho% >!,,_===#04466GW"7HgSZ[[[B} 222BB_h8J   	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   -B(E""E&)E&r   r   tuple[Hello, float]c                   | j                                         }t          j                    }|j        r$t          |                                d          }n]| j        r?|j        r8| j	        j
        r,|                    || j	        j
        | j        j                  }n|                    |dd          }|t          j                    |z
  fS )zcReturn (Hello, round_trip_time).

        Can raise ConnectionFailure or OperationFailure.
        T)r   N)rE   max_cluster_timer   r   more_to_comer   _next_replyru   performed_handshakerg   r   _hellori   re   )rG   r   cluster_timer   r   s        r%   r   zMonitor._check_with_socket'  s    
 ~6688   	=T--//4@@@HHL	=!5	=:>:R:c	= {{(92 HH {{<t<<H))E111r'   )r]   r   r*   r   r^   r   r_   r   rV   )r   r   )r   r   r   r   )rW   rX   rY   rI   rz   r}   r=   rC   r   r4   r   r   r   __classcell__rv   s   @r%   r\   r\   n   s        %* %* %* %* %* %*N   & & & &   
! ! ! !   % % % %N; ; ; ;@" " " "H2 2 2 2 2 2 2 2r'   r\   c                  0     e Zd Zd fdZddZdd
Z xZS )
SrvMonitorr*   r   r_   r   c                2   t                                          |dt          j        |j                   || _        | j        j        | _        t          | j        j	        t                    sJ | j        j	        | _        t          j                    | _        dS )zClass to poll SRV records on a background thread.

        Pass a Topology and a TopologySettings.

        The Topology is weakly referenced.
        pymongo_srv_polling_threadN)rd   rI   r	   MIN_SRV_RESCAN_INTERVALre   ri   _seeds	_seedlistr   fqdnr,   _fqdnr   r   _startup_time)rG   r*   r_   rv   s      r%   rI   zSrvMonitor.__init__A  s     	(*1		
 	
 	
 +.$.-s33333.-
!^--r'   r   r   c                $   t          j                    | j        t          j        z   k     rd S |                                 }|rN|| _        	 | j                            | j                   d S # t          $ r | 
                                 Y d S w xY wd S r;   )r   r   r   r	   r   _get_seedlistr   rE   on_srv_updater   rC   )rG   seedlists     r%   r4   zSrvMonitor._runT  s    >d063QQQQF%%'' 	%DN,,T^<<<<<!   

		 	s   
A+ +BBOptional[list[tuple[str, Any]]]c                x   	 t          | j        | j        j        j        | j        j                  }|                                \  }}t          |          dk    rt          	 | j	        
                    t          |t          j                             |S # t          $ r |                                  Y dS w xY w)zXPoll SRV records for a seedlist.

        Returns a list of ServerDescriptions.
        r   N)r   r   ri   pool_optionsconnect_timeoutsrv_service_nameget_hosts_and_min_ttllenr   r@   update_intervalmaxr	   r   rU   )rG   resolverr   ttls       r%   r   zSrvMonitor._get_seedlista  s    
	#
+;/ H
 %::<<MHc8}}!! " N**3sF4R+S+STTTO  	 	 	
    44	s   A B B98B9)r*   r   r_   r   rV   )r   r   )rW   rX   rY   rI   r4   r   r   r   s   @r%   r   r   @  se        . . . . . .&          r'   r   c                  P     e Zd Zd fdZdd
ZddZddZddZddZddZ	 xZ
S )rp   r*   r   r_   r   r^   r   c                    t                                          |d|j        t          j                   || _        t                      | _        t                      | _	        t                      | _        dS )z\Maintain round trip times for a server.

        The Topology is weakly referenced.
        pymongo_server_rtt_threadN)rd   rI   re   r	   rf   rh   r   _moving_averager   _moving_minr   _lock)rG   r*   r_   r^   rv   s       r%   rI   z_RttMonitor.__init__}  sc    
 	'1)		
 	
 	
 
,(??!^^


r'   r   r   c                `    |                                   | j                                         d S r;   )r=   rh   r   rL   s    r%   rC   z_RttMonitor.close  s0     	
r'   sampler0   c                    | j         5  | j                            |           | j                            |           ddd           dS # 1 swxY w Y   dS )zAdd a RTT sample.N)r   r   r   r   )rG   r   s     r%   r   z_RttMonitor.add_sample  s    Z 	0 	0 ++F333''///	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0s   5A

AAtuple[Optional[float], float]c                    | j         5  | j                                        | j                                        fcddd           S # 1 swxY w Y   dS )zBGet the calculated average, or None if no samples yet and the min.N)r   r   r   r   rL   s    r%   r   z_RttMonitor.get  s    Z 	F 	F'++--t/?/C/C/E/EE	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	Fs   2AAAc                    | j         5  | j                                         | j                                         ddd           dS # 1 swxY w Y   dS )zReset the average RTT.N)r   r   r   r   rL   s    r%   r   z_RttMonitor.reset  s    Z 	% 	% &&(((""$$$	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	%s   3AAAc                    	 |                                  }|                     |           d S # t          $ r |                                  Y d S t          $ r | j                                         Y d S w xY wr;   )_pingr   r   rC   r   rh   r   )rG   rtts     r%   r4   z_RttMonitor._run  s    
	 **,,COOC      	 	 	JJLLLLLL 	 	 	J	s   )- A4"A43A4c                   | j                                         5 }| j        j        rt	          d          t          j                    }|                                 t          j                    |z
  cddd           S # 1 swxY w Y   dS )z)Run a "hello" command and return the RTT.z_RttMonitor closedN)rh   r   r@   r|   r   r   r   hello)rG   r   r   s      r%   r   z_RttMonitor._ping  s    Z  "" 	,d~& 6 4555N$$EJJLLL>##e+	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	,s   AA??BB)r*   r   r_   r   r^   r   rV   )r   r0   r   r   )r   r   )r   r0   )rW   rX   rY   rI   rC   r   r   r   r4   r   r   r   s   @r%   rp   rp   |  s        $ $ $ $ $ $"   0 0 0 0F F F F
% % % %   , , , , , , , ,r'   rp   r5   c                n    t          j        | t                    }t                              |           d S r;   )rA   rB   _unregister	_MONITORSadd)r5   rB   s     r%   rF   rF     s+    
+g{
+
+CMM#r'   monitor_ref"weakref.ReferenceType[MonitorBase]c                :    t                               |            d S r;   )r   remove)r   s    r%   r   r     s    [!!!!!r'   c                     t           d S t          t                     } | D ]"} |            }|r|                                 #d }d S r;   )r   listr=   )monitorsrB   r5   s      r%   _shutdown_monitorsr     sZ     IH  $ $#%% 	$!!###GGGr'   c                 V    t           } | r
 |              t          } | r |              d S d S r;   )r   r   )shutdowns    r%   _shutdown_resourcesr     s@    !H 


"H 




 r'   )r   r   r   r   )r5   r)   r   r   )r   r   r   r   rV   )7__doc__
__future__r   atexitr   rA   typingr   r   r   r   r   pymongor	   r
   pymongo._csotr   pymongo.errorsr   r   r   r   pymongo.hellor   pymongo.lockr   pymongo.periodic_executorr   pymongo.poolr   pymongo.read_preferencesr   pymongo.server_descriptionr   pymongo.srv_resolverr   r   r   r   pymongo.settingsr   pymongo.topologyr   r&   r)   r\   r   rp   setr   rF   r   r   r   registerrZ   r'   r%   <module>r      s   @ ? " " " " " "    > > > > > > > > > > > > > > - - - - - - - - ' ' ' ' ' ' a a a a a a a a a a a a       % % % % % % 9 9 9 9 9 9 ! ! ! ! ! ! 2 2 2 2 2 2 8 8 8 8 8 8 - - - - - - *CCCCCCCCCC111111))))))   ; ; ; ; ; ; ; ;|O2 O2 O2 O2 O2k O2 O2 O2d9 9 9 9 9 9 9 9x=, =, =, =, =,+ =, =, =,F CEE	   
" " " "        # $ $ $ $ $r'   