o
    `۷id                     @   s  d dl Z d dlZd dlZd dlZd dlmZ d dlmZmZm	Z	 zd dlm
Z
 W n ey7   d dlm
Z
 Y nw eeZe	dZe
dZ			dd	eeef d
edeee  dededejdejdefddZ			dd
edeee  dededeeeef geeef f f
ddZdS )    N)Sequence)CallableOptionalTypeVar)	ParamSpecRP
       fdescriptionmatchmax_attemptsmax_backoff_sargskwargsreturnc                    s$  |dksJ d| dt |D ]}z
| |i |W   S  ty } zgt| |du p6t fdd|D }	|	rc|d |k rctd| |tdd	 }
td
|d  d| d|
 d t	
|
 n"|	rttd| d| d |dtd  d|d  d |dW Y d}~qd}~ww dS )al  Retry a function with exponential backoff.

    Args:
        f: The function to retry.
        description: An imperative description of the function being retried. For
            example, "open the file".
        match: A sequence of strings to match in the exception message.
            If ``None``, any error is retried.
        max_attempts: The maximum number of attempts to retry.
        max_backoff_s: The maximum number of seconds to backoff.
        *args: Arguments to pass to the function.
        **kwargs: Keyword arguments to pass to the function.

    Returns:
        The result of the function.
       z%`max_attempts` must be positive. Got .Nc                 3   s    | ]}| v V  qd S N ).0patternexception_strr   G/home/ubuntu/vllm_env/lib/python3.10/site-packages/ray/_common/retry.py	<genexpr>4   s    
z"call_with_retry.<locals>.<genexpr>   g?g333333?z	Retrying z attempts to z after z	 seconds.z
Failed to z attempts. Raising.zDid not find a match for z. Raising after z
 attempts.)range	Exceptionstranyminrandomuniformloggerdebugtimesleep)r   r   r   r   r   r   r   ieis_retryablebackoffr   r   r   call_with_retry   s8   r-   c                    s0   dt ttf dt ttf f fdd}|S )a  Decorator-based version of call_with_retry.

    Args:
        description: An imperative description of the function being retried. For
            example, "open the file".
        match: A sequence of strings to match in the exception message.
            If ``None``, any error is retried.
        max_attempts: The maximum number of attempts to retry.
        max_backoff_s: The maximum number of seconds to backoff.

    Returns:
        A Callable that can be applied in a normal decorator fashion.
    funcr   c                    s4   t  dtjdtjdtf fdd}|S )Nr   r   r   c                     s   t  g| R i |S r   )r-   )r   r   )r   r.   r   r   r   r   r   inner_   s   
z'retry.<locals>.decorator.<locals>.inner)	functoolswrapsr   r   r   r   )r.   r/   r   r   r   r   )r.   r   	decorator^   s   (zretry.<locals>.decorator)r   r   r   )r   r   r   r   r3   r   r2   r   retryJ   s   ,	r4   )Nr	   r
   )r0   loggingr#   r'   collections.abcr   typingr   r   r   r   ImportErrortyping_extensions	getLogger__name__r%   r   r   r    intr   r   r-   r4   r   r   r   r   <module>   s`    



9
