
    =hC                       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 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. e/       Z0d dZ1d!dZ2d"dZ3d"dZ4 ejj                  e4       y)#z9Class to monitor a MongoDB server on a background thread.    )annotationsN)TYPE_CHECKINGAnyMappingOptionalcast)commonperiodic_executor)MovingMinimum)NotPrimaryErrorOperationFailure_OperationCancelled)Hello)_create_lock)_shutdown_executors)_is_faas)MovingAverage)ServerDescription)_SrvResolver)
ConnectionPool_CancellationContext)TopologySettings)Topologyc                .    d| _         d| _        d| _        y)z'PYTHON-2433 Clear error traceback info.N)__traceback____context__	__cause__errors    _/var/www/html/phonemate/phone_mate_backend/venv/lib/python3.12/site-packages/pymongo/monitor.py	_sanitizer"   )   s    EEEO    c                  >    e Zd ZddZd	dZd	dZd	dZd
ddZd	dZy)MonitorBasec                    dfd}t        j                  ||||      }|| _        ddfd}t        j                  | |j
                        t        j                  ||      | _        t        |        y)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                 :            } | y| j                          y)NFT)_run)monitorself_refs    r!   targetz$MonitorBase.__init__.<locals>.target9   s    jGLLNr#   )intervalmin_intervalr+   nameNc                :            }|r|j                          y y Ngc_safe_close)dummyr)   r*   s     r!   _on_topology_gcz-MonitorBase.__init__.<locals>._on_topology_gcF   s      jG%%' r#   )returnboolr0   )r3   zOptional[Topology]r5   None)	r
   PeriodicExecutor	_executorweakrefrefcloseproxy	_topology	_register)	selftopologyr.   r,   r-   r+   executorr4   r*   s	           @r!   __init__zMonitorBase.__init__1   s`    	 %55Ld
 "	( ;;tX^^4 xA$r#   c                8    | j                   j                          y)z[Start monitoring, or restart after a fork.

        Multiple calls have no effect.
        N)r9   openr@   s    r!   rE   zMonitorBase.openR   s    
 	r#   c                8    | j                   j                          y)zGC safe close.N)r9   r<   rF   s    r!   r2   zMonitorBase.gc_safe_closeY   s    r#   c                $    | j                          y)zWClose and stop monitoring.

        open() restarts the monitor after closing.
        Nr1   rF   s    r!   r<   zMonitorBase.close]   s    
 	r#   Nc                :    | j                   j                  |       y)zWait for the monitor to stop.N)r9   join)r@   timeouts     r!   rJ   zMonitorBase.joind   s    G$r#   c                8    | j                   j                          y)z)If the monitor is sleeping, wake it soon.N)r9   wakerF   s    r!   request_checkzMonitorBase.request_checkh   s    r#   )rA   r   r.   strr,   intr-   floatr5   r7   r0   )rK   zOptional[int]r5   r7   )	__name__
__module____qualname__rC   rE   r2   r<   rJ   rN    r#   r!   r%   r%   0   s!    B%r#   r%   c                  x     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 )Monitorc                   t         |   |d|j                  t        j                         || _        || _        || _        | j                  j                  j                  | _
        | j                  duxr | j                  j                  | _        d| _        t        |||j                  |j                               | _        |j$                  dk(  rd| _        y|j$                  dk(  rd| _        yt)                | _        y)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)superrC   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   )r@   server_descriptionrA   pooltopology_settings	__class__s        r!   rC   zMonitor.__init__n   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                B    | j                   }|r|j                          yy)zCancel any concurrent hello check.

        Note: this is called from a weakref.proxy callback and MUST NOT take
        any locks.
        N)rh   cancel)r@   contexts     r!   cancel_checkzMonitor.cancel_check   s#     && NN	 r#   c                    | j                   j                          | j                  j                  r| j                   j	                          yy)z1Start an _RttMonitor that periodically runs ping.N)rl   rE   r9   _stoppedr<   rF   s    r!   _start_rtt_monitorzMonitor._start_rtt_monitor   s:    
 	 >>""##% #r#   c                    | j                   j                          | j                  j                          | j	                          y r0   )r9   r<   rl   r2   rv   rF   s    r!   r2   zMonitor.gc_safe_close   s0    '')r#   c                x    | j                          | j                  j                          | j                          y r0   )r2   rl   r<   _reset_connectionrF   s    r!   r<   zMonitor.close   s.    ! 	 r#   c                8    | j                   j                          y r0   )ra   resetrF   s    r!   r|   zMonitor._reset_connection   s    

r#   c                   	 | j                   }	 | j                         | _         | j                  j                  | j                   | j                   j                         | j                  rV| j                   j                  r@| j                   j                  r*| j                          | j                  j                          | j                   j                  r(|j                  r| j                  j                          y y y # t        $ ra}t        |       t	        | j                   j
                  |      | _         |j                  r| j                  j                          Y d }~y d }~ww xY w# t        $ r | j!                          Y y w xY w)Nr   )
reset_pool)r`   _check_serverr   r"   r   rk   is_server_type_knownr9   
skip_sleepr>   	on_changer    rn   topology_versionry   ReferenceErrorr<   )r@   prev_sdexcs      r!   r(   zMonitor._run   s;   "	..G+/+=+=+?( NN$$((T5M5M5S5S %  ||((==,,=='')))+''--'2N2N))+ 3O-3 ' 
#+<,,44C,( // NN--/
8  	JJL	s;   E- D  CE-  	E*	AE% E- %E**E- -F	F	c                4   t        j                         }	 	 | j                         S # t        t        f$ rW}t        t        t        t        f   |j                        }| j                  j                  |j                  d              d}~ww xY w# t        $ r  t        $ r}t        |       | j                   }|j"                  }t        j                         |z
  }| j$                  r]t'        | j(                  xr |j*                  xr |j,                        }| j.                  J | j.                  j1                  ||||       | j3                          t5        |t6              r | j8                  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   rO   r   detailsr>   receive_cluster_timegetr   	Exceptionr"   r`   rk   rg   r6   rn   r   r   re   publish_server_heartbeat_failedr|   
