HEX
Server: Apache
System: Linux br512.hostgator.com.br 5.14.0-162.23.1.9991722448259.nf.el9.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Jul 31 18:11:45 UTC 2024 x86_64
User: stiliz28 (2548)
PHP: 8.3.30
Disabled: NONE
Upload Files
File: //lib/python3.9/site-packages/__pycache__/circuitbreaker.cpython-39.opt-1.pyc
a

��e3/�@s�ddlmZddlmZmZddlmZddlmZmZm	Z	ddl
mZmZddl
mZddlmZmZeefZdZd	Zd
Zdd�Zd
d�ZGdd�de�ZGdd�de�ZGdd�de�Zdddddefdd�ZdS)�)�iscoroutinefunction)�datetime�	timedelta)�wraps)�isgeneratorfunction�isasyncgenfunction�isclass)�ceil�floor)�	monotonic)�AnyStr�Iterable�closed�openZ	half_opencs�fdd�}|S)zOBuild a predicate function that checks if an exception is a subtype from a listcs
t|��S�N)�
issubclass)Zthrown_type�_��	exc_types��2/usr/lib/python3.9/site-packages/circuitbreaker.py�
matches_typessz(in_exception_list.<locals>.matches_typesr)rrrrr�in_exception_listsrcCsvt|�rt|t�rt|�}nVz&t|�t|t�r8td��t|�}Wn.typt	|�r`t|�rhtd��|}Yn0|S)az Build a failure predicate_function.
          The returned function has the signature (Type[Exception], Exception) -> bool.
          Return value True indicates a failure in the underlying function.

        :param expected_exception: either an type of Exception, iterable of Exception types, or a predicate function.

          If an Exception type or iterable of Exception types, the failure predicate will return True when a thrown
          exception type matches one of the provided types.

          If a predicate function, it will just be returned as is.

         :return: callable (Type[Exception], Exception) -> bool
    z9expected_exception cannot be a string. Did you mean name?z1expected_exception does not look like a predicate)
rr�	Exceptionr�iter�
isinstance�STRING_TYPES�
ValueError�	TypeError�callable)�expected_exceptionZfailure_predicaterrr�build_failure_predicates


r!c@s�eZdZdZdZeZdZd2dd�Zdd�Z	dd	�Z
d
d�Zdd
�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zedd��Zed d!��Zed"d#��Zed$d%��Zed&d'��Zed(d)��Zed*d+��Zed,d-��Zed.d/��Zd0d1�ZdS)3�CircuitBreaker��NcCs�d|_d|_|p|j|_|p |j|_|sTzt|�jd}WntyRt	j
}Yn0t|�|_|pf|j
|_||_t|_t�|_dS)a�
        Construct a circuit breaker.

        :param failure_threshold: break open after this many failures
        :param recovery_timeout: close after this many seconds
        :param expected_exception: either an type of Exception, iterable of Exception types, or a predicate function.
        :param name: name for this circuitbreaker
        :param fallback_function: called when the circuit is opened

           :return: Circuitbreaker instance
           :rtype: Circuitbreaker
        Nr�EXPECTED_EXCEPTION)�
_last_failure�_failure_count�FAILURE_THRESHOLD�_failure_threshold�RECOVERY_TIMEOUT�_recovery_timeout�type�__dict__�KeyErrorr"r%r!�
is_failure�FALLBACK_FUNCTION�_fallback_function�_name�STATE_CLOSED�_stater�_opened)�self�failure_threshold�recovery_timeoutr �name�fallback_functionrrr�__init__@s
zCircuitBreaker.__init__cCs
|�|�Sr)�decorate)r6�wrappedrrr�__call__kszCircuitBreaker.__call__cCsdSrr�r6rrr�	__enter__nszCircuitBreaker.__enter__cCs,|r |�||�r ||_|��n|��dS)NF)r/r&�_CircuitBreaker__call_failed�_CircuitBreaker__call_succeeded)r6�exc_type�	exc_valueZ
_tracebackrrr�__exit__qs

zCircuitBreaker.__exit__cCs`|jdur2z|j|_Wnty0|j|_Yn0t�|�t|�sLt|�rV|�|�S|�	|�S)z;
        Applies the circuit breaker to a function
        N)
r2�__qualname__�AttributeError�__name__�CircuitBreakerMonitor�registerrr�_decorate_async�_decorate_sync)r6�functionrrrr<zs


zCircuitBreaker.decoratecs<t����fdd��}t����fdd��}t��r8|S|S)Ncs<�jr$�jr�j|i|��St����j�g|�Ri|��Sr)�openedr:�CircuitBreakerError�call��args�kwargs�rMr6rr�wrapper�s
z.CircuitBreaker._decorate_sync.<locals>.wrapperc?sP�jr.�jr&�j|i|��EdHdSt����j�g|�Ri|��EdHdSr)rNr:rO�call_generatorrQrTrr�gen_wrapper�sz2CircuitBreaker._decorate_sync.<locals>.gen_wrapper)rr)r6rMrUrWrrTrrL�s
zCircuitBreaker._decorate_synccs<t����fdd��}t����fdd��}t��r8|S|S)Nc�sH�jr*�jr"�j|i|��IdHSt����j�g|�Ri|��IdHSr)rNr:rO�
call_asyncrQrTrr�awrapper�s
z0CircuitBreaker._decorate_async.<locals>.awrappercsp�jr>�jr6�j|i|��2z3dHW}|Vq6dSt����j�g|�Ri|��2z3dHW}|VqV6dSr)rNr:rO�call_async_generator)rRrS�elrTrr�gen_awrapper�s
$z4CircuitBreaker._decorate_async.<locals>.gen_awrapper)rr)r6rMrYr\rrTrrK�s

