o
    /it                     @   s<  d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZmZ d dl	m
Z
mZmZmZ d dlmZ G dd de jZG dd de jZe
rNd d	lmZmZ G d
d deZG dd deZG dd deZG dd deZG dd deZG dd deZdedefddZG dd dZG dd dZ G dd dZ!dS )     N)ABCabstractmethod)TYPE_CHECKINGLiteralOptionalUnion)Numberc                   @   s   e Zd ZdZdZdZdS )MaintenanceStatenonemovingmaintenanceN)__name__
__module____qualname__NONEMOVINGMAINTENANCE r   r   Y/var/www/html/flask_server/venv/lib/python3.10/site-packages/redis/maint_notifications.pyr	      s    r	   c                   @   s,   e Zd ZdZdZdZdZdZdZdd Z	d	S )
EndpointTypez@Valid endpoint types used in CLIENT MAINT_NOTIFICATIONS command.zinternal-ipzinternal-fqdnzexternal-ipzexternal-fqdnr
   c                 C   s   | j S )z$Return the string value of the enum.)valueselfr   r   r   __str__   s   zEndpointType.__str__N)
r   r   r   __doc__INTERNAL_IPINTERNAL_FQDNEXTERNAL_IPEXTERNAL_FQDNr   r   r   r   r   r   r      s    r   )$MaintNotificationsAbstractConnection(MaintNotificationsAbstractConnectionPoolc                   @   sf   e Zd ZdZdedefddZdefddZede	fd	d
Z
edefddZedefddZdS )MaintenanceNotificationa  
    Base class for maintenance notifications sent through push messages by Redis server.

    This class provides common functionality for all maintenance notifications including
    unique identification and TTL (Time-To-Live) functionality.

    Attributes:
        id (int): Unique identifier for this notification
        ttl (int): Time-to-live in seconds for this notification
        creation_time (float): Timestamp when the notification was created/read
    idttlc                 C   s(   || _ || _t | _| j| j | _dS )z
        Initialize a new MaintenanceNotification with unique ID and TTL functionality.

        Args:
            id (int): Unique identifier for this notification
            ttl (int): Time-to-live in seconds for this notification
        N)r"   r#   time	monotoniccreation_time	expire_atr   r"   r#   r   r   r   __init__5   s   
z MaintenanceNotification.__init__returnc                 C   s   t  | j| j kS )z
        Check if this notification has expired based on its TTL
        and creation time.

        Returns:
            bool: True if the notification has expired, False otherwise
        )r$   r%   r&   r#   r   r   r   r   
is_expiredB   s   z"MaintenanceNotification.is_expiredc                 C      dS )z
        Return a string representation of the maintenance notification.

        This method must be implemented by all concrete subclasses.

        Returns:
            str: String representation of the notification
        Nr   r   r   r   r   __repr__L   s   
z MaintenanceNotification.__repr__c                 C   r,   )a  
        Compare two maintenance notifications for equality.

        This method must be implemented by all concrete subclasses.
        Notifications are typically considered equal if they have the same id
        and are of the same type.

        Args:
            other: The other object to compare with

        Returns:
            bool: True if the notifications are equal, False otherwise
        Nr   r   otherr   r   r   __eq__X   s   zMaintenanceNotification.__eq__c                 C   r,   )a  
        Return a hash value for the maintenance notification.

        This method must be implemented by all concrete subclasses to allow
        instances to be used in sets and as dictionary keys.

        Returns:
            int: Hash value for the notification
        Nr   r   r   r   r   __hash__i   s   z MaintenanceNotification.__hash__N)r   r   r   r   intr)   boolr+   r   strr-   r0   r1   r   r   r   r   r!   (   s    
r!   c                       sd   e Zd ZdZdedee dee def fddZdefd	d
Zde	fddZ
defddZ  ZS )NodeMovingNotificationz
    This notification is received when a node is replaced with a new node
    during cluster rebalancing or maintenance operations.
    r"   new_node_hostnew_node_portr#   c                    s   t  || || _|| _dS )ai  
        Initialize a new NodeMovingNotification.

        Args:
            id (int): Unique identifier for this notification
            new_node_host (str): Hostname or IP address of the new replacement node
            new_node_port (int): Port number of the new replacement node
            ttl (int): Time-to-live in seconds for this notification
        N)superr)   r6   r7   )r   r"   r6   r7   r#   	__class__r   r   r)   }   s   
