o
    <‘ði¾%  ã                   @   s^  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mZmZmZ d dlmZmZ d d	lmZmZmZmZmZm Z 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, d dl-m.Z.m/Z/m0Z0 d dl1m2Z2m3Z3m4Z4m5Z5 dZ6G dd„ deƒZ7de,fdd„Z8eG dd„ dƒƒZ9eG dd„ dƒƒZ:dS )é    )Ú	dataclassÚfield)ÚEnum)ÚListÚOptionalÚTypeÚUnionN)ÚConnectionPoolÚRedisÚRedisCluster)ÚDatabaseÚ	Databases)ÚDEFAULT_FAILOVER_ATTEMPTSÚDEFAULT_FAILOVER_DELAYÚAsyncFailoverStrategyÚWeightBasedFailoverStrategy)ÚAsyncFailureDetectorÚFailureDetectorAsyncWrapper)ÚDEFAULT_HEALTH_CHECK_DELAYÚDEFAULT_HEALTH_CHECK_INTERVALÚDEFAULT_HEALTH_CHECK_POLICYÚDEFAULT_HEALTH_CHECK_PROBESÚDEFAULT_HEALTH_CHECK_TIMEOUTÚHealthCheckÚHealthCheckPoliciesÚPingHealthCheck)ÚRetry)ÚExponentialWithJitterBackoffÚ	NoBackoff)ÚWeightedList)ÚEventDispatcherÚEventDispatcherInterface)ÚDEFAULT_GRACE_PERIODÚCircuitBreakerÚPBCircuitBreakerAdapter)ÚDEFAULT_FAILURE_RATE_THRESHOLDÚ!DEFAULT_FAILURES_DETECTION_WINDOWÚDEFAULT_MIN_NUM_FAILURESÚCommandFailureDetectoréx   c                   @   s   e Zd ZdZdZdZdS )ÚInitialHealthCheckÚall_availableÚmajority_availableÚone_availableN)Ú__name__Ú
__module__Ú__qualname__ÚALL_AVAILABLEÚMAJORITY_AVAILABLEÚONE_AVAILABLE© r4   r4   ú[/root/parts/websockify/install/lib/python3.10/site-packages/redis/asyncio/multidb/config.pyr*   0   s    r*   Úreturnc                   C   ó   t ƒ S ©N)r    r4   r4   r4   r5   Údefault_event_dispatcher6   ó   r9   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_urlr6   c                 C   s   t j| jd�}t|ƒS )N)Úreset_timeout)Ú	pybreakerr#   rC   r$   )ÚselfÚcircuit_breakerr4   r4   r5   Údefault_circuit_breaker\   s   z&DatabaseConfig.default_circuit_breaker)r.   r/   r0   Ú__doc__r<   ÚfloatÚ__annotations__r   Údictr?   r@   r   ÚstrrA   r	   rB   r#   r"   rC   rD   rI   r4   r4   r4   r5   r;   :   s   
 r;   c                   @   sn  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< 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/e0d�Z1e2ed< e3j4Z5e3ed< de6fdd„Z7dee fdd „Z8dee fd!d"„Z9de(fd#d$„Z:d
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_delay: Delay between health check attempts.
        health_check_timeout: Timeout for the full health check operation (including all probes).
        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_delayÚhealth_check_timeoutÚhealth_check_policyÚfailover_strategyÚfailover_attemptsÚfailover_delayÚauto_fallback_intervalr=   Úevent_dispatcherÚinitial_health_check_policyr6   c              	   C   sÆ   t ƒ }| jD ]Z}|j dtdtƒ d�i¡ |jr%| jj|jfi |j¤Ž}n |jr<|j 	tdtƒ d�¡ | jj|jd�}n	| jdi |j¤Ž}|j
d u rN| ¡ n|j
}| t|||j|jd�|j¡ q|S )NÚretryr   )rX   rW   )Úconnection_pool)ÚclientrB   r<   rD   r4   )r   rP   r?   Úupdater   r   r@   rQ   rA   Ú	set_retryrB   rI   Úaddr   r<   rD   )rG   Ú	databasesÚdatabase_configrl   rB   r4   r4   r5   rp   ¦   sB   
ÿÿÿÿÿ
ÿýüù
zMultiDbConfig.databasesc                 C   s   t t| j| j| jd�ƒgS )N)r[   r\   Úfailure_detection_window)r   r(   r[   r\   r]   ©rG   r4   r4   r5   Údefault_failure_detectorsÏ   s   ýÿÿz'MultiDbConfig.default_failure_detectorsc                 C   s   t | j| j| jd�gS )N)r`   ra   rb   )r   r`   ra   rb   rs   r4   r4   r5   Údefault_health_checksÚ   s   ýÿz#MultiDbConfig.default_health_checksc                 C   r7   r8   )r   rs   r4   r4   r5   Údefault_failover_strategyã   r:   z'MultiDbConfig.default_failover_strategy);r.   r/   r0   rJ   r   r;   rL   r
   rQ   r   r   r   r   r   rY   rZ   r   r   r'   r[   Úintr%   r\   rK   r&   r]   r^   r   r   r_   r   r`   r   ra   r   rb   r   rc   r   rd   r   r   re   r   rf   ÚDEFAULT_AUTO_FALLBACK_INTERVALrg   r   r9   rh   r!   r*   r1   ri   r   rp   rt   ru   rv   r4   r4   r4   r5   rO   a   s:   
 +ÿÿ)	rO   );Údataclassesr   r   Úenumr   Útypingr   r   r   r   rF   Úredis.asyncior	   r
   r   Úredis.asyncio.multidb.databaser   r   Úredis.asyncio.multidb.failoverr   r   r   r   Ú&redis.asyncio.multidb.failure_detectorr   r   Ú!redis.asyncio.multidb.healthcheckr   r   r   r   r   r   r   r   Úredis.asyncio.retryr   Úredis.backoffr   r   Úredis.data_structurer   Úredis.eventr    r!   Úredis.multidb.circuitr"   r#   r$   Úredis.multidb.failure_detectorr%   r&   r'   r(   rx   r*   r9   r;   rO   r4   r4   r4   r5   Ú<module>   s,    (
&