isinstancer   rl   r~   r   )	r@   startr   r   r    sdrk   durationawaiteds	            r!   r   zMonitor._check_server   sL   
  	;''))$o6 wsCx0#++>33GKK4OP	
  	 	;e))BjjG~~'%/H}}t||_0G0G_BL_L_`222??SXZab""$%!45##%$WE::	;s4   ' BABBB F"C*FFFc                   | j                   j                  }| j                  r| j                  J | j                   }t	        | j
                  j                  xr( | j                  xr |j                  xr |j                        }| j                  j                  ||       | j                  r&| j                  j                  r| j                          | j
                  j                         5 }|j                  | _        | j!                  |      \  }}|j"                  s| j$                  j'                  |       | j$                  j)                         \  }}t+        ||||      }| j                  r6| j                  J | j                  j-                  ||||j"                         |cddd       S # 1 sw Y   yxY w)zfA single attempt to call hello.

        Returns a ServerDescription, or raises an exception.
        N)min_round_trip_time)r`   rk   rg   re   r6   ra   connsrn   r   r    publish_server_heartbeat_startedrh   	cancelledr|   checkoutcancel_context_check_with_socket	awaitablerl   
add_sampler   r   "publish_server_heartbeat_succeeded)	r@   rk   r   r   connresponseround_trip_timeavg_rttmin_rtts	            r!   r   zMonitor._check_once   sx   
 **22==??...))B 

   (LL(++( ''	G OO<<WgND$8$8$B$B""$ZZ  "d#'#6#6D (,(?(?(E%Ho%%!!,,_=#00446GW"7HgSZ[B}}222BB_h8J8J  #""s   .B<F44F=c                   | j                   j                         }t        j                         }|j                  rt        |j                         d      }n}| j                  r^|j                  rR| j                  j                  r<|j                  || j                  j                  | j                  j                        }n|j                  |dd      }|t        j                         |z
  fS )zcReturn (Hello, round_trip_time).

        Can raise ConnectionFailure or OperationFailure.
        T)r   N)r>   max_cluster_timer   r   more_to_comer   _next_replyrn   performed_handshaker`   r   _hellorb   r^   )r@   r   cluster_timer   r   s        r!   r   zMonitor._check_with_socket$  s    
 ~~668 T--/4@HLLT55$:R:R:c:c {{((9922H {{<t<H)E111r#   )ro   r   rA   r   rp   r   rq   r   rR   )r5   r   )r   r   r5   ztuple[Hello, float])rS   rT   rU   rC   rv   ry   r2   r<   r|   r(   r   r   r   __classcell__rr   s   @r!   rX   rX   m   s_    %*-%* %* 	%*
 ,%*N&
!#J;@"H2r#   rX   c                  0     e Zd Zd fdZddZddZ xZS )
SrvMonitorc                    t         |   |dt        j                  |j                         || _        | j
                  j                  | _        t        | j
                  j                  t              sJ | j
                  j                  | _        y)zClass to poll SRV records on a background thread.

        Pass a Topology and a TopologySettings.

        The Topology is weakly referenced.
        pymongo_srv_polling_threadN)r]   rC   r	   MIN_SRV_RESCAN_INTERVALr^   rb   _seeds	_seedlistr   fqdnrO   _fqdn)r@   rA   rq   rr   s      r!   rC   zSrvMonitor.__init__>  so     	(**11		
 +..$..--s333..--
r#   c                    | j                         }|r.|| _        	 | j                  j                  | j                         y y # t        $ r | j                          Y y w xY wr0   )_get_seedlistr   r>   on_srv_updater   r<   )r@   seedlists     r!   r(   zSrvMonitor._runP  sT    %%'%DN,,T^^<  " 

s   %A AAc                   	 t        | j                  | j                  j                  j                  | j                  j
                        }|j                         \  }}t        |      dk(  rt        	 | j                  j                  t        |t        j                               |S # t        $ r | j                          Y yw xY w)zXPoll SRV records for a seedlist.

        Returns a list of ServerDescriptions.
        r   N)r   r   rb   pool_optionsconnect_timeoutsrv_service_nameget_hosts_and_min_ttllenr   r9   update_intervalmaxr	   r   rN   )r@   resolverr   ttls       r!   r   zSrvMonitor._get_seedlistZ  s    
	#