zNodeMovingNotification.__init__r*   c                 C   sf   | j }td|t  }| jj d| j d| j d| j d| j	 d| j
 d| d|d	d
|   dS )Nr   (id=z, new_node_host='z', new_node_port=, ttl=, creation_time=, expires_at=, remaining=.1fs, expired=))r'   maxr$   r%   r:   r   r"   r6   r7   r#   r&   r+   r   expiry_time	remainingr   r   r   r-      s(   
zNodeMovingNotification.__repr__c                 C   s2   t |tsdS | j|jko| j|jko| j|jkS )z
        Two NodeMovingNotification notifications are considered equal if they have the same
        id, new_node_host, and new_node_port.
        F)
isinstancer5   r"   r6   r7   r.   r   r   r   r0      s   


zNodeMovingNotification.__eq__c                 C   sP   z| j r	t| j nd}W n ty   d}Y nw t| jjt| jt| j|fS )a  
        Return a hash value for the notification to allow
        instances to be used in sets and as dictionary keys.

        Returns:
            int: Hash value based on notification type class name, id,
            new_node_host and new_node_port
        Nr   )	r7   r2   
ValueErrorhashr:   r   r"   r4   r6   )r   	node_portr   r   r   r1      s   	zNodeMovingNotification.__hash__)r   r   r   r   r2   r   r4   r)   r-   r3   r0   r1   __classcell__r   r   r9   r   r5   w   s    r5   c                       T   e Zd ZdZdedef fddZdefddZdefd	d
Z	defddZ
  ZS )NodeMigratingNotificationat  
    Notification for when a Redis cluster node is in the process of migrating slots.

    This notification is received when a node starts migrating its slots to another node
    during cluster rebalancing or maintenance operations.

    Args:
        id (int): Unique identifier for this notification
        ttl (int): Time-to-live in seconds for this notification
    r"   r#   c                       t  || d S Nr8   r)   r(   r9   r   r   r)         z"NodeMigratingNotification.__init__r*   c                 C   \   | j | j }td|t  }| jj d| j d| j d| j  d| d|dd|   d	S 
Nr   r;   r<   r=   r>   r?   r@   rA   rB   	r&   r#   rC   r$   r%   r:   r   r"   r+   rD   r   r   r   r-          
z"NodeMigratingNotification.__repr__c                 C   *   t |tsdS | j|jkot| t|u S )z
        Two NodeMigratingNotification notifications are considered equal if they have the same
        id and are of the same type.
        F)rG   rM   r"   typer.   r   r   r   r0         
z NodeMigratingNotification.__eq__c                 C      t | jjt| jfS z
        Return a hash value for the notification to allow
        instances to be used in sets and as dictionary keys.

        Returns:
            int: Hash value based on notification type and id
        rI   r:   r   r2   r"   r   r   r   r   r1         z"NodeMigratingNotification.__hash__r   r   r   r   r2   r)   r4   r-   r3   r0   r1   rK   r   r   r9   r   rM          	rM   c                       T   e Zd ZdZdZdef fddZdefddZde	fd	d
