o
    Åîï]z$  ã                   @   s˜   d Z ddlZddl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 G d
d„ deƒZG dd„ deƒZG dd„ deƒZdS )z9Class to monitor a MongoDB server on a background thread.é    N)ÚcommonÚperiodic_executor)ÚOperationFailure)Útime)ÚMovingAverage)ÚServerDescription)ÚSERVER_TYPE)Ú_SrvResolverc                   @   s6   e Zd Zdd„ Zdd„ Zdd„ Zddd	„Zd
d„ ZdS )ÚMonitorBasec                 O   s   t ‚)z+Override this method to create an executor.)ÚNotImplementedError)ÚselfÚargsÚkwargs© r   úA/var/www/html/env/lib/python3.10/site-packages/pymongo/monitor.pyÚ__init__   s   zMonitorBase.__init__c                 C   ó   | j  ¡  dS )z[Start monitoring, or restart after a fork.

        Multiple calls have no effect.
        N)Ú	_executorÚopen©r   r   r   r   r   !   ó   zMonitorBase.openc                 C   r   )zWClose and stop monitoring.

        open() restarts the monitor after closing.
        N)r   Úcloser   r   r   r   r   (   r   zMonitorBase.closeNc                 C   s   | j  |¡ dS )zWait for the monitor to stop.N)r   Újoin)r   Útimeoutr   r   r   r   /   s   zMonitorBase.joinc                 C   r   )z)If the monitor is sleeping, wake it soon.N)r   Úwaker   r   r   r   Úrequest_check3   s   zMonitorBase.request_check©N)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   r   r   r   r
      s    
r
   c                       sD   e Zd Zdd„ Z‡ fdd„Zdd„ Zdd„ Zd	d
„ Zdd„ Z‡  Z	S )ÚMonitorc                    sŠ   || _ || _|| _tƒ | _| jjj| _| jdu}|o| jj| _	‡ fdd„}t
j| jjtj|dd�}|| _t | |j¡‰ t ||j¡| _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.
        Nc                     ó    ˆ ƒ } | d u r	dS t  | ¡ dS ©NFT)r    Ú_run©Úmonitor©Úself_refr   r   ÚtargetQ   ó
   
z Monitor.__init__.<locals>.targetÚpymongo_server_monitor_thread©ÚintervalÚmin_intervalr(   Úname)Ú_server_descriptionÚ_poolÚ	_settingsr   Ú_avg_round_trip_timeÚ_pool_optionsÚevent_listenersÚ
_listenersÚenabled_for_server_heartbeatÚ_publishr   ÚPeriodicExecutorÚheartbeat_frequencyr   ÚMIN_HEARTBEAT_INTERVALr   ÚweakrefÚrefr   ÚproxyÚ	_topology)r   Úserver_descriptionÚtopologyÚpoolÚtopology_settingsÚpubr(   Úexecutorr   r&   r   r   9   s"   
üzMonitor.__init__c                    s   t t| ƒ ¡  | j ¡  d S r   )Úsuperr    r   r0   Úresetr   ©Ú	__class__r   r   r   d   s   zMonitor.closec                 C   s<   z|   ¡ | _| j | j¡ W d S  ty   |  ¡  Y d S w r   )Ú_check_with_retryr/   r>   Ú	on_changeÚReferenceErrorr   r   r   r   r   r#   k   s   
þzMonitor._runc                 C   s6  | j j}d}| j jtjkrd}tƒ }z|  ¡ W S  ty   ‚  tyš } zptƒ | }| j	r6| j
 |||¡ | j |¡ t||d�}|sQ| j ¡  |W  Y d}~S tƒ }z|  ¡ W W  Y d}~S  tyg   ‚  ty• } z#tƒ | }| j	r~| j
 |||¡ | j ¡  |W  Y d}~W  Y d}~S d}~ww d}~ww )ziCall ismaster once or twice. Reset server's pool on error.

        Returns a ServerDescription.
        TF)ÚerrorN)r/   ÚaddressÚserver_typer   ÚUnknownÚ_timeÚ_check_oncerK   Ú	Exceptionr7   r5   Úpublish_server_heartbeat_failedr>   Ú
