o
    i %                     @   sZ  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
Z
d dl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 d d	lmZ d d
lmZmZmZ d dlmZmZ d dl m!Z!m"Z"m#Z#m$Z$ d dl%m&Z&m'Z'm(Z(m)Z)m*Z* d dl+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2 d dl3m4Z4 dZ5G dd deZ6defddZ7eG dd dZ8eG dd dZ9dS )    )	dataclassfield)Enum)ListOptionalTypeUnionN)ConnectionPoolRedisRedisCluster)ExponentialWithJitterBackoff	NoBackoff)WeightedList)EventDispatcherEventDispatcherInterface)MaintNotificationsConfig)DEFAULT_GRACE_PERIODCircuitBreakerPBCircuitBreakerAdapter)Database	Databases)DEFAULT_FAILOVER_ATTEMPTSDEFAULT_FAILOVER_DELAYFailoverStrategyWeightBasedFailoverStrategy)DEFAULT_FAILURE_RATE_THRESHOLD!DEFAULT_FAILURES_DETECTION_WINDOWDEFAULT_MIN_NUM_FAILURESCommandFailureDetectorFailureDetector)DEFAULT_HEALTH_CHECK_DELAYDEFAULT_HEALTH_CHECK_INTERVALDEFAULT_HEALTH_CHECK_POLICYDEFAULT_HEALTH_CHECK_PROBESHealthCheckHealthCheckPoliciesPingHealthCheck)Retryx   c                   @   s   e Zd ZdZdZdZdS )InitialHealthCheckZall_availableZmajority_availableZone_availableN)__name__
__module____qualname__ALL_AVAILABLEZMAJORITY_AVAILABLEZONE_AVAILABLE r.   r.   K/home/app/Keep/.python/lib/python3.10/site-packages/redis/multidb/config.pyr)   -   s    r)   returnc                   C      t  S N)r   r.   r.   r.   r/   default_event_dispatcher3      r3   c                   @   s   e Zd ZU dZdZeed< eedZ	eed< dZ
ee ed< dZee ed< dZee ed	< eZeed
< dZee ed< defddZdS )DatabaseConfiga  
    Dataclass representing the configuration for a database connection.

    This class is used to store configuration settings for a database connection,
    including client options, connection sourcing details, circuit breaker settings,
    and cluster-specific properties. It provides a structure for defining these
    attributes and allows for the creation of customized configurations for various
    database setups.

    Attributes:
        weight (float): Weight of the database to define the active one.
        client_kwargs (dict): Additional parameters for the database client connection.
        from_url (Optional[str]): Redis URL way of connecting to the database.
        from_pool (Optional[ConnectionPool]): A pre-configured connection pool to use.
        circuit (Optional[CircuitBreaker]): Custom circuit breaker implementation.
        grace_period (float): Grace period after which we need to check if the circuit could be closed again.
        health_check_url (Optional[str]): URL for health checks. Cluster FQDN is typically used
            on public Redis Enterprise endpoints.

    Methods:
        default_circuit_breaker:
            Generates and returns a default CircuitBreaker instance adapted for use.
    g      ?weightdefault_factoryclient_kwargsNfrom_url	from_poolcircuitgrace_periodhealth_check_urlr0   c                 C   s   t j| jd}t|S )N)Zreset_timeout)	pybreakerr   r=   r   )selfZcircuit_breakerr.   r.   r/   default_circuit_breakerY   s   z&DatabaseConfig.default_circuit_breaker)r*   r+   r,   __doc__r6   float__annotations__r   dictr9   r:   r   strr;   r	   r<   r   r   r=   r>   rA   r.   r.   r.   r/   r5   7   s   
 r5   c                   @   sb  e Zd ZU dZee ed< eZe	e