Z
defddZ  ZS )NodeMigratedNotificationa5  
    Notification for when a Redis cluster node has completed migrating slots.

    This notification is received when a node has finished migrating all its slots
    to other nodes during cluster rebalancing or maintenance operations.

    Args:
        id (int): Unique identifier for this notification
       r"   c                       t  |tj d S rO   )r8   r)   r`   DEFAULT_TTLr   r"   r9   r   r   r)        z!NodeMigratedNotification.__init__r*   c                 C   rR   rS   rT   rD   r   r   r   r-     rU   z!NodeMigratedNotification.__repr__c                 C   rV   )z
        Two NodeMigratedNotification notifications are considered equal if they have the same
        id and are of the same type.
        F)rG   r`   r"   rW   r.   r   r   r   r0     rX   zNodeMigratedNotification.__eq__c                 C   rY   rZ   r[   r   r   r   r   r1     r\   z!NodeMigratedNotification.__hash__r   r   r   r   rc   r2   r)   r4   r-   r3   r0   r1   rK   r   r   r9   r   r`          
	r`   c                       rL   )NodeFailingOverNotificationap  
    Notification for when a Redis cluster node is in the process of failing over.

    This notification is received when a node starts a failover process during
    cluster maintenance operations or when handling node failures.

    Args:
        id (int): Unique identifier for this notification
        ttl (int): Time-to-live in seconds for this notification
    r"   r#   c                    rN   rO   rP   r(   r9   r   r   r)   6  rQ   z$NodeFailingOverNotification.__init__r*   c                 C   rR   rS   rT   rD   r   r   r   r-   9  rU   z$NodeFailingOverNotification.__repr__c                 C   rV   )z
        Two NodeFailingOverNotification notifications are considered equal if they have the same
        id and are of the same type.
        F)rG   rh   r"   rW   r.   r   r   r   r0   G  rX   z"NodeFailingOverNotification.__eq__c                 C   rY   rZ   r[   r   r   r   r   r1   P  r\   z$NodeFailingOverNotification.__hash__r]   r   r   r9   r   rh   *  r^   rh   c                       r_   )NodeFailedOverNotificationa/  
    Notification for when a Redis cluster node has completed a failover.

    This notification is received when a node has finished the failover process
    during cluster maintenance operations or after handling node failures.

    Args:
        id (int): Unique identifier for this notification
    ra   r"   c                    rb   rO   )r8   r)   ri   rc   rd   r9   r   r   r)   h  re   z#NodeFailedOverNotification.__init__r*   c                 C   rR   rS   rT   rD   r   r   r   r-   k  rU   z#NodeFailedOverNotification.__repr__c                 C   rV   )z
        Two NodeFailedOverNotification notifications are considered equal if they have the same
        id and are of the same type.
        F)rG   ri   r"   rW   r.   r   r   r   r0   y  rX   z!NodeFailedOverNotification.__eq__c                 C   rY   rZ   r[   r   r   r   r   r1     r\   z#NodeFailedOverNotification.__hash__rf   r   r   r9   r   ri   [  rg   ri   hostr*   c                 C   sB   |   d}d|vrdS g d}|D ]}t||r dS qdS )a  
    Determine if an FQDN is likely to be internal/private.

    This uses heuristics based on RFC 952 and RFC 1123 standards:
    - .local domains (RFC 6762 - Multicast DNS)
    - .internal domains (common internal convention)
    - Single-label hostnames (no dots)
    - Common internal TLDs

    Args:
        host (str): The FQDN to check

    Returns:
        bool: True if the FQDN appears to be internal/private
    .T)z\.local$z\.internal$z\.corp$z\.lan$z\.intranet$z
\.private$F)lowerrstripresearch)rj   
host_lowerinternal_patternspatternr   r   r   _is_private_fqdn  s   	rs   c                
   @   sz   e Zd ZdZ				ddeeed f dedee d	ee	 fd
dZ
defddZdefddZdeddde	fddZdS )MaintNotificationsConfiga#  
    Configuration class for maintenance notifications handling behaviour. Notifications are received through
    push notifications.

    This class defines how the Redis client should react to different push notifications
    such as node moving, migrations, etc. in a Redis cluster.

    autoT
   Nenabledproactive_reconnectrelaxed_timeoutendpoint_typec                 C   s   || _ || _|| _|| _dS )a!  
        Initialize a new MaintNotificationsConfig.

        Args:
            enabled (bool | "auto"): Controls maintenance notifications handling behavior.
                - True: The CLIENT MAINT_NOTIFICATIONS command must succeed during connection setup,
                otherwise a ResponseError is raised.
                - "auto": The CLIENT MAINT_NOTIFICATIONS command is attempted but failures are
                gracefully handled - a warning is logged and normal operation continues.
                - False: Maintenance notifications are completely disabled.
                Defaults to "auto".
            proactive_reconnect (bool): Whether to proactively reconnect when a node is replaced.
                Defaults to True.
            relaxed_timeout (Number): The relaxed timeout to use for the connection during maintenance.
                If -1 is provided - the relaxed timeout is disabled. Defaults to 20.
            endpoint_type (Optional[EndpointType]): Override for the endpoint type to use in CLIENT MAINT_NOTIFICATIONS.
                If None, the endpoint type will be automatically determined based on the host and TLS configuration.
                Defaults to None.

        Raises:
            ValueError: If endpoint_type is provided but is not a valid endpoint type.
        N)rw   ry   rx   rz   )r   rw   rx   ry   rz   r   r   r   r)     s   
z!MaintNotificationsConfig.__init__r*   c              
   C   s.   | j j d| j d| j d| j d| jd
S )Nz	(enabled=z, proactive_reconnect=z, relaxed_timeout=z, endpoint_type=rB   )r:   r   rw   rx   ry   rz   r   r   r   r   r-     s   
z!MaintNotificationsConfig.__repr__c                 C   s
   | j dkS )aK  
        Check if the relaxed_timeout is enabled. The '-1' value is used to disable the relaxed_timeout.
        If relaxed_timeout is set to None, it will make the operation blocking
        and waiting until any response is received.

        Returns:
            True if the relaxed_timeout is enabled, False otherwise.
        )ry   r   r   r   r   is_relaxed_timeouts_enabled  s   
	z4MaintNotificationsConfig.is_relaxed_timeouts_enabledrj   
connectionr   c                 C   s   | j dur| j S zt|}|j}|rtjW S tjW S  ty#   Y nw | }|rFzt|}|j}|r9tj	W S tj
W S  tyE   Y nw t|}|rOtj	S tj
S )aY  
        Determine the appropriate endpoint type for CLIENT MAINT_NOTIFICATIONS command.

        Logic:
        1. If endpoint_type is explicitly set, use it
        2. Otherwise, check the original host from connection.host:
           - If host is an IP address, use it directly to determine internal-ip vs external-ip
           - If host is an FQDN, get the resolved IP to determine internal-fqdn vs external-fqdn

        Args:
            host: User provided hostname to analyze
            connection: The connection object to analyze for endpoint type determination

        Returns:
        N)rz   	ipaddress
ip_address
is_privater   r   r   rH   get_resolved_ipr   r   rs   )r   rj   r}   ip_addrr   resolved_ipr   r   r   get_endpoint_type  s0   


z*MaintNotificationsConfig.get_endpoint_type)ru   Trv   N)r   r   r   r   r   r3   r   r   r   r   r)   r4   r-   r|   r   r   r   r   r   rt     s0    
"
rt   c                   @   sz   e Zd ZdddeddfddZdd
dZdd Zdd ZdefddZ	de
fddZddee fddZde
fddZdS )MaintNotificationsPoolHandlerpoolr    configr*   Nc                 C   s(   || _ || _t | _t | _d | _d S rO   )r   r   set_processed_notifications	threadingRLock_lockr}   )r   r   r   r   r   r   r)   .  s
   

z&MaintNotificationsPoolHandler.__init__r}   r   c                 C   s
   || _ d S rO   )r}   )r   r}   r   r   r   set_connection9  s   
z,MaintNotificationsPoolHandler.set_connectionc                 C   s(   t | j| j}| j|_| j|_d |_|S rO   )r   r   r   r   r   r}   )r   copyr   r   r   get_handler_for_connection<  s
   z8MaintNotificationsPoolHandler.get_handler_for_connectionc                 C   sP   | j  t| jD ]}| r| j| q	W d    d S 1 s!w   Y  d S rO   )r   tupler   r+   remover   notificationr   r   r   remove_expired_notificationsF  s   "z:MaintNotificationsPoolHandler.remove_expired_notificationsr   c                 C   s0   |    t|tr| |S td|  d S NzUnhandled notification type: )r   rG   r5   handle_node_moving_notificationloggingerrorr   r   r   r   handle_notificationL  s   

z1MaintNotificationsPoolHandler.handle_notificationc                 C   s  | j js| j  sd S | j || jv r	 W d    d S | jj | j js+| j  r| jr3| j nd }t| jddrB| j	d | jj
tjt|| j j|j|dddd | j jrt|jd ure| | ntj|jd | j|fd  tjt|d}|jd ur|d	|ji | j  r|| j j| j jd
 | jjdi | t| jddr| j	d W d    n1 sw   Y  tj|j| j|fd  | j| W d    d S 1 sw   Y  d S )Nset_in_maintenanceFTconnected_address)statemaintenance_notification_hashry   host_addressmatching_addressmatching_patternupdate_notification_hashinclude_free_connections   )args)maintenance_stater   rj   )socket_timeoutsocket_connect_timeoutr   )r   rx   r|   r   r   r   r}   getpeernamegetattrr   update_connections_settingsr	   r   rI   ry   r6   run_proactive_reconnectr   Timerr#   startupdateupdate_connection_kwargshandle_node_moved_notificationadd)r   r   moving_address_srckwargsr   r   r   r   T  s   

	


F"z=MaintNotificationsPoolHandler.handle_node_moving_notificationr   c              	   C   s   | j 3 | jj  | jj|d | jj|d W d   n1 s!w   Y  W d   dS W d   dS 1 s9w   Y  dS )z
        Run proactive reconnect for the pool.
        Active connections are marked for reconnect after they complete the current command.
        Inactive connections are disconnected and will be connected on next use.
        )r   N)r   r   'update_active_connections_for_reconnectdisconnect_free_connections)r   r   r   r   r   r     s   
"z5MaintNotificationsPoolHandler.run_proactive_reconnectc           	      C   s   t |}| jn | jjd|kr8| jjd}| jjd}| jjd}tjd|||d}| jjdi | | jj! | j	 }| jj
}| jjdtjd|dd	||d	d
	 W d   n1 s`w   Y  W d   dS W d   dS 1 sxw   Y  dS )zN
        Handle the cleanup after a node moving notification expires.
        r   orig_host_addressorig_socket_timeoutorig_socket_connect_timeoutN)r   r   rj   r   r   r{   notification_hashT)	ry   r   r   matching_notification_hashr   r   reset_relaxed_timeoutreset_host_addressr   r   )rI   r   r   connection_kwargsgetr	   r   r   r   r|   rx   r   )	r   r   r   	orig_hostr   orig_connect_timeoutr   r   r   r   r   r   r     sJ   