zCircuitBreaker._decorate_asynccOs6|�||i|��Wd�S1s(0YdS)z�
        Calls the decorated function and applies the circuit breaker
        rules on success or failure
        :param func: Decorated function
        Nr�r6�funcrRrSrrrrP�szCircuitBreaker.callcosB|�*||i|��D]
}|VqWd�n1s40YdS)z�
        Calls the decorated generator function and applies the circuit breaker
        rules on success or failure
        :param func: Decorated generator function
        Nr�r6r^rRrSr[rrrrV�szCircuitBreaker.call_generatorc�s<|�$||i|��IdHWd�S1s.0YdS)z�
        Calls the decorated async function and applies the circuit breaker
        rules on success or failure
        :param func: Decorated async function
        Nrr]rrrrX�szCircuitBreaker.call_asynccOsL|�4||i|��2z3dHW}|Vq6Wd�n1s>0YdS)z�
        Calls the decorated async generator function and applies the circuit breaker
        rules on success or failure
        :param func: Decorated async generator function
        Nrr_rrrrZ�sz#CircuitBreaker.call_async_generatorcCst|_d|_d|_dS)zR
        Close circuit after successful execution and reset failure count
        Nr)r3r4r&r'r?rrrZ__call_succeeded�szCircuitBreaker.__call_succeededcCs,|jd7_|j|jkr(t|_t�|_dS)zO
        Count failure and open circuit, if threshold has been reached
        �N)r'r)�
STATE_OPENr4rr5r?rrrZ
__call_failed�szCircuitBreaker.__call_failedcCs|jtkr|jdkrtS|jS�Nr)r4ra�open_remaining�STATE_HALF_OPENr?rrr�state�szCircuitBreaker.statecCst��t|jd�S)zq
        The approximate datetime when the circuit breaker will try to recover
        :return: datetime
        )Zseconds)rZutcnowrrcr?rrr�
open_until�szCircuitBreaker.open_untilcCs*|j|jt�}|dkr"t|�St|�S)zk
        Number of seconds remaining, the circuit breaker stays in OPEN state
        :return: int
        r)r5r+rr	r
)r6Zremainrrrrc�szCircuitBreaker.open_remainingcCs|jSr)r'r?rrr�
failure_countszCircuitBreaker.failure_countcCs
|jtkSr)rer3r?rrrrszCircuitBreaker.closedcCs
|jtkSr)rerar?rrrrNszCircuitBreaker.openedcCs|jSr�r2r?rrrr9szCircuitBreaker.namecCs|jSr)r&r?rrr�last_failureszCircuitBreaker.last_failurecCs|jSr)r1r?rrrr:sz CircuitBreaker.fallback_functioncOs|jSrrh�r6rRrSrrr�__str__szCircuitBreaker.__str__)NNNNN) rH�
__module__rFr(r*rr%r0r;r>r@rEr<rLrKrPrVrXrZrBrA�propertyrerfrcrgrrNr9rir:rkrrrrr":sR�
+		
	
	








r"cs$eZdZ�fdd�Zdd�Z�ZS)rOcs tt|�j|i|��||_dS)zf
        :param circuit_breaker:
        :param args:
        :param kwargs:
        :return:
        N)�superrOr;�_circuit_breaker)r6�circuit_breakerrRrS��	__class__rrr; szCircuitBreakerError.__init__cOs*d|jj|jj|jjt|jj�|jjfS)NzMCircuit "%s" OPEN until %s (%d failures, %d sec remaining) (last_failure: %r))ror9rfrg�roundrcrirjrrrrk*s
�zCircuitBreakerError.__str__)rHrlrFr;rk�
__classcell__rrrqrrOs
rOc@s�eZdZiZedd��Zeed�dd��Zeee	d�dd��Z
eee	d�d	d
��Zeee	d�dd��Z
eee	d�d
d��ZdS)rIcCs||j|j<dSr)�circuit_breakersr9)�clsrprrrrJ7szCircuitBreakerMonitor.register)�returncCstt|����dkSrb)�len�list�get_open�rvrrr�
all_closed;sz CircuitBreakerMonitor.all_closedcCs
|j��Sr)ru�valuesr{rrr�get_circuits?sz"CircuitBreakerMonitor.get_circuits)r9rwcCs|j�|�Sr)ru�get)rvr9rrrrCszCircuitBreakerMonitor.getccs|��D]}|jr|VqdSr)r~rN�rv�circuitrrrrzGszCircuitBreakerMonitor.get_openccs|��D]}|jr|VqdSr)r~rr�rrr�
get_closedMsz CircuitBreakerMonitor.get_closedN)rHrlrFru�classmethodrJ�boolr|r
r"r~rrrzr�rrrrrI4s
rINcCs*t|�r|��|�S||||||d�SdS)N)r7r8r r9r:)rr<)r7r8r r9r:rvrrrr�Ts�r�) Zasynciorrr�	functoolsr�inspectrrrZmathr	r
�timer�typingrr
�bytes�strrr3rardrr!�objectr"rrOrIr�rrrr�<module>s.	"f �