o
    ýe>  ã                   @   sf   d Z ddlZddlZddlmZ ddlmZ ddlmZ e 	e
¡ZG dd„ deƒZG dd	„ d	eƒZdS )
z;
This module defines a common configuration class for BCE.
é    N)Ústr)Úbytes)ÚBceServerErrorc                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚNoRetryPolicyzA policy that never retries.c                 C   ó   dS )zšAlways returns False.

        :param error: ignored
        :param retries_attempted: ignored
        :return: always False
        :rtype: bool
        F© ©ÚselfÚerrorÚretries_attemptedr   r   úKD:\Projects\ConvertPro\env\Lib\site-packages\baidubce/retry/retry_policy.pyÚshould_retry   ó   zNoRetryPolicy.should_retryc                 C   r   )z‘Always returns 0.

        :param error: ignored
        :param retries_attempted: ignored
        :return: always 0
        :rtype: int
        r   r   r   r   r   r   Ú%get_delay_before_next_retry_in_millis)   r   z3NoRetryPolicy.get_delay_before_next_retry_in_millisN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r      s    
r   c                   @   s0   e Zd ZdZ			ddd„Zdd„ Zd	d
„ ZdS )ÚBackOffRetryPolicyaÆ  A policy that retries with exponential back-off strategy.

    This policy will keep retrying until the maximum number of retries is reached. The delay time
    will be a fixed interval for the first time then 2 * interval for the second, 4 * internal for
    the third, and so on. In general, the delay time will be 2^number_of_retries_attempted*interval.

    When a maximum of delay time is specified, the delay time will never exceed this limit.
    é   é N  é,  c                 C   s6   |dk rt dƒ‚|dk rt dƒ‚|| _|| _|| _dS )a®  
        :param max_error_retry: the maximum number of retries.
        :type max_error_retry: int
        :param max_delay_in_millis: the maximum of delay time in milliseconds.
        :type max_delay_in_millis: int
        :param base_interval_in_millis: the base delay interval in milliseconds.
        :type base_interval_in_millis: int
        :raise ValueError if max_error_retry or max_delay_in_millis is negative.
        r   s1   max_error_retry should be a non-negative integer.s5   max_delay_in_millis should be a non-negative integer.N)Ú
ValueErrorÚmax_error_retryÚmax_delay_in_millisÚbase_interval_in_millis)r	   r   r   r   r   r   r   Ú__init__>   s   
zBackOffRetryPolicy.__init__c                 C   s†   || j krdS t|tƒrt d¡ dS t|tƒrA|jtjj	kr&t d¡ dS |jtjj
kr4t d¡ dS |jtjkrAt d¡ dS dS )a_  Return true if the http client should retry the request.

        :param error: the caught error.
        :type error: Exception
        :param retries_attempted: the number of retries which has been attempted before.
        :type retries_attempted: int
        :return: true if the http client should retry the request.
        :rtype: bool
        Fs   Retry for IOError.Ts    Retry for internal server error.s   Retry for service unavailable.s   Retry for request expired.)r   Ú
isinstanceÚIOErrorÚ_loggerÚdebugr   Ústatus_codeÚhttpÚclientÚINTERNAL_SERVER_ERRORÚSERVICE_UNAVAILABLEÚcodeZREQUEST_EXPIREDr   r   r   r   r   T   s    






zBackOffRetryPolicy.should_retryc                 C   s.   |dk rdS d|> | j  }|| jkr| jS |S )ag  Returns the delay time in milliseconds before the next retry.

        :param error: the caught error.
        :type error: Exception
        :param retries_attempted: the number of retries which has been attempted before.
        :type retries_attempted: int
        :return: the delay time in milliseconds before the next retry.
        :rtype: int
        r   é   )r   r   )r	   r
   r   Zdelay_in_millisr   r   r   r   v   s   

z8BackOffRetryPolicy.get_delay_before_next_retry_in_millisN)r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   4   s    

ý"r   )r   Úhttp.clientr"   ÚloggingÚbuiltinsr   r   Zbaidubce.exceptionr   Ú	getLoggerr   r   Úobjectr   r   r   r   r   r   Ú<module>   s   