"z<MaintNotificationsPoolHandler.handle_node_moved_notification)r}   r   rO   )r   r   r   rt   r)   r   r   r   r!   r   r5   r   r   r4   r   r   r   r   r   r   r   -  s    


[r   c                   @   sp   e Zd ZU edededediZee	d e
f ed< dddedd	fd
dZdefddZdefddZdd Zd	S )#MaintNotificationsConnectionHandler   r   r!   _NOTIFICATION_TYPESr}   r   r   r*   Nc                 C   s   || _ || _d S rO   )r}   r   )r   r}   r   r   r   r   r)     s   
z,MaintNotificationsConnectionHandler.__init__r   c                 C   sL   | j |jd }|d u rtd|  d S |r | tj d S |   d S r   )	r   r   r:   r   r   %handle_maintenance_start_notificationr	   r   )handle_maintenance_completed_notification)r   r   notification_typer   r   r   r     s   z7MaintNotificationsConnectionHandler.handle_notificationr   c                 C   sJ   | j jtjks| j sd S || j _| j j| jjd | j | jj d S )N)tmp_relaxed_timeout)	r}   r   r	   r   r   r|   set_tmp_settingsry   update_current_socket_timeout)r   r   r   r   r   r     s   zIMaintNotificationsConnectionHandler.handle_maintenance_start_notificationc                 C   sD   | j jtjks| j sd S | j jdd | j d tj| j _d S )NT)r   r{   )	r}   r   r	   r   r   r|   reset_tmp_settingsr   r   r   r   r   r   r     s   zMMaintNotificationsConnectionHandler.handle_maintenance_completed_notification)r   r   r   rM   rh   r`   ri   r   dictrW   r2   __annotations__rt   r)   r!   r   r	   r   r   r   r   r   r   r     s$   
 

r   )"enumr~   r   rn   r   r$   abcr   r   typingr   r   r   r   redis.typingr   Enumr	   r   redis.connectionr   r    r!   r5   rM   r`   rh   ri   r4   r3   rs   rt   r   r   r   r   r   r   <module>   s0    OP1212(x E