reset_poolr   r2   rF   )r   rM   ÚretryÚstartrL   Ú
error_timeÚdefaultr   r   r   rI   s   sJ   

ÿ

ÿ
€ú€ízMonitor._check_with_retryc                 C   s’   | j j}| jr| j |¡ | j i ¡�,}|  |¡\}}| j 	|¡ t
||| j ¡ d�}| jr6| j |||¡ |W  d  ƒ S 1 sBw   Y  dS )ziA single attempt to call ismaster.

        Returns a ServerDescription, or raises an exception.
        )rM   ÚismasterÚround_trip_timeN)r/   rM   r7   r5   Ú publish_server_heartbeat_startedr0   Ú
get_socketÚ_check_with_socketr2   Ú
add_sampler   ÚgetÚ"publish_server_heartbeat_succeeded)r   rM   Ú	sock_infoÚresponserZ   Úsdr   r   r   rQ       s"   ýÿ$õzMonitor._check_oncec              
   C   sZ   t ƒ }z| | jjj| j ¡ ¡t ƒ | fW S  ty, } z| j |j	 
d¡¡ ‚ d}~ww )zfReturn (IsMaster, round_trip_time).

        Can raise ConnectionFailure or OperationFailure.
        z$clusterTimeN)rP   rY   r0   ÚoptsÚmetadatar>   Úmax_cluster_timer   Úreceive_cluster_timeÚdetailsr_   )r   ra   rV   Úexcr   r   r   r]   µ   s   ÿþ
ÿ€üzMonitor._check_with_socket)
r   r   r   r   r   r#   rI   rQ   r]   Ú__classcell__r   r   rG   r   r    8   s    +-r    c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )Ú
SrvMonitorc                    sf   || _ | j j| _| j j| _‡ fdd„}tjtj| j j	|dd�}|| _
t | |j¡‰ t ||j¡| _dS )z—Class to poll SRV records on a background thread.

        Pass a Topology and a TopologySettings.

        The Topology is weakly referenced.
        c                     r!   r"   )rk   r#   r$   r&   r   r   r(   Ô   r)   z#SrvMonitor.__init__.<locals>.targetÚpymongo_srv_polling_threadr+   N)r1   Ú_seedsÚ	_seedlistÚfqdnÚ_fqdnr   r8   r   ÚMIN_SRV_RESCAN_INTERVALr9   r   r;   r<   r   r=   r>   )r   r@   rB   r(   rD   r   r&   r   r   Ç   s   

üzSrvMonitor.__init__c                 C   sH   |   ¡ }|r"|| _z
| j | j¡ W d S  ty!   |  ¡  Y d S w d S r   )Ú_get_seedlistrn   r>   Úon_srv_updaterK   r   )r   Úseedlistr   r   r   r#   ç   s   þüzSrvMonitor._runc                 C   s\   zt | jƒ ¡ \}}t|ƒdkrt‚W n ty!   |  ¡  Y dS w | j t|t	j
ƒ¡ |S )zXPoll SRV records for a seedlist.

        Returns a list of ServerDescriptions.
        r   N)r	   rp   Úget_hosts_and_min_ttlÚlenrR   r   r   Úupdate_intervalÚmaxr   rq   )r   rt   Úttlr   r   r   rr   ñ   s   þú
ÿzSrvMonitor._get_seedlistN)r   r   r   r   r#   rr   r   r   r   r   rk   Æ   s     
rk   )Ú__doc__r;   Úpymongor   r   Úpymongo.errorsr   Úpymongo.monotonicr   rP   Úpymongo.read_preferencesr   Úpymongo.server_descriptionr   Úpymongo.server_typer   Úpymongo.srv_resolverr	   Úobjectr
   r    rk   r   r   r   r   Ú<module>   s    