o
    ?8¾\æ  ã                   @   sH   d Z ddlmZ ddlmZ dZdZdd„ Zdd	„ Zd
d„ Z	dd„ Z
dS )aò  Criteria to select ServerDescriptions based on maxStalenessSeconds.

The Max Staleness Spec says: When there is a known primary P,
a secondary S's staleness is estimated with this formula:

  (S.lastUpdateTime - S.lastWriteDate) - (P.lastUpdateTime - P.lastWriteDate)
  + heartbeatFrequencyMS

When there is no known primary, a secondary S's staleness is estimated with:

  SMax.lastWriteDate - S.lastWriteDate + heartbeatFrequencyMS

where "SMax" is the secondary with the greatest lastWriteDate.
é    )ÚConfigurationError)ÚSERVER_TYPEé
   éZ   c                 C   s>   | |t  k rtdt | |d f ƒ‚| tk rtdt| f ƒ‚d S )NzŒmaxStalenessSeconds must be at least heartbeatFrequencyMS + %d seconds. maxStalenessSeconds is set to %d, heartbeatFrequencyMS is set to %d.iè  zJmaxStalenessSeconds must be at least %d. maxStalenessSeconds is set to %d.)ÚIDLE_WRITE_PERIODr   ÚSMALLEST_MAX_STALENESS)Úmax_stalenessÚheartbeat_frequency© r
   úQ/var/www/html/env/lib/python3.10/site-packages/pymongo/max_staleness_selectors.pyÚ_validate_max_staleness(   s   
ÿþÿÿÿÿÿr   c                 C   sh   |j }g }|jD ]&}|jtjkr)|j|j |j|j  |j }|| kr(| |¡ q| |¡ q| 	|¡S )zEApply max_staleness, in seconds, to a Selection with a known primary.)
ÚprimaryÚserver_descriptionsÚserver_typer   ÚRSSecondaryÚlast_update_timeÚlast_write_dater	   ÚappendÚwith_server_descriptions)r   Ú	selectionr   ÚsdsÚsÚ	stalenessr
   r
   r   Ú_with_primary9   s   


ÿþÿ
€
r   c                 C   sl   |  ¡ }|s| g ¡S g }|jD ] }|jtjkr+|j|j |j }|| kr*| |¡ q| |¡ q| |¡S )zFApply max_staleness, in seconds, to a Selection with no known primary.)	Ú"secondary_with_max_last_write_dater   r   r   r   r   r   r	   r   )r   r   Úsmaxr   r   r   r
   r
   r   Ú_no_primaryN   s    

ÿþ
€
r   c                 C   s2   | dkr|S t | |jƒ |jrt| |ƒS t| |ƒS )z0Apply max_staleness, in seconds, to a Selection.éÿÿÿÿ)r   r	   r   r   r   )r   r   r
   r
   r   Úselectg   s   

r   N)Ú__doc__Úpymongo.errorsr   Úpymongo.server_typer   r   r   r   r   r   r   r
   r
   r
   r   Ú<module>   s   