++;;//H
 %::<MHc8}! " NN**3sF4R4R+STO  	
  	s   A0B( (CC)rA   r   rq   r   rR   )r5   zOptional[list[tuple[str, Any]]])rS   rT   rU   rC   r(   r   r   r   s   @r!   r   r   =  s    .$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 )ri   c                    t         |   |d|j                  t        j                         || _        t               | _        t               | _	        t               | _        y)z\Maintain round trip times for a server.

        The Topology is weakly referenced.
        pymongo_server_rtt_threadN)r]   rC   r^   r	   r_   ra   r   _moving_averager   _moving_minr   _lock)r@   rA   rq   rp   rr   s       r!   rC   z_RttMonitor.__init__v  sP    
 	'11))		
 
,(?!^
r#   c                X    | j                          | j                  j                          y r0   )r2   ra   r~   rF   s    r!   r<   z_RttMonitor.close  s      	

r#   c                    | j                   5  | j                  j                  |       | j                  j                  |       ddd       y# 1 sw Y   yxY w)zAdd a RTT sample.N)r   r   r   r   )r@   samples     r!   r   z_RttMonitor.add_sample  s;    ZZ  ++F3''/ ZZs   7AAc                    | j                   5  | j                  j                         | j                  j                         fcddd       S # 1 sw Y   yxY w)zBGet the calculated average, or None if no samples yet and the min.N)r   r   r   r   rF   s    r!   r   z_RttMonitor.get  s9    ZZ''++-t/?/?/C/C/EE ZZs   4AAc                    | j                   5  | j                  j                          | j                  j                          ddd       y# 1 sw Y   yxY w)zReset the average RTT.N)r   r   r~   r   rF   s    r!   r~   z_RttMonitor.reset  s7    ZZ  &&(""$ ZZs   5AAc                    	 | j                         }| j                  |       y # t        $ r | j                          Y y t        $ r | j
                  j                          Y y w xY wr0   )_pingr   r   r<   r   ra   r~   )r@   rtts     r!   r(   z_RttMonitor._run  sP    
	 **,COOC  	JJL 	JJ	s   !$ A$"A$#A$c                   | j                   j                         5 }| j                  j                  rt	        d      t        j                         }|j                          t        j                         |z
  cddd       S # 1 sw Y   yxY w)z)Run a "hello" command and return the RTT.z_RttMonitor closedN)ra   r   r9   rx   r   r   r   hello)r@   r   r   s      r!   r   z_RttMonitor._ping  s]    ZZ  "d~~&& 455NN$EJJL>>#e+ #""s   ABB
)rA   r   rq   r   rp   r   rR   )r   rQ   r5   r7   )r5   ztuple[Optional[float], float])r5   rQ   )rS   rT   rU   rC   r<   r   r   r~   r(   r   r   r   s   @r!   ri   ri   u  s'    $"0F
%,r#   ri   c                b    t        j                  | t              }t        j	                  |       y r0   )r:   r;   _unregister	_MONITORSadd)r)   r;   s     r!   r?   r?     s    
++g{
+CMM#r#   c                .    t         j                  |        y r0   )r   remove)monitor_refs    r!   r   r     s    [!r#   c                 v    t         y t        t               } | D ]  } |       }|s|j                           d }y r0   )r   listr2   )monitorsr;   r)   s      r!   _shutdown_monitorsr     s?     IH %!!# 
 Gr#   c                 B    t         } | r |         t        } | r |         y y r0   )r   r   )shutdowns    r!   _shutdown_resourcesr     s!    !H
"H
 r#   )r    r   r5   r7   )r)   r%   r5   r7   )r   z"weakref.ReferenceType[MonitorBase]r5   r7   rR   )6__doc__
__future__r   atexitr   r:   typingr   r   r   r   r   pymongor	   r
   pymongo._csotr   pymongo.errorsr   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%   rX   r   ri   setr   r?   r   r   r   registerrV   r#   r!   <module>r      s    @ "    > > - ' Q Q  % 9 ! 2 8 -CC1): :zM2k M2`5 5p=,+ =,F E	
"  # $r#   