
    Vh                      p    d dl mZmZmZ eZ	 d dlZd dlmZ d dl	Z	d dl
Z
ddZdddefdZ G d	 d
e      Zy)    )absolute_importdivisionprint_functionNwraps
   <   c                       fd}|S )a   Customizable exponential backoff strategy.
    Args:
        retries (int): Maximum number of times to retry a request.
        delay (float): Initial (base) delay.
        backoff (float): base of the exponent to use for exponential
            backoff.
        max_delay (int): Optional. If provided each delay generated is capped
            at this amount. Defaults to 60 seconds.
    Returns:
        Callable that returns a generator. This generator yields durations in
        seconds to be used as delays for an exponential backoff strategy.
    Usage:
        >>> backoff = _exponential_backoff()
        >>> backoff
        <function backoff_backoff at 0x7f0d939facf8>
        >>> list(backoff())
        [2, 4, 8, 16, 32, 60, 60, 60, 60, 60]
    c               3   d   K   t        d      D ]  } | z  z  }|nt        |        y w)Nr   )rangemin)retrysleepbackoffdelay	max_delayretriess     p/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/module_utils/cloud.pybackoff_genz)_exponential_backoff.<locals>.backoff_gen;   sB     1g& 	HEGu,,E$,%#eY2GG	Hs   -0 )r   r   r   r   r   s   ```` r   _exponential_backoffr   (   s    &H        c                       fd}|S )a   Implements the "Full Jitter" backoff strategy described here
    https://www.awsarchitectureblog.com/2015/03/backoff.html
    Args:
        retries (int): Maximum number of times to retry a request.
        delay (float): Approximate number of seconds to sleep for the first
            retry.
        max_delay (int): The maximum number of seconds to sleep for any retry.
            _random (random.Random or None): Makes this generator testable by
            allowing developers to explicitly pass in the a seeded Random.
    Returns:
        Callable that returns a generator. This generator yields durations in
        seconds to be used as delays for a full jitter backoff strategy.
    Usage:
        >>> backoff = _full_jitter_backoff(retries=5)
        >>> backoff
        <function backoff_backoff at 0x7f0d939facf8>
        >>> list(backoff())
        [3, 6, 5, 23, 38]
        >>> list(backoff())
        [2, 1, 6, 6, 31]
    c            
   3   x   K   t        d      D ]&  } j                  dt        d| z  z               ( y w)Nr      )r   randintr   )r   _randomr   r   r   s    r   r   z)_full_jitter_backoff.<locals>.backoff_genX   s@     1g& 	IE//!SEAJ4F%GHH	Is   7:r   )r   r   r   r   r   s   ```` r   _full_jitter_backoffr   B   s    ,I r   c                   ~    e Zd ZdZdZed        Zed	d       Zed	d       Z	ed
d       Z
edd       Zedd       Zy)
CloudRetryz CloudRetry can be used by any cloud provider, in order to implement a
        backoff algorithm/retry effect based on Status Code from Exceptions.
    Nc                      y)zz Return the status code from the exception object
        Args:
            error (object): The exception itself.
        Nr   )errors    r   status_code_from_exceptionz%CloudRetry.status_code_from_exceptionf        	r   c                      y)z Return True if the Response Code to retry on was found.
        Args:
            response_code (str): This is the Response Code that is being matched against.
        Nr   )response_codecatch_extra_error_codess     r   foundzCloudRetry.foundn   r%   r   c                       fd}|S )a$   Retry calling the Cloud decorated function using the provided
        backoff strategy.
        Args:
            backoff_strategy (callable): Callable that returns a generator. The
            generator should yield sleep times for each retry of the decorated
            function.
        c                 6     t                fd       }|S )Nc                             D ]  }	  	| i |c S   	| i |S # t         $ r}t        |j                        rzj                  |      }j	                  |      rUdj                  t        |      |      }t        j                  t        j                  |       t        j                  |       n||Y d }~d }~ww xY w)Nz{0}: Retrying in {1} seconds...)	Exception
isinstance
base_classr$   r)   formatstrsyslogLOG_INFOtimer   )
argskwargsr   er'   msgbackoff_strategyr(   clsfs
         r   
retry_funcz5CloudRetry._backoff.<locals>.deco.<locals>.retry_func   s    -/ $E$ $1&11$  $)&)) % $%a8,/,J,J1,MM"yy8OP&G&N&NsSTvW\&] &foos C $

5 1 '( #$G !2$s    	CBC  Cr   )r;   r<   r9   r(   r:   s   ` r   decoz!CloudRetry._backoff.<locals>.deco   s!    1X* *& r   r   )r:   r9   r(   r=   s   ``` r   _backoffzCloudRetry._backoffv   s    	. r   c                 @    | j                  t        ||||      |      S )aE  
        Retry calling the Cloud decorated function using an exponential backoff.

        Kwargs:
            retries (int): Number of times to retry a failed request before giving up
                default=10
            delay (int or float): Initial delay between retries in seconds
                default=3
            backoff (int or float): backoff multiplier e.g. value of 2 will
                double the delay each retry
                default=1.1
            max_delay (int or None): maximum amount of time to wait between retries.
                default=60
        )r   r   r   r   )r>   r   )r:   r   r   r   r   r(   s         r   exponential_backoffzCloudRetry.exponential_backoff   s,      ||05'YPQhj 	jr   c                 >    | j                  t        |||      |      S )a  
        Retry calling the Cloud decorated function using a jittered backoff
        strategy. More on this strategy here:

        https://www.awsarchitectureblog.com/2015/03/backoff.html

        Kwargs:
            retries (int): Number of times to retry a failed request before giving up
                default=10
            delay (int): Initial delay between retries in seconds
                default=3
            max_delay (int): maximum amount of time to wait between retries.
                default=60
        )r   r   r   )r>   r   )r:   r   r   r   r(   s        r   jittered_backoffzCloudRetry.jittered_backoff   s)      ||05I?@WY 	Yr   c                 4    | j                  |dz
  ||d|      S )a  
        Retry calling the Cloud decorated function using an exponential backoff.

        Compatibility for the original implementation of CloudRetry.backoff that
        did not provide configurable backoff strategies. Developers should use
        CloudRetry.exponential_backoff instead.

        Kwargs:
            tries (int): Number of times to try (not retry) before giving up
                default=10
            delay (int or float): Initial delay between retries in seconds
                default=3
            backoff (int or float): backoff multiplier e.g. value of 2 will
                double the delay each retry
                default=1.1
           N)r   r   r   r   r(   )r@   )r:   triesr   r   r(   s        r   r   zCloudRetry.backoff   s.    $ &&AIUGte| ' ~ 	~r   )N)r   r   r   r	   N)r   r   r	   N)r   r   g?N)__name__
__module____qualname____doc__r/   staticmethodr$   r)   classmethodr>   r@   rB   r   r   r   r   r!   r!   ^   s    
 J     B j j$ Y Y$ ~ ~r   r!   )r   r   r   r	   )
__future__r   r   r   type__metaclass__random	functoolsr   r2   r4   r   r   objectr!   r   r   r   <module>rR      sK    C B.    4 "$1F 8s~ s~r   