eef  ed< eedddddZeed	< d
Zeee  ed< eZeed< eZeed< eZeed< d
Zeee  ed< eZeed< eZeed< e Z!eed< e"Z#e$ed< d
Z%ee& ed< e'Z(eed< e)Z*eed< e+Z,eed< e-e.dZ/e0ed< e1j2Z3e1ed< de4fddZ5dee fddZ6dee fd d!Z7de&fd"d#Z8d
S )$MultiDbConfiga	  
    Configuration class for managing multiple database connections in a resilient and fail-safe manner.

    Attributes:
        databases_config: A list of database configurations.
        client_class: The client class used to manage database connections.
        command_retry: Retry strategy for executing database commands.
        failure_detectors: Optional list of additional failure detectors for monitoring database failures.
        min_num_failures: Minimal count of failures required for failover
        failure_rate_threshold: Percentage of failures required for failover
        failures_detection_window: Time interval for tracking database failures.
        health_checks: Optional list of additional health checks performed on databases.
        health_check_interval: Time interval for executing health checks.
        health_check_probes: Number of attempts to evaluate the health of a database.
        health_check_probes_delay: Delay between health check attempts.
        health_check_policy: Policy for determining database health based on health checks.
        failover_strategy: Optional strategy for handling database failover scenarios.
        failover_attempts: Number of retries allowed for failover operations.
        failover_delay: Delay between failover attempts.
        auto_fallback_interval: Time interval to trigger automatic fallback.
        event_dispatcher: Interface for dispatching events related to database operations.
        initial_health_check_policy: Defines the policy used to determine whether the databases setup is
                                     healthy during the initial health check.

    Methods:
        databases:
            Retrieves a collection of database clients managed by weighted configurations.
            Initializes database clients based on the provided configuration and removes
            redundant retry objects for lower-level clients to rely on global retry logic.

        default_failure_detectors:
            Returns the default list of failure detectors used to monitor database failures.

        default_health_checks:
            Returns the default list of health checks used to monitor database health
            with specific retry and backoff strategies.

        default_failover_strategy:
            Provides the default failover strategy used for handling failover scenarios
            with defined retry and backoff configurations.
    databases_configclient_class   
   )basecap   )backoffretriescommand_retryNfailure_detectorsmin_num_failuresfailure_rate_thresholdfailures_detection_windowhealth_checkshealth_check_intervalhealth_check_probeshealth_check_probes_delayhealth_check_policyfailover_strategyfailover_attemptsfailover_delayauto_fallback_intervalr7   event_dispatcherinitial_health_check_policyr0   c              	   C   s   t  }| jD ]d}tdt d|jd< d|jvrtdd|jd< |jr/| jj|jfi |j}n |jrF|j	tdt d | jj|jd}n	| jd	i |j}|j
d u rX| n|j
}|t|||j|jd|j q|S )
Nr   )rP   rO   retryZmaint_notifications_configF)enabled)Zconnection_pool)clientr<   r6   r>   r.   )r   rH   r'   r   r9   r   r:   rI   r;   Z	set_retryr<   rA   addr   r6   r>   )r@   	databasesZdatabase_configrc   r<   r.   r.   r/   re      sH   



zMultiDbConfig.databasesc                 C   s   t | j| j| jdgS )N)rS   rT   Zfailure_detection_window)r   rS   rT   rU   r@   r.   r.   r/   default_failure_detectors   s   z'MultiDbConfig.default_failure_detectorsc                 C   s   t  gS r2   )r&   rf   r.   r.   r/   default_health_checks   s   z#MultiDbConfig.default_health_checksc                 C   r1   r2   )r   rf   r.   r.   r/   default_failover_strategy   r4   z'MultiDbConfig.default_failover_strategy)9r*   r+   r,   rB   r   r5   rD   r
   rI   r   r   r   r'   r   rQ   rR   r   r   r   rS   intr   rT   rC   r   rU   rV   r$   r!   rW   r#   rX   r    rY   r"   rZ   r%   r[   r   r   r\   r   r]   DEFAULT_AUTO_FALLBACK_INTERVALr^   r   r3   r_   r   r)   r-   r`   r   re   rg   rh   ri   r.   r.   r.   r/   rG   ^   s8   
 *0	rG   ):dataclassesr   r   enumr   typingr   r   r   r   r?   Zredisr	   r
   r   Zredis.backoffr   r   Zredis.data_structurer   Zredis.eventr   r   Zredis.maint_notificationsr   Zredis.multidb.circuitr   r   r   Zredis.multidb.databaser   r   Zredis.multidb.failoverr   r   r   r   Zredis.multidb.failure_detectorr   r   r   r   r   Zredis.multidb.healthcheckr    r!   r"   r#   r$   r%   r&   Zredis.retryr'   rk   r)   r3   r5   rG   r.   r.   r.   r/   <module>   s,    $	&