Add loan-advisor-agent
Browse files- loan-advisor-agent/.venv/Lib/site-packages/tenacity/__init__.py +750 -0
- loan-advisor-agent/.venv/Lib/site-packages/tenacity/__pycache__/__init__.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/tenacity/__pycache__/_utils.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/tenacity/__pycache__/after.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/tenacity/__pycache__/before.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/tenacity/__pycache__/before_sleep.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/tenacity/__pycache__/nap.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/tenacity/__pycache__/retry.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/tenacity/__pycache__/stop.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/tenacity/__pycache__/wait.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/tenacity/_utils.py +113 -0
- loan-advisor-agent/.venv/Lib/site-packages/tenacity/after.py +49 -0
- loan-advisor-agent/.venv/Lib/site-packages/tenacity/asyncio/__init__.py +210 -0
- loan-advisor-agent/.venv/Lib/site-packages/tenacity/asyncio/__pycache__/__init__.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/tenacity/asyncio/__pycache__/retry.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/tenacity/asyncio/retry.py +125 -0
- loan-advisor-agent/.venv/Lib/site-packages/tenacity/before.py +46 -0
- loan-advisor-agent/.venv/Lib/site-packages/tenacity/before_sleep.py +71 -0
- loan-advisor-agent/.venv/Lib/site-packages/tenacity/nap.py +43 -0
- loan-advisor-agent/.venv/Lib/site-packages/tenacity/py.typed +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/tenacity/retry.py +282 -0
- loan-advisor-agent/.venv/Lib/site-packages/tenacity/stop.py +130 -0
- loan-advisor-agent/.venv/Lib/site-packages/tenacity/tornadoweb.py +63 -0
- loan-advisor-agent/.venv/Lib/site-packages/tenacity/wait.py +273 -0
loan-advisor-agent/.venv/Lib/site-packages/tenacity/__init__.py
ADDED
|
@@ -0,0 +1,750 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2016-2018 Julien Danjou
|
| 2 |
+
# Copyright 2017 Elisey Zanko
|
| 3 |
+
# Copyright 2016 Étienne Bersac
|
| 4 |
+
# Copyright 2016 Joshua Harlow
|
| 5 |
+
# Copyright 2013-2014 Ray Holder
|
| 6 |
+
#
|
| 7 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 8 |
+
# you may not use this file except in compliance with the License.
|
| 9 |
+
# You may obtain a copy of the License at
|
| 10 |
+
#
|
| 11 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 12 |
+
#
|
| 13 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 14 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 15 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 16 |
+
# See the License for the specific language governing permissions and
|
| 17 |
+
# limitations under the License.
|
| 18 |
+
import dataclasses
|
| 19 |
+
import functools
|
| 20 |
+
import sys
|
| 21 |
+
import threading
|
| 22 |
+
import time
|
| 23 |
+
import types
|
| 24 |
+
import typing as t
|
| 25 |
+
import warnings
|
| 26 |
+
from abc import ABC, abstractmethod
|
| 27 |
+
from concurrent import futures
|
| 28 |
+
|
| 29 |
+
from . import _utils
|
| 30 |
+
|
| 31 |
+
# Import all built-in retry strategies for easier usage.
|
| 32 |
+
from .retry import retry_base # noqa
|
| 33 |
+
from .retry import retry_all # noqa
|
| 34 |
+
from .retry import retry_always # noqa
|
| 35 |
+
from .retry import retry_any # noqa
|
| 36 |
+
from .retry import retry_if_exception # noqa
|
| 37 |
+
from .retry import retry_if_exception_type # noqa
|
| 38 |
+
from .retry import retry_if_exception_cause_type # noqa
|
| 39 |
+
from .retry import retry_if_not_exception_type # noqa
|
| 40 |
+
from .retry import retry_if_not_result # noqa
|
| 41 |
+
from .retry import retry_if_result # noqa
|
| 42 |
+
from .retry import retry_never # noqa
|
| 43 |
+
from .retry import retry_unless_exception_type # noqa
|
| 44 |
+
from .retry import retry_if_exception_message # noqa
|
| 45 |
+
from .retry import retry_if_not_exception_message # noqa
|
| 46 |
+
|
| 47 |
+
# Import all nap strategies for easier usage.
|
| 48 |
+
from .nap import sleep # noqa
|
| 49 |
+
from .nap import sleep_using_event # noqa
|
| 50 |
+
|
| 51 |
+
# Import all built-in stop strategies for easier usage.
|
| 52 |
+
from .stop import stop_after_attempt # noqa
|
| 53 |
+
from .stop import stop_after_delay # noqa
|
| 54 |
+
from .stop import stop_before_delay # noqa
|
| 55 |
+
from .stop import stop_all # noqa
|
| 56 |
+
from .stop import stop_any # noqa
|
| 57 |
+
from .stop import stop_never # noqa
|
| 58 |
+
from .stop import stop_when_event_set # noqa
|
| 59 |
+
|
| 60 |
+
# Import all built-in wait strategies for easier usage.
|
| 61 |
+
from .wait import wait_chain # noqa
|
| 62 |
+
from .wait import wait_combine # noqa
|
| 63 |
+
from .wait import wait_exception # noqa
|
| 64 |
+
from .wait import wait_exponential # noqa
|
| 65 |
+
from .wait import wait_fixed # noqa
|
| 66 |
+
from .wait import wait_incrementing # noqa
|
| 67 |
+
from .wait import wait_none # noqa
|
| 68 |
+
from .wait import wait_random # noqa
|
| 69 |
+
from .wait import wait_random_exponential # noqa
|
| 70 |
+
from .wait import wait_random_exponential as wait_full_jitter # noqa
|
| 71 |
+
from .wait import wait_exponential_jitter # noqa
|
| 72 |
+
|
| 73 |
+
# Import all built-in before strategies for easier usage.
|
| 74 |
+
from .before import before_log # noqa
|
| 75 |
+
from .before import before_nothing # noqa
|
| 76 |
+
|
| 77 |
+
# Import all built-in after strategies for easier usage.
|
| 78 |
+
from .after import after_log # noqa
|
| 79 |
+
from .after import after_nothing # noqa
|
| 80 |
+
|
| 81 |
+
# Import all built-in before sleep strategies for easier usage.
|
| 82 |
+
from .before_sleep import before_sleep_log # noqa
|
| 83 |
+
from .before_sleep import before_sleep_nothing # noqa
|
| 84 |
+
|
| 85 |
+
try:
|
| 86 |
+
import tornado
|
| 87 |
+
except ImportError:
|
| 88 |
+
tornado = None
|
| 89 |
+
|
| 90 |
+
if t.TYPE_CHECKING:
|
| 91 |
+
from typing_extensions import Self
|
| 92 |
+
|
| 93 |
+
from . import asyncio as tasyncio
|
| 94 |
+
from .retry import RetryBaseT
|
| 95 |
+
from .stop import StopBaseT
|
| 96 |
+
from .wait import WaitBaseT
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
WrappedFnReturnT = t.TypeVar("WrappedFnReturnT")
|
| 100 |
+
WrappedFn = t.TypeVar("WrappedFn", bound=t.Callable[..., t.Any])
|
| 101 |
+
P = t.ParamSpec("P")
|
| 102 |
+
R = t.TypeVar("R")
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
@dataclasses.dataclass(slots=True)
|
| 106 |
+
class IterState:
|
| 107 |
+
actions: t.List[t.Callable[["RetryCallState"], t.Any]] = dataclasses.field(
|
| 108 |
+
default_factory=list
|
| 109 |
+
)
|
| 110 |
+
retry_run_result: bool = False
|
| 111 |
+
delay_since_first_attempt: int = 0
|
| 112 |
+
stop_run_result: bool = False
|
| 113 |
+
is_explicit_retry: bool = False
|
| 114 |
+
|
| 115 |
+
def reset(self) -> None:
|
| 116 |
+
self.actions = []
|
| 117 |
+
self.retry_run_result = False
|
| 118 |
+
self.delay_since_first_attempt = 0
|
| 119 |
+
self.stop_run_result = False
|
| 120 |
+
self.is_explicit_retry = False
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
class TryAgain(Exception):
|
| 124 |
+
"""Always retry the executed function when raised."""
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
NO_RESULT = object()
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
class DoAttempt:
|
| 131 |
+
pass
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
class DoSleep(float):
|
| 135 |
+
pass
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
class BaseAction:
|
| 139 |
+
"""Base class for representing actions to take by retry object.
|
| 140 |
+
|
| 141 |
+
Concrete implementations must define:
|
| 142 |
+
- __init__: to initialize all necessary fields
|
| 143 |
+
- REPR_FIELDS: class variable specifying attributes to include in repr(self)
|
| 144 |
+
- NAME: for identification in retry object methods and callbacks
|
| 145 |
+
"""
|
| 146 |
+
|
| 147 |
+
REPR_FIELDS: t.Sequence[str] = ()
|
| 148 |
+
NAME: t.Optional[str] = None
|
| 149 |
+
|
| 150 |
+
def __repr__(self) -> str:
|
| 151 |
+
state_str = ", ".join(
|
| 152 |
+
f"{field}={getattr(self, field)!r}" for field in self.REPR_FIELDS
|
| 153 |
+
)
|
| 154 |
+
return f"{self.__class__.__name__}({state_str})"
|
| 155 |
+
|
| 156 |
+
def __str__(self) -> str:
|
| 157 |
+
return repr(self)
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
class RetryAction(BaseAction):
|
| 161 |
+
REPR_FIELDS = ("sleep",)
|
| 162 |
+
NAME = "retry"
|
| 163 |
+
|
| 164 |
+
def __init__(self, sleep: t.SupportsFloat) -> None:
|
| 165 |
+
self.sleep = float(sleep)
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
_unset = object()
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
def _first_set(first: t.Union[t.Any, object], second: t.Any) -> t.Any:
|
| 172 |
+
return second if first is _unset else first
|
| 173 |
+
|
| 174 |
+
|
| 175 |
+
class RetryError(Exception):
|
| 176 |
+
"""Encapsulates the last attempt instance right before giving up."""
|
| 177 |
+
|
| 178 |
+
def __init__(self, last_attempt: "Future") -> None:
|
| 179 |
+
self.last_attempt = last_attempt
|
| 180 |
+
super().__init__(last_attempt)
|
| 181 |
+
|
| 182 |
+
def reraise(self) -> t.NoReturn:
|
| 183 |
+
if self.last_attempt.failed:
|
| 184 |
+
raise self.last_attempt.result()
|
| 185 |
+
raise self
|
| 186 |
+
|
| 187 |
+
def __str__(self) -> str:
|
| 188 |
+
return f"{self.__class__.__name__}[{self.last_attempt}]"
|
| 189 |
+
|
| 190 |
+
|
| 191 |
+
class AttemptManager:
|
| 192 |
+
"""Manage attempt context."""
|
| 193 |
+
|
| 194 |
+
def __init__(self, retry_state: "RetryCallState"):
|
| 195 |
+
self.retry_state = retry_state
|
| 196 |
+
|
| 197 |
+
def __enter__(self) -> None:
|
| 198 |
+
pass
|
| 199 |
+
|
| 200 |
+
def __exit__(
|
| 201 |
+
self,
|
| 202 |
+
exc_type: t.Optional[t.Type[BaseException]],
|
| 203 |
+
exc_value: t.Optional[BaseException],
|
| 204 |
+
traceback: t.Optional["types.TracebackType"],
|
| 205 |
+
) -> t.Optional[bool]:
|
| 206 |
+
if exc_type is not None and exc_value is not None:
|
| 207 |
+
self.retry_state.set_exception((exc_type, exc_value, traceback))
|
| 208 |
+
return True # Swallow exception.
|
| 209 |
+
else:
|
| 210 |
+
# We don't have the result, actually.
|
| 211 |
+
self.retry_state.set_result(None)
|
| 212 |
+
return None
|
| 213 |
+
|
| 214 |
+
|
| 215 |
+
class BaseRetrying(ABC):
|
| 216 |
+
def __init__(
|
| 217 |
+
self,
|
| 218 |
+
sleep: t.Callable[[t.Union[int, float]], None] = sleep,
|
| 219 |
+
stop: "StopBaseT" = stop_never,
|
| 220 |
+
wait: "WaitBaseT" = wait_none(),
|
| 221 |
+
retry: "RetryBaseT" = retry_if_exception_type(),
|
| 222 |
+
before: t.Callable[["RetryCallState"], None] = before_nothing,
|
| 223 |
+
after: t.Callable[["RetryCallState"], None] = after_nothing,
|
| 224 |
+
before_sleep: t.Optional[t.Callable[["RetryCallState"], None]] = None,
|
| 225 |
+
reraise: bool = False,
|
| 226 |
+
retry_error_cls: t.Type[RetryError] = RetryError,
|
| 227 |
+
retry_error_callback: t.Optional[t.Callable[["RetryCallState"], t.Any]] = None,
|
| 228 |
+
):
|
| 229 |
+
self.sleep = sleep
|
| 230 |
+
self.stop = stop
|
| 231 |
+
self.wait = wait
|
| 232 |
+
self.retry = retry
|
| 233 |
+
self.before = before
|
| 234 |
+
self.after = after
|
| 235 |
+
self.before_sleep = before_sleep
|
| 236 |
+
self.reraise = reraise
|
| 237 |
+
self._local = threading.local()
|
| 238 |
+
self.retry_error_cls = retry_error_cls
|
| 239 |
+
self.retry_error_callback = retry_error_callback
|
| 240 |
+
|
| 241 |
+
def copy(
|
| 242 |
+
self,
|
| 243 |
+
sleep: t.Union[t.Callable[[t.Union[int, float]], None], object] = _unset,
|
| 244 |
+
stop: t.Union["StopBaseT", object] = _unset,
|
| 245 |
+
wait: t.Union["WaitBaseT", object] = _unset,
|
| 246 |
+
retry: t.Union[retry_base, object] = _unset,
|
| 247 |
+
before: t.Union[t.Callable[["RetryCallState"], None], object] = _unset,
|
| 248 |
+
after: t.Union[t.Callable[["RetryCallState"], None], object] = _unset,
|
| 249 |
+
before_sleep: t.Union[
|
| 250 |
+
t.Optional[t.Callable[["RetryCallState"], None]], object
|
| 251 |
+
] = _unset,
|
| 252 |
+
reraise: t.Union[bool, object] = _unset,
|
| 253 |
+
retry_error_cls: t.Union[t.Type[RetryError], object] = _unset,
|
| 254 |
+
retry_error_callback: t.Union[
|
| 255 |
+
t.Optional[t.Callable[["RetryCallState"], t.Any]], object
|
| 256 |
+
] = _unset,
|
| 257 |
+
) -> "Self":
|
| 258 |
+
"""Copy this object with some parameters changed if needed."""
|
| 259 |
+
return self.__class__(
|
| 260 |
+
sleep=_first_set(sleep, self.sleep),
|
| 261 |
+
stop=_first_set(stop, self.stop),
|
| 262 |
+
wait=_first_set(wait, self.wait),
|
| 263 |
+
retry=_first_set(retry, self.retry),
|
| 264 |
+
before=_first_set(before, self.before),
|
| 265 |
+
after=_first_set(after, self.after),
|
| 266 |
+
before_sleep=_first_set(before_sleep, self.before_sleep),
|
| 267 |
+
reraise=_first_set(reraise, self.reraise),
|
| 268 |
+
retry_error_cls=_first_set(retry_error_cls, self.retry_error_cls),
|
| 269 |
+
retry_error_callback=_first_set(
|
| 270 |
+
retry_error_callback, self.retry_error_callback
|
| 271 |
+
),
|
| 272 |
+
)
|
| 273 |
+
|
| 274 |
+
def __repr__(self) -> str:
|
| 275 |
+
return (
|
| 276 |
+
f"<{self.__class__.__name__} object at 0x{id(self):x} ("
|
| 277 |
+
f"stop={self.stop}, "
|
| 278 |
+
f"wait={self.wait}, "
|
| 279 |
+
f"sleep={self.sleep}, "
|
| 280 |
+
f"retry={self.retry}, "
|
| 281 |
+
f"before={self.before}, "
|
| 282 |
+
f"after={self.after})>"
|
| 283 |
+
)
|
| 284 |
+
|
| 285 |
+
@property
|
| 286 |
+
def statistics(self) -> t.Dict[str, t.Any]:
|
| 287 |
+
"""Return a dictionary of runtime statistics.
|
| 288 |
+
|
| 289 |
+
This dictionary will be empty when the controller has never been
|
| 290 |
+
ran. When it is running or has ran previously it should have (but
|
| 291 |
+
may not) have useful and/or informational keys and values when
|
| 292 |
+
running is underway and/or completed.
|
| 293 |
+
|
| 294 |
+
.. warning:: The keys in this dictionary **should** be some what
|
| 295 |
+
stable (not changing), but there existence **may**
|
| 296 |
+
change between major releases as new statistics are
|
| 297 |
+
gathered or removed so before accessing keys ensure that
|
| 298 |
+
they actually exist and handle when they do not.
|
| 299 |
+
|
| 300 |
+
.. note:: The values in this dictionary are local to the thread
|
| 301 |
+
running call (so if multiple threads share the same retrying
|
| 302 |
+
object - either directly or indirectly) they will each have
|
| 303 |
+
there own view of statistics they have collected (in the
|
| 304 |
+
future we may provide a way to aggregate the various
|
| 305 |
+
statistics from each thread).
|
| 306 |
+
"""
|
| 307 |
+
if not hasattr(self._local, "statistics"):
|
| 308 |
+
self._local.statistics = t.cast(t.Dict[str, t.Any], {})
|
| 309 |
+
return self._local.statistics # type: ignore[no-any-return]
|
| 310 |
+
|
| 311 |
+
@property
|
| 312 |
+
def iter_state(self) -> IterState:
|
| 313 |
+
if not hasattr(self._local, "iter_state"):
|
| 314 |
+
self._local.iter_state = IterState()
|
| 315 |
+
return self._local.iter_state # type: ignore[no-any-return]
|
| 316 |
+
|
| 317 |
+
def wraps(self, f: WrappedFn) -> WrappedFn:
|
| 318 |
+
"""Wrap a function for retrying.
|
| 319 |
+
|
| 320 |
+
:param f: A function to wraps for retrying.
|
| 321 |
+
"""
|
| 322 |
+
|
| 323 |
+
@functools.wraps(
|
| 324 |
+
f, functools.WRAPPER_ASSIGNMENTS + ("__defaults__", "__kwdefaults__")
|
| 325 |
+
)
|
| 326 |
+
def wrapped_f(*args: t.Any, **kw: t.Any) -> t.Any:
|
| 327 |
+
# Always create a copy to prevent overwriting the local contexts when
|
| 328 |
+
# calling the same wrapped functions multiple times in the same stack
|
| 329 |
+
copy = self.copy()
|
| 330 |
+
wrapped_f.statistics = copy.statistics # type: ignore[attr-defined]
|
| 331 |
+
return copy(f, *args, **kw)
|
| 332 |
+
|
| 333 |
+
def retry_with(*args: t.Any, **kwargs: t.Any) -> WrappedFn:
|
| 334 |
+
return self.copy(*args, **kwargs).wraps(f)
|
| 335 |
+
|
| 336 |
+
# Preserve attributes
|
| 337 |
+
wrapped_f.retry = self # type: ignore[attr-defined]
|
| 338 |
+
wrapped_f.retry_with = retry_with # type: ignore[attr-defined]
|
| 339 |
+
wrapped_f.statistics = {} # type: ignore[attr-defined]
|
| 340 |
+
|
| 341 |
+
return wrapped_f # type: ignore[return-value]
|
| 342 |
+
|
| 343 |
+
def begin(self) -> None:
|
| 344 |
+
self.statistics.clear()
|
| 345 |
+
self.statistics["start_time"] = time.monotonic()
|
| 346 |
+
self.statistics["attempt_number"] = 1
|
| 347 |
+
self.statistics["idle_for"] = 0
|
| 348 |
+
|
| 349 |
+
def _add_action_func(self, fn: t.Callable[..., t.Any]) -> None:
|
| 350 |
+
self.iter_state.actions.append(fn)
|
| 351 |
+
|
| 352 |
+
def _run_retry(self, retry_state: "RetryCallState") -> None:
|
| 353 |
+
self.iter_state.retry_run_result = self.retry(retry_state)
|
| 354 |
+
|
| 355 |
+
def _run_wait(self, retry_state: "RetryCallState") -> None:
|
| 356 |
+
if self.wait:
|
| 357 |
+
sleep = self.wait(retry_state)
|
| 358 |
+
else:
|
| 359 |
+
sleep = 0.0
|
| 360 |
+
|
| 361 |
+
retry_state.upcoming_sleep = sleep
|
| 362 |
+
|
| 363 |
+
def _run_stop(self, retry_state: "RetryCallState") -> None:
|
| 364 |
+
self.statistics["delay_since_first_attempt"] = retry_state.seconds_since_start
|
| 365 |
+
self.iter_state.stop_run_result = self.stop(retry_state)
|
| 366 |
+
|
| 367 |
+
def iter(self, retry_state: "RetryCallState") -> t.Union[DoAttempt, DoSleep, t.Any]: # noqa
|
| 368 |
+
self._begin_iter(retry_state)
|
| 369 |
+
result = None
|
| 370 |
+
for action in self.iter_state.actions:
|
| 371 |
+
result = action(retry_state)
|
| 372 |
+
return result
|
| 373 |
+
|
| 374 |
+
def _begin_iter(self, retry_state: "RetryCallState") -> None: # noqa
|
| 375 |
+
self.iter_state.reset()
|
| 376 |
+
|
| 377 |
+
fut = retry_state.outcome
|
| 378 |
+
if fut is None:
|
| 379 |
+
if self.before is not None:
|
| 380 |
+
self._add_action_func(self.before)
|
| 381 |
+
self._add_action_func(lambda rs: DoAttempt())
|
| 382 |
+
return
|
| 383 |
+
|
| 384 |
+
self.iter_state.is_explicit_retry = fut.failed and isinstance(
|
| 385 |
+
fut.exception(), TryAgain
|
| 386 |
+
)
|
| 387 |
+
if not self.iter_state.is_explicit_retry:
|
| 388 |
+
self._add_action_func(self._run_retry)
|
| 389 |
+
self._add_action_func(self._post_retry_check_actions)
|
| 390 |
+
|
| 391 |
+
def _post_retry_check_actions(self, retry_state: "RetryCallState") -> None:
|
| 392 |
+
if not (self.iter_state.is_explicit_retry or self.iter_state.retry_run_result):
|
| 393 |
+
self._add_action_func(lambda rs: rs.outcome.result())
|
| 394 |
+
return
|
| 395 |
+
|
| 396 |
+
if self.after is not None:
|
| 397 |
+
self._add_action_func(self.after)
|
| 398 |
+
|
| 399 |
+
self._add_action_func(self._run_wait)
|
| 400 |
+
self._add_action_func(self._run_stop)
|
| 401 |
+
self._add_action_func(self._post_stop_check_actions)
|
| 402 |
+
|
| 403 |
+
def _post_stop_check_actions(self, retry_state: "RetryCallState") -> None:
|
| 404 |
+
if self.iter_state.stop_run_result:
|
| 405 |
+
if self.retry_error_callback:
|
| 406 |
+
self._add_action_func(self.retry_error_callback)
|
| 407 |
+
return
|
| 408 |
+
|
| 409 |
+
def exc_check(rs: "RetryCallState") -> None:
|
| 410 |
+
fut = t.cast(Future, rs.outcome)
|
| 411 |
+
retry_exc = self.retry_error_cls(fut)
|
| 412 |
+
if self.reraise:
|
| 413 |
+
raise retry_exc.reraise()
|
| 414 |
+
raise retry_exc from fut.exception()
|
| 415 |
+
|
| 416 |
+
self._add_action_func(exc_check)
|
| 417 |
+
return
|
| 418 |
+
|
| 419 |
+
def next_action(rs: "RetryCallState") -> None:
|
| 420 |
+
sleep = rs.upcoming_sleep
|
| 421 |
+
rs.next_action = RetryAction(sleep)
|
| 422 |
+
rs.idle_for += sleep
|
| 423 |
+
self.statistics["idle_for"] += sleep
|
| 424 |
+
self.statistics["attempt_number"] += 1
|
| 425 |
+
|
| 426 |
+
self._add_action_func(next_action)
|
| 427 |
+
|
| 428 |
+
if self.before_sleep is not None:
|
| 429 |
+
self._add_action_func(self.before_sleep)
|
| 430 |
+
|
| 431 |
+
self._add_action_func(lambda rs: DoSleep(rs.upcoming_sleep))
|
| 432 |
+
|
| 433 |
+
def __iter__(self) -> t.Generator[AttemptManager, None, None]:
|
| 434 |
+
self.begin()
|
| 435 |
+
|
| 436 |
+
retry_state = RetryCallState(self, fn=None, args=(), kwargs={})
|
| 437 |
+
while True:
|
| 438 |
+
do = self.iter(retry_state=retry_state)
|
| 439 |
+
if isinstance(do, DoAttempt):
|
| 440 |
+
yield AttemptManager(retry_state=retry_state)
|
| 441 |
+
elif isinstance(do, DoSleep):
|
| 442 |
+
retry_state.prepare_for_next_attempt()
|
| 443 |
+
self.sleep(do)
|
| 444 |
+
else:
|
| 445 |
+
break
|
| 446 |
+
|
| 447 |
+
@abstractmethod
|
| 448 |
+
def __call__(
|
| 449 |
+
self,
|
| 450 |
+
fn: t.Callable[..., WrappedFnReturnT],
|
| 451 |
+
*args: t.Any,
|
| 452 |
+
**kwargs: t.Any,
|
| 453 |
+
) -> WrappedFnReturnT:
|
| 454 |
+
pass
|
| 455 |
+
|
| 456 |
+
|
| 457 |
+
class Retrying(BaseRetrying):
|
| 458 |
+
"""Retrying controller."""
|
| 459 |
+
|
| 460 |
+
def __call__(
|
| 461 |
+
self,
|
| 462 |
+
fn: t.Callable[..., WrappedFnReturnT],
|
| 463 |
+
*args: t.Any,
|
| 464 |
+
**kwargs: t.Any,
|
| 465 |
+
) -> WrappedFnReturnT:
|
| 466 |
+
self.begin()
|
| 467 |
+
|
| 468 |
+
retry_state = RetryCallState(retry_object=self, fn=fn, args=args, kwargs=kwargs)
|
| 469 |
+
while True:
|
| 470 |
+
do = self.iter(retry_state=retry_state)
|
| 471 |
+
if isinstance(do, DoAttempt):
|
| 472 |
+
try:
|
| 473 |
+
result = fn(*args, **kwargs)
|
| 474 |
+
except BaseException: # noqa: B902
|
| 475 |
+
retry_state.set_exception(sys.exc_info()) # type: ignore[arg-type]
|
| 476 |
+
else:
|
| 477 |
+
retry_state.set_result(result)
|
| 478 |
+
elif isinstance(do, DoSleep):
|
| 479 |
+
retry_state.prepare_for_next_attempt()
|
| 480 |
+
self.sleep(do)
|
| 481 |
+
else:
|
| 482 |
+
return do # type: ignore[no-any-return]
|
| 483 |
+
|
| 484 |
+
|
| 485 |
+
class Future(futures.Future[t.Any]):
|
| 486 |
+
"""Encapsulates a (future or past) attempted call to a target function."""
|
| 487 |
+
|
| 488 |
+
def __init__(self, attempt_number: int) -> None:
|
| 489 |
+
super().__init__()
|
| 490 |
+
self.attempt_number = attempt_number
|
| 491 |
+
|
| 492 |
+
@property
|
| 493 |
+
def failed(self) -> bool:
|
| 494 |
+
"""Return whether a exception is being held in this future."""
|
| 495 |
+
return self.exception() is not None
|
| 496 |
+
|
| 497 |
+
@classmethod
|
| 498 |
+
def construct(
|
| 499 |
+
cls, attempt_number: int, value: t.Any, has_exception: bool
|
| 500 |
+
) -> "Future":
|
| 501 |
+
"""Construct a new Future object."""
|
| 502 |
+
fut = cls(attempt_number)
|
| 503 |
+
if has_exception:
|
| 504 |
+
fut.set_exception(value)
|
| 505 |
+
else:
|
| 506 |
+
fut.set_result(value)
|
| 507 |
+
return fut
|
| 508 |
+
|
| 509 |
+
|
| 510 |
+
class RetryCallState:
|
| 511 |
+
"""State related to a single call wrapped with Retrying."""
|
| 512 |
+
|
| 513 |
+
def __init__(
|
| 514 |
+
self,
|
| 515 |
+
retry_object: BaseRetrying,
|
| 516 |
+
fn: t.Optional[WrappedFn],
|
| 517 |
+
args: t.Any,
|
| 518 |
+
kwargs: t.Any,
|
| 519 |
+
) -> None:
|
| 520 |
+
#: Retry call start timestamp
|
| 521 |
+
self.start_time = time.monotonic()
|
| 522 |
+
#: Retry manager object
|
| 523 |
+
self.retry_object = retry_object
|
| 524 |
+
#: Function wrapped by this retry call
|
| 525 |
+
self.fn = fn
|
| 526 |
+
#: Arguments of the function wrapped by this retry call
|
| 527 |
+
self.args = args
|
| 528 |
+
#: Keyword arguments of the function wrapped by this retry call
|
| 529 |
+
self.kwargs = kwargs
|
| 530 |
+
|
| 531 |
+
#: The number of the current attempt
|
| 532 |
+
self.attempt_number: int = 1
|
| 533 |
+
#: Last outcome (result or exception) produced by the function
|
| 534 |
+
self.outcome: t.Optional[Future] = None
|
| 535 |
+
#: Timestamp of the last outcome
|
| 536 |
+
self.outcome_timestamp: t.Optional[float] = None
|
| 537 |
+
#: Time spent sleeping in retries
|
| 538 |
+
self.idle_for: float = 0.0
|
| 539 |
+
#: Next action as decided by the retry manager
|
| 540 |
+
self.next_action: t.Optional[RetryAction] = None
|
| 541 |
+
#: Next sleep time as decided by the retry manager.
|
| 542 |
+
self.upcoming_sleep: float = 0.0
|
| 543 |
+
|
| 544 |
+
@property
|
| 545 |
+
def seconds_since_start(self) -> t.Optional[float]:
|
| 546 |
+
if self.outcome_timestamp is None:
|
| 547 |
+
return None
|
| 548 |
+
return self.outcome_timestamp - self.start_time
|
| 549 |
+
|
| 550 |
+
def prepare_for_next_attempt(self) -> None:
|
| 551 |
+
self.outcome = None
|
| 552 |
+
self.outcome_timestamp = None
|
| 553 |
+
self.attempt_number += 1
|
| 554 |
+
self.next_action = None
|
| 555 |
+
|
| 556 |
+
def set_result(self, val: t.Any) -> None:
|
| 557 |
+
ts = time.monotonic()
|
| 558 |
+
fut = Future(self.attempt_number)
|
| 559 |
+
fut.set_result(val)
|
| 560 |
+
self.outcome, self.outcome_timestamp = fut, ts
|
| 561 |
+
|
| 562 |
+
def set_exception(
|
| 563 |
+
self,
|
| 564 |
+
exc_info: t.Tuple[
|
| 565 |
+
t.Type[BaseException], BaseException, "types.TracebackType| None"
|
| 566 |
+
],
|
| 567 |
+
) -> None:
|
| 568 |
+
ts = time.monotonic()
|
| 569 |
+
fut = Future(self.attempt_number)
|
| 570 |
+
fut.set_exception(exc_info[1])
|
| 571 |
+
self.outcome, self.outcome_timestamp = fut, ts
|
| 572 |
+
|
| 573 |
+
def __repr__(self) -> str:
|
| 574 |
+
if self.outcome is None:
|
| 575 |
+
result = "none yet"
|
| 576 |
+
elif self.outcome.failed:
|
| 577 |
+
exception = self.outcome.exception()
|
| 578 |
+
result = f"failed ({exception.__class__.__name__} {exception})"
|
| 579 |
+
else:
|
| 580 |
+
result = f"returned {self.outcome.result()}"
|
| 581 |
+
|
| 582 |
+
slept = float(round(self.idle_for, 2))
|
| 583 |
+
clsname = self.__class__.__name__
|
| 584 |
+
return f"<{clsname} {id(self)}: attempt #{self.attempt_number}; slept for {slept}; last result: {result}>"
|
| 585 |
+
|
| 586 |
+
|
| 587 |
+
class _AsyncRetryDecorator(t.Protocol):
|
| 588 |
+
@t.overload
|
| 589 |
+
def __call__(
|
| 590 |
+
self, fn: "t.Callable[P, types.CoroutineType[t.Any, t.Any, R]]"
|
| 591 |
+
) -> "t.Callable[P, types.CoroutineType[t.Any, t.Any, R]]": ...
|
| 592 |
+
@t.overload
|
| 593 |
+
def __call__(
|
| 594 |
+
self, fn: t.Callable[P, t.Coroutine[t.Any, t.Any, R]]
|
| 595 |
+
) -> t.Callable[P, t.Coroutine[t.Any, t.Any, R]]: ...
|
| 596 |
+
@t.overload
|
| 597 |
+
def __call__(
|
| 598 |
+
self, fn: t.Callable[P, t.Awaitable[R]]
|
| 599 |
+
) -> t.Callable[P, t.Awaitable[R]]: ...
|
| 600 |
+
@t.overload
|
| 601 |
+
def __call__(self, fn: t.Callable[P, R]) -> t.Callable[P, t.Awaitable[R]]: ...
|
| 602 |
+
|
| 603 |
+
|
| 604 |
+
@t.overload
|
| 605 |
+
def retry(func: WrappedFn) -> WrappedFn: ...
|
| 606 |
+
|
| 607 |
+
|
| 608 |
+
@t.overload
|
| 609 |
+
def retry(
|
| 610 |
+
*,
|
| 611 |
+
sleep: t.Callable[[t.Union[int, float]], t.Awaitable[None]],
|
| 612 |
+
stop: "StopBaseT" = ...,
|
| 613 |
+
wait: "WaitBaseT" = ...,
|
| 614 |
+
retry: "t.Union[RetryBaseT, tasyncio.retry.RetryBaseT]" = ...,
|
| 615 |
+
before: t.Callable[["RetryCallState"], t.Union[None, t.Awaitable[None]]] = ...,
|
| 616 |
+
after: t.Callable[["RetryCallState"], t.Union[None, t.Awaitable[None]]] = ...,
|
| 617 |
+
before_sleep: t.Optional[
|
| 618 |
+
t.Callable[["RetryCallState"], t.Union[None, t.Awaitable[None]]]
|
| 619 |
+
] = ...,
|
| 620 |
+
reraise: bool = ...,
|
| 621 |
+
retry_error_cls: t.Type["RetryError"] = ...,
|
| 622 |
+
retry_error_callback: t.Optional[
|
| 623 |
+
t.Callable[["RetryCallState"], t.Union[t.Any, t.Awaitable[t.Any]]]
|
| 624 |
+
] = ...,
|
| 625 |
+
) -> _AsyncRetryDecorator: ...
|
| 626 |
+
|
| 627 |
+
|
| 628 |
+
@t.overload
|
| 629 |
+
def retry(
|
| 630 |
+
sleep: t.Callable[[t.Union[int, float]], None] = sleep,
|
| 631 |
+
stop: "StopBaseT" = stop_never,
|
| 632 |
+
wait: "WaitBaseT" = wait_none(),
|
| 633 |
+
retry: "t.Union[RetryBaseT, tasyncio.retry.RetryBaseT]" = retry_if_exception_type(),
|
| 634 |
+
before: t.Callable[
|
| 635 |
+
["RetryCallState"], t.Union[None, t.Awaitable[None]]
|
| 636 |
+
] = before_nothing,
|
| 637 |
+
after: t.Callable[
|
| 638 |
+
["RetryCallState"], t.Union[None, t.Awaitable[None]]
|
| 639 |
+
] = after_nothing,
|
| 640 |
+
before_sleep: t.Optional[
|
| 641 |
+
t.Callable[["RetryCallState"], t.Union[None, t.Awaitable[None]]]
|
| 642 |
+
] = None,
|
| 643 |
+
reraise: bool = False,
|
| 644 |
+
retry_error_cls: t.Type["RetryError"] = RetryError,
|
| 645 |
+
retry_error_callback: t.Optional[
|
| 646 |
+
t.Callable[["RetryCallState"], t.Union[t.Any, t.Awaitable[t.Any]]]
|
| 647 |
+
] = None,
|
| 648 |
+
) -> t.Callable[[WrappedFn], WrappedFn]: ...
|
| 649 |
+
|
| 650 |
+
|
| 651 |
+
def retry(*dargs: t.Any, **dkw: t.Any) -> t.Any:
|
| 652 |
+
"""Wrap a function with a new `Retrying` object.
|
| 653 |
+
|
| 654 |
+
:param dargs: positional arguments passed to Retrying object
|
| 655 |
+
:param dkw: keyword arguments passed to the Retrying object
|
| 656 |
+
"""
|
| 657 |
+
# support both @retry and @retry() as valid syntax
|
| 658 |
+
if len(dargs) == 1 and callable(dargs[0]):
|
| 659 |
+
return retry()(dargs[0])
|
| 660 |
+
else:
|
| 661 |
+
|
| 662 |
+
def wrap(f: WrappedFn) -> WrappedFn:
|
| 663 |
+
if isinstance(f, retry_base):
|
| 664 |
+
warnings.warn(
|
| 665 |
+
f"Got retry_base instance ({f.__class__.__name__}) as callable argument, "
|
| 666 |
+
f"this will probably hang indefinitely (did you mean retry={f.__class__.__name__}(...)?)"
|
| 667 |
+
)
|
| 668 |
+
r: "BaseRetrying"
|
| 669 |
+
sleep = dkw.get("sleep")
|
| 670 |
+
if _utils.is_coroutine_callable(f) or (
|
| 671 |
+
sleep is not None and _utils.is_coroutine_callable(sleep)
|
| 672 |
+
):
|
| 673 |
+
r = AsyncRetrying(*dargs, **dkw)
|
| 674 |
+
elif (
|
| 675 |
+
tornado
|
| 676 |
+
and hasattr(tornado.gen, "is_coroutine_function")
|
| 677 |
+
and tornado.gen.is_coroutine_function(f)
|
| 678 |
+
):
|
| 679 |
+
r = TornadoRetrying(*dargs, **dkw)
|
| 680 |
+
else:
|
| 681 |
+
r = Retrying(*dargs, **dkw)
|
| 682 |
+
|
| 683 |
+
return r.wraps(f)
|
| 684 |
+
|
| 685 |
+
return wrap
|
| 686 |
+
|
| 687 |
+
|
| 688 |
+
from tenacity.asyncio import AsyncRetrying # noqa:E402,I100
|
| 689 |
+
|
| 690 |
+
if tornado:
|
| 691 |
+
from tenacity.tornadoweb import TornadoRetrying
|
| 692 |
+
|
| 693 |
+
|
| 694 |
+
__all__ = [
|
| 695 |
+
"retry_base",
|
| 696 |
+
"retry_all",
|
| 697 |
+
"retry_always",
|
| 698 |
+
"retry_any",
|
| 699 |
+
"retry_if_exception",
|
| 700 |
+
"retry_if_exception_type",
|
| 701 |
+
"retry_if_exception_cause_type",
|
| 702 |
+
"retry_if_not_exception_type",
|
| 703 |
+
"retry_if_not_result",
|
| 704 |
+
"retry_if_result",
|
| 705 |
+
"retry_never",
|
| 706 |
+
"retry_unless_exception_type",
|
| 707 |
+
"retry_if_exception_message",
|
| 708 |
+
"retry_if_not_exception_message",
|
| 709 |
+
"sleep",
|
| 710 |
+
"sleep_using_event",
|
| 711 |
+
"stop_after_attempt",
|
| 712 |
+
"stop_after_delay",
|
| 713 |
+
"stop_before_delay",
|
| 714 |
+
"stop_all",
|
| 715 |
+
"stop_any",
|
| 716 |
+
"stop_never",
|
| 717 |
+
"stop_when_event_set",
|
| 718 |
+
"wait_chain",
|
| 719 |
+
"wait_combine",
|
| 720 |
+
"wait_exception",
|
| 721 |
+
"wait_exponential",
|
| 722 |
+
"wait_fixed",
|
| 723 |
+
"wait_incrementing",
|
| 724 |
+
"wait_none",
|
| 725 |
+
"wait_random",
|
| 726 |
+
"wait_random_exponential",
|
| 727 |
+
"wait_full_jitter",
|
| 728 |
+
"wait_exponential_jitter",
|
| 729 |
+
"before_log",
|
| 730 |
+
"before_nothing",
|
| 731 |
+
"after_log",
|
| 732 |
+
"after_nothing",
|
| 733 |
+
"before_sleep_log",
|
| 734 |
+
"before_sleep_nothing",
|
| 735 |
+
"retry",
|
| 736 |
+
"WrappedFn",
|
| 737 |
+
"TryAgain",
|
| 738 |
+
"NO_RESULT",
|
| 739 |
+
"DoAttempt",
|
| 740 |
+
"DoSleep",
|
| 741 |
+
"BaseAction",
|
| 742 |
+
"RetryAction",
|
| 743 |
+
"RetryError",
|
| 744 |
+
"AttemptManager",
|
| 745 |
+
"BaseRetrying",
|
| 746 |
+
"Retrying",
|
| 747 |
+
"Future",
|
| 748 |
+
"RetryCallState",
|
| 749 |
+
"AsyncRetrying",
|
| 750 |
+
]
|
loan-advisor-agent/.venv/Lib/site-packages/tenacity/__pycache__/__init__.cpython-312.pyc
ADDED
|
Binary file (36 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/tenacity/__pycache__/_utils.cpython-312.pyc
ADDED
|
Binary file (4.42 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/tenacity/__pycache__/after.cpython-312.pyc
ADDED
|
Binary file (1.61 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/tenacity/__pycache__/before.cpython-312.pyc
ADDED
|
Binary file (1.45 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/tenacity/__pycache__/before_sleep.cpython-312.pyc
ADDED
|
Binary file (2.37 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/tenacity/__pycache__/nap.cpython-312.pyc
ADDED
|
Binary file (1.41 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/tenacity/__pycache__/retry.cpython-312.pyc
ADDED
|
Binary file (14.7 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/tenacity/__pycache__/stop.cpython-312.pyc
ADDED
|
Binary file (6.86 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/tenacity/__pycache__/wait.cpython-312.pyc
ADDED
|
Binary file (14.2 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/tenacity/_utils.py
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2016 Julien Danjou
|
| 2 |
+
# Copyright 2016 Joshua Harlow
|
| 3 |
+
# Copyright 2013-2014 Ray Holder
|
| 4 |
+
#
|
| 5 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 6 |
+
# you may not use this file except in compliance with the License.
|
| 7 |
+
# You may obtain a copy of the License at
|
| 8 |
+
#
|
| 9 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 10 |
+
#
|
| 11 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 12 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 13 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 14 |
+
# See the License for the specific language governing permissions and
|
| 15 |
+
# limitations under the License.
|
| 16 |
+
import functools
|
| 17 |
+
import inspect
|
| 18 |
+
import sys
|
| 19 |
+
import typing
|
| 20 |
+
from datetime import timedelta
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
# sys.maxsize:
|
| 24 |
+
# An integer giving the maximum value a variable of type Py_ssize_t can take.
|
| 25 |
+
MAX_WAIT = sys.maxsize / 2
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
class LoggerProtocol(typing.Protocol):
|
| 29 |
+
"""
|
| 30 |
+
Protocol used by utils expecting a logger (eg: before_log).
|
| 31 |
+
|
| 32 |
+
Compatible with logging, structlog, loguru, etc...
|
| 33 |
+
"""
|
| 34 |
+
|
| 35 |
+
def log(
|
| 36 |
+
self, level: int, msg: str, /, *args: typing.Any, **kwargs: typing.Any
|
| 37 |
+
) -> typing.Any: ...
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def find_ordinal(pos_num: int) -> str:
|
| 41 |
+
# See: https://en.wikipedia.org/wiki/English_numerals#Ordinal_numbers
|
| 42 |
+
if pos_num == 0:
|
| 43 |
+
return "th"
|
| 44 |
+
elif pos_num == 1:
|
| 45 |
+
return "st"
|
| 46 |
+
elif pos_num == 2:
|
| 47 |
+
return "nd"
|
| 48 |
+
elif pos_num == 3:
|
| 49 |
+
return "rd"
|
| 50 |
+
elif 4 <= pos_num <= 20:
|
| 51 |
+
return "th"
|
| 52 |
+
else:
|
| 53 |
+
return find_ordinal(pos_num % 10)
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def to_ordinal(pos_num: int) -> str:
|
| 57 |
+
return f"{pos_num}{find_ordinal(pos_num)}"
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def get_callback_name(cb: typing.Callable[..., typing.Any]) -> str:
|
| 61 |
+
"""Get a callback fully-qualified name.
|
| 62 |
+
|
| 63 |
+
If no name can be produced ``repr(cb)`` is called and returned.
|
| 64 |
+
"""
|
| 65 |
+
segments = []
|
| 66 |
+
try:
|
| 67 |
+
segments.append(cb.__qualname__)
|
| 68 |
+
except AttributeError:
|
| 69 |
+
try:
|
| 70 |
+
segments.append(cb.__name__)
|
| 71 |
+
except AttributeError:
|
| 72 |
+
pass
|
| 73 |
+
if not segments:
|
| 74 |
+
return repr(cb)
|
| 75 |
+
else:
|
| 76 |
+
try:
|
| 77 |
+
# When running under sphinx it appears this can be none?
|
| 78 |
+
if cb.__module__:
|
| 79 |
+
segments.insert(0, cb.__module__)
|
| 80 |
+
except AttributeError:
|
| 81 |
+
pass
|
| 82 |
+
return ".".join(segments)
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
time_unit_type = typing.Union[int, float, timedelta]
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
def to_seconds(time_unit: time_unit_type) -> float:
|
| 89 |
+
return float(
|
| 90 |
+
time_unit.total_seconds() if isinstance(time_unit, timedelta) else time_unit
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
def is_coroutine_callable(call: typing.Callable[..., typing.Any]) -> bool:
|
| 95 |
+
if inspect.isclass(call):
|
| 96 |
+
return False
|
| 97 |
+
if inspect.iscoroutinefunction(call):
|
| 98 |
+
return True
|
| 99 |
+
partial_call = isinstance(call, functools.partial) and call.func
|
| 100 |
+
dunder_call = partial_call or getattr(call, "__call__", None)
|
| 101 |
+
return inspect.iscoroutinefunction(dunder_call)
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
def wrap_to_async_func(
|
| 105 |
+
call: typing.Callable[..., typing.Any],
|
| 106 |
+
) -> typing.Callable[..., typing.Awaitable[typing.Any]]:
|
| 107 |
+
if is_coroutine_callable(call):
|
| 108 |
+
return call
|
| 109 |
+
|
| 110 |
+
async def inner(*args: typing.Any, **kwargs: typing.Any) -> typing.Any:
|
| 111 |
+
return call(*args, **kwargs)
|
| 112 |
+
|
| 113 |
+
return inner
|
loan-advisor-agent/.venv/Lib/site-packages/tenacity/after.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2016 Julien Danjou
|
| 2 |
+
# Copyright 2016 Joshua Harlow
|
| 3 |
+
# Copyright 2013-2014 Ray Holder
|
| 4 |
+
#
|
| 5 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 6 |
+
# you may not use this file except in compliance with the License.
|
| 7 |
+
# You may obtain a copy of the License at
|
| 8 |
+
#
|
| 9 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 10 |
+
#
|
| 11 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 12 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 13 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 14 |
+
# See the License for the specific language governing permissions and
|
| 15 |
+
# limitations under the License.
|
| 16 |
+
|
| 17 |
+
import typing
|
| 18 |
+
|
| 19 |
+
from tenacity import _utils
|
| 20 |
+
|
| 21 |
+
if typing.TYPE_CHECKING:
|
| 22 |
+
from tenacity import RetryCallState
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def after_nothing(retry_state: "RetryCallState") -> None:
|
| 26 |
+
"""After call strategy that does nothing."""
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def after_log(
|
| 30 |
+
logger: _utils.LoggerProtocol,
|
| 31 |
+
log_level: int,
|
| 32 |
+
sec_format: str = "%.3g",
|
| 33 |
+
) -> typing.Callable[["RetryCallState"], None]:
|
| 34 |
+
"""After call strategy that logs to some logger the finished attempt."""
|
| 35 |
+
|
| 36 |
+
def log_it(retry_state: "RetryCallState") -> None:
|
| 37 |
+
if retry_state.fn is None:
|
| 38 |
+
# NOTE(sileht): can't really happen, but we must please mypy
|
| 39 |
+
fn_name = "<unknown>"
|
| 40 |
+
else:
|
| 41 |
+
fn_name = _utils.get_callback_name(retry_state.fn)
|
| 42 |
+
logger.log(
|
| 43 |
+
log_level,
|
| 44 |
+
f"Finished call to '{fn_name}' "
|
| 45 |
+
f"after {sec_format % retry_state.seconds_since_start}(s), "
|
| 46 |
+
f"this was the {_utils.to_ordinal(retry_state.attempt_number)} time calling it.",
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
return log_it
|
loan-advisor-agent/.venv/Lib/site-packages/tenacity/asyncio/__init__.py
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2016 Étienne Bersac
|
| 2 |
+
# Copyright 2016 Julien Danjou
|
| 3 |
+
# Copyright 2016 Joshua Harlow
|
| 4 |
+
# Copyright 2013-2014 Ray Holder
|
| 5 |
+
#
|
| 6 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 7 |
+
# you may not use this file except in compliance with the License.
|
| 8 |
+
# You may obtain a copy of the License at
|
| 9 |
+
#
|
| 10 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 11 |
+
#
|
| 12 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 13 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 14 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 15 |
+
# See the License for the specific language governing permissions and
|
| 16 |
+
# limitations under the License.
|
| 17 |
+
|
| 18 |
+
import functools
|
| 19 |
+
import sys
|
| 20 |
+
import typing as t
|
| 21 |
+
|
| 22 |
+
import tenacity
|
| 23 |
+
from tenacity import AttemptManager
|
| 24 |
+
from tenacity import BaseRetrying
|
| 25 |
+
from tenacity import DoAttempt
|
| 26 |
+
from tenacity import DoSleep
|
| 27 |
+
from tenacity import RetryCallState
|
| 28 |
+
from tenacity import RetryError
|
| 29 |
+
from tenacity import after_nothing
|
| 30 |
+
from tenacity import before_nothing
|
| 31 |
+
from tenacity import _utils
|
| 32 |
+
|
| 33 |
+
# Import all built-in retry strategies for easier usage.
|
| 34 |
+
from .retry import RetryBaseT
|
| 35 |
+
from .retry import retry_all # noqa
|
| 36 |
+
from .retry import retry_any # noqa
|
| 37 |
+
from .retry import retry_if_exception # noqa
|
| 38 |
+
from .retry import retry_if_result # noqa
|
| 39 |
+
from ..retry import RetryBaseT as SyncRetryBaseT
|
| 40 |
+
|
| 41 |
+
if t.TYPE_CHECKING:
|
| 42 |
+
from tenacity.stop import StopBaseT
|
| 43 |
+
from tenacity.wait import WaitBaseT
|
| 44 |
+
|
| 45 |
+
WrappedFnReturnT = t.TypeVar("WrappedFnReturnT")
|
| 46 |
+
WrappedFn = t.TypeVar("WrappedFn", bound=t.Callable[..., t.Awaitable[t.Any]])
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def _portable_async_sleep(seconds: float) -> t.Awaitable[None]:
|
| 50 |
+
# If trio is already imported, then importing it is cheap.
|
| 51 |
+
# If trio isn't already imported, then it's definitely not running, so we
|
| 52 |
+
# can skip further checks.
|
| 53 |
+
if "trio" in sys.modules:
|
| 54 |
+
# If trio is available, then sniffio is too
|
| 55 |
+
import trio
|
| 56 |
+
import sniffio
|
| 57 |
+
|
| 58 |
+
if sniffio.current_async_library() == "trio":
|
| 59 |
+
return trio.sleep(seconds)
|
| 60 |
+
# Otherwise, assume asyncio
|
| 61 |
+
# Lazy import asyncio as it's expensive (responsible for 25-50% of total import overhead).
|
| 62 |
+
import asyncio
|
| 63 |
+
|
| 64 |
+
return asyncio.sleep(seconds)
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
class AsyncRetrying(BaseRetrying):
|
| 68 |
+
def __init__(
|
| 69 |
+
self,
|
| 70 |
+
sleep: t.Callable[
|
| 71 |
+
[t.Union[int, float]], t.Union[None, t.Awaitable[None]]
|
| 72 |
+
] = _portable_async_sleep,
|
| 73 |
+
stop: "StopBaseT" = tenacity.stop.stop_never,
|
| 74 |
+
wait: "WaitBaseT" = tenacity.wait.wait_none(),
|
| 75 |
+
retry: "t.Union[SyncRetryBaseT, RetryBaseT]" = tenacity.retry_if_exception_type(),
|
| 76 |
+
before: t.Callable[
|
| 77 |
+
["RetryCallState"], t.Union[None, t.Awaitable[None]]
|
| 78 |
+
] = before_nothing,
|
| 79 |
+
after: t.Callable[
|
| 80 |
+
["RetryCallState"], t.Union[None, t.Awaitable[None]]
|
| 81 |
+
] = after_nothing,
|
| 82 |
+
before_sleep: t.Optional[
|
| 83 |
+
t.Callable[["RetryCallState"], t.Union[None, t.Awaitable[None]]]
|
| 84 |
+
] = None,
|
| 85 |
+
reraise: bool = False,
|
| 86 |
+
retry_error_cls: t.Type["RetryError"] = RetryError,
|
| 87 |
+
retry_error_callback: t.Optional[
|
| 88 |
+
t.Callable[["RetryCallState"], t.Union[t.Any, t.Awaitable[t.Any]]]
|
| 89 |
+
] = None,
|
| 90 |
+
) -> None:
|
| 91 |
+
super().__init__(
|
| 92 |
+
sleep=sleep, # type: ignore[arg-type]
|
| 93 |
+
stop=stop,
|
| 94 |
+
wait=wait,
|
| 95 |
+
retry=retry, # type: ignore[arg-type]
|
| 96 |
+
before=before, # type: ignore[arg-type]
|
| 97 |
+
after=after, # type: ignore[arg-type]
|
| 98 |
+
before_sleep=before_sleep, # type: ignore[arg-type]
|
| 99 |
+
reraise=reraise,
|
| 100 |
+
retry_error_cls=retry_error_cls,
|
| 101 |
+
retry_error_callback=retry_error_callback,
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
async def __call__( # type: ignore[override]
|
| 105 |
+
self, fn: WrappedFn, *args: t.Any, **kwargs: t.Any
|
| 106 |
+
) -> WrappedFnReturnT:
|
| 107 |
+
self.begin()
|
| 108 |
+
|
| 109 |
+
retry_state = RetryCallState(retry_object=self, fn=fn, args=args, kwargs=kwargs)
|
| 110 |
+
is_async = _utils.is_coroutine_callable(fn)
|
| 111 |
+
while True:
|
| 112 |
+
do = await self.iter(retry_state=retry_state)
|
| 113 |
+
if isinstance(do, DoAttempt):
|
| 114 |
+
try:
|
| 115 |
+
if is_async:
|
| 116 |
+
result = await fn(*args, **kwargs)
|
| 117 |
+
else:
|
| 118 |
+
result = fn(*args, **kwargs)
|
| 119 |
+
except BaseException: # noqa: B902
|
| 120 |
+
retry_state.set_exception(sys.exc_info()) # type: ignore[arg-type]
|
| 121 |
+
else:
|
| 122 |
+
retry_state.set_result(result)
|
| 123 |
+
elif isinstance(do, DoSleep):
|
| 124 |
+
retry_state.prepare_for_next_attempt()
|
| 125 |
+
await self.sleep(do) # type: ignore[misc]
|
| 126 |
+
else:
|
| 127 |
+
return do # type: ignore[no-any-return]
|
| 128 |
+
|
| 129 |
+
def _add_action_func(self, fn: t.Callable[..., t.Any]) -> None:
|
| 130 |
+
self.iter_state.actions.append(_utils.wrap_to_async_func(fn))
|
| 131 |
+
|
| 132 |
+
async def _run_retry(self, retry_state: "RetryCallState") -> None: # type: ignore[override]
|
| 133 |
+
self.iter_state.retry_run_result = await _utils.wrap_to_async_func(self.retry)(
|
| 134 |
+
retry_state
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
async def _run_wait(self, retry_state: "RetryCallState") -> None: # type: ignore[override]
|
| 138 |
+
if self.wait:
|
| 139 |
+
sleep = await _utils.wrap_to_async_func(self.wait)(retry_state)
|
| 140 |
+
else:
|
| 141 |
+
sleep = 0.0
|
| 142 |
+
|
| 143 |
+
retry_state.upcoming_sleep = sleep
|
| 144 |
+
|
| 145 |
+
async def _run_stop(self, retry_state: "RetryCallState") -> None: # type: ignore[override]
|
| 146 |
+
self.statistics["delay_since_first_attempt"] = retry_state.seconds_since_start
|
| 147 |
+
self.iter_state.stop_run_result = await _utils.wrap_to_async_func(self.stop)(
|
| 148 |
+
retry_state
|
| 149 |
+
)
|
| 150 |
+
|
| 151 |
+
async def iter(
|
| 152 |
+
self, retry_state: "RetryCallState"
|
| 153 |
+
) -> t.Union[DoAttempt, DoSleep, t.Any]: # noqa: A003
|
| 154 |
+
self._begin_iter(retry_state)
|
| 155 |
+
result = None
|
| 156 |
+
for action in self.iter_state.actions:
|
| 157 |
+
result = await action(retry_state)
|
| 158 |
+
return result
|
| 159 |
+
|
| 160 |
+
def __iter__(self) -> t.Generator[AttemptManager, None, None]:
|
| 161 |
+
raise TypeError("AsyncRetrying object is not iterable")
|
| 162 |
+
|
| 163 |
+
def __aiter__(self) -> "AsyncRetrying":
|
| 164 |
+
self.begin()
|
| 165 |
+
self._retry_state = RetryCallState(self, fn=None, args=(), kwargs={})
|
| 166 |
+
return self
|
| 167 |
+
|
| 168 |
+
async def __anext__(self) -> AttemptManager:
|
| 169 |
+
while True:
|
| 170 |
+
do = await self.iter(retry_state=self._retry_state)
|
| 171 |
+
if do is None:
|
| 172 |
+
raise StopAsyncIteration
|
| 173 |
+
elif isinstance(do, DoAttempt):
|
| 174 |
+
return AttemptManager(retry_state=self._retry_state)
|
| 175 |
+
elif isinstance(do, DoSleep):
|
| 176 |
+
self._retry_state.prepare_for_next_attempt()
|
| 177 |
+
await self.sleep(do) # type: ignore[misc]
|
| 178 |
+
else:
|
| 179 |
+
raise StopAsyncIteration
|
| 180 |
+
|
| 181 |
+
def wraps(self, fn: WrappedFn) -> WrappedFn:
|
| 182 |
+
wrapped = super().wraps(fn)
|
| 183 |
+
# Ensure wrapper is recognized as a coroutine function.
|
| 184 |
+
|
| 185 |
+
@functools.wraps(
|
| 186 |
+
fn, functools.WRAPPER_ASSIGNMENTS + ("__defaults__", "__kwdefaults__")
|
| 187 |
+
)
|
| 188 |
+
async def async_wrapped(*args: t.Any, **kwargs: t.Any) -> t.Any:
|
| 189 |
+
# Always create a copy to prevent overwriting the local contexts when
|
| 190 |
+
# calling the same wrapped functions multiple times in the same stack
|
| 191 |
+
copy = self.copy()
|
| 192 |
+
async_wrapped.statistics = copy.statistics # type: ignore[attr-defined]
|
| 193 |
+
return await copy(fn, *args, **kwargs)
|
| 194 |
+
|
| 195 |
+
# Preserve attributes
|
| 196 |
+
async_wrapped.retry = self # type: ignore[attr-defined]
|
| 197 |
+
async_wrapped.retry_with = wrapped.retry_with # type: ignore[attr-defined]
|
| 198 |
+
async_wrapped.statistics = {} # type: ignore[attr-defined]
|
| 199 |
+
|
| 200 |
+
return async_wrapped # type: ignore[return-value]
|
| 201 |
+
|
| 202 |
+
|
| 203 |
+
__all__ = [
|
| 204 |
+
"retry_all",
|
| 205 |
+
"retry_any",
|
| 206 |
+
"retry_if_exception",
|
| 207 |
+
"retry_if_result",
|
| 208 |
+
"WrappedFn",
|
| 209 |
+
"AsyncRetrying",
|
| 210 |
+
]
|
loan-advisor-agent/.venv/Lib/site-packages/tenacity/asyncio/__pycache__/__init__.cpython-312.pyc
ADDED
|
Binary file (9.86 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/tenacity/asyncio/__pycache__/retry.cpython-312.pyc
ADDED
|
Binary file (5.81 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/tenacity/asyncio/retry.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2016–2021 Julien Danjou
|
| 2 |
+
# Copyright 2016 Joshua Harlow
|
| 3 |
+
# Copyright 2013-2014 Ray Holder
|
| 4 |
+
#
|
| 5 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 6 |
+
# you may not use this file except in compliance with the License.
|
| 7 |
+
# You may obtain a copy of the License at
|
| 8 |
+
#
|
| 9 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 10 |
+
#
|
| 11 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 12 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 13 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 14 |
+
# See the License for the specific language governing permissions and
|
| 15 |
+
# limitations under the License.
|
| 16 |
+
import abc
|
| 17 |
+
import typing
|
| 18 |
+
|
| 19 |
+
from tenacity import _utils
|
| 20 |
+
from tenacity import retry_base
|
| 21 |
+
|
| 22 |
+
if typing.TYPE_CHECKING:
|
| 23 |
+
from tenacity import RetryCallState
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
class async_retry_base(retry_base):
|
| 27 |
+
"""Abstract base class for async retry strategies."""
|
| 28 |
+
|
| 29 |
+
@abc.abstractmethod
|
| 30 |
+
async def __call__(self, retry_state: "RetryCallState") -> bool: # type: ignore[override]
|
| 31 |
+
pass
|
| 32 |
+
|
| 33 |
+
def __and__( # type: ignore[override]
|
| 34 |
+
self, other: "typing.Union[retry_base, async_retry_base]"
|
| 35 |
+
) -> "retry_all":
|
| 36 |
+
return retry_all(self, other)
|
| 37 |
+
|
| 38 |
+
def __rand__( # type: ignore[misc,override]
|
| 39 |
+
self, other: "typing.Union[retry_base, async_retry_base]"
|
| 40 |
+
) -> "retry_all":
|
| 41 |
+
return retry_all(other, self)
|
| 42 |
+
|
| 43 |
+
def __or__( # type: ignore[override]
|
| 44 |
+
self, other: "typing.Union[retry_base, async_retry_base]"
|
| 45 |
+
) -> "retry_any":
|
| 46 |
+
return retry_any(self, other)
|
| 47 |
+
|
| 48 |
+
def __ror__( # type: ignore[misc,override]
|
| 49 |
+
self, other: "typing.Union[retry_base, async_retry_base]"
|
| 50 |
+
) -> "retry_any":
|
| 51 |
+
return retry_any(other, self)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
RetryBaseT = typing.Union[
|
| 55 |
+
async_retry_base, typing.Callable[["RetryCallState"], typing.Awaitable[bool]]
|
| 56 |
+
]
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
class retry_if_exception(async_retry_base):
|
| 60 |
+
"""Retry strategy that retries if an exception verifies a predicate."""
|
| 61 |
+
|
| 62 |
+
def __init__(
|
| 63 |
+
self, predicate: typing.Callable[[BaseException], typing.Awaitable[bool]]
|
| 64 |
+
) -> None:
|
| 65 |
+
self.predicate = predicate
|
| 66 |
+
|
| 67 |
+
async def __call__(self, retry_state: "RetryCallState") -> bool: # type: ignore[override]
|
| 68 |
+
if retry_state.outcome is None:
|
| 69 |
+
raise RuntimeError("__call__() called before outcome was set")
|
| 70 |
+
|
| 71 |
+
if retry_state.outcome.failed:
|
| 72 |
+
exception = retry_state.outcome.exception()
|
| 73 |
+
if exception is None:
|
| 74 |
+
raise RuntimeError("outcome failed but the exception is None")
|
| 75 |
+
return await self.predicate(exception)
|
| 76 |
+
else:
|
| 77 |
+
return False
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
class retry_if_result(async_retry_base):
|
| 81 |
+
"""Retries if the result verifies a predicate."""
|
| 82 |
+
|
| 83 |
+
def __init__(
|
| 84 |
+
self, predicate: typing.Callable[[typing.Any], typing.Awaitable[bool]]
|
| 85 |
+
) -> None:
|
| 86 |
+
self.predicate = predicate
|
| 87 |
+
|
| 88 |
+
async def __call__(self, retry_state: "RetryCallState") -> bool: # type: ignore[override]
|
| 89 |
+
if retry_state.outcome is None:
|
| 90 |
+
raise RuntimeError("__call__() called before outcome was set")
|
| 91 |
+
|
| 92 |
+
if not retry_state.outcome.failed:
|
| 93 |
+
return await self.predicate(retry_state.outcome.result())
|
| 94 |
+
else:
|
| 95 |
+
return False
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
class retry_any(async_retry_base):
|
| 99 |
+
"""Retries if any of the retries condition is valid."""
|
| 100 |
+
|
| 101 |
+
def __init__(self, *retries: typing.Union[retry_base, async_retry_base]) -> None:
|
| 102 |
+
self.retries = retries
|
| 103 |
+
|
| 104 |
+
async def __call__(self, retry_state: "RetryCallState") -> bool: # type: ignore[override]
|
| 105 |
+
result = False
|
| 106 |
+
for r in self.retries:
|
| 107 |
+
result = result or await _utils.wrap_to_async_func(r)(retry_state)
|
| 108 |
+
if result:
|
| 109 |
+
break
|
| 110 |
+
return result
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
class retry_all(async_retry_base):
|
| 114 |
+
"""Retries if all the retries condition are valid."""
|
| 115 |
+
|
| 116 |
+
def __init__(self, *retries: typing.Union[retry_base, async_retry_base]) -> None:
|
| 117 |
+
self.retries = retries
|
| 118 |
+
|
| 119 |
+
async def __call__(self, retry_state: "RetryCallState") -> bool: # type: ignore[override]
|
| 120 |
+
result = True
|
| 121 |
+
for r in self.retries:
|
| 122 |
+
result = result and await _utils.wrap_to_async_func(r)(retry_state)
|
| 123 |
+
if not result:
|
| 124 |
+
break
|
| 125 |
+
return result
|
loan-advisor-agent/.venv/Lib/site-packages/tenacity/before.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2016 Julien Danjou
|
| 2 |
+
# Copyright 2016 Joshua Harlow
|
| 3 |
+
# Copyright 2013-2014 Ray Holder
|
| 4 |
+
#
|
| 5 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 6 |
+
# you may not use this file except in compliance with the License.
|
| 7 |
+
# You may obtain a copy of the License at
|
| 8 |
+
#
|
| 9 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 10 |
+
#
|
| 11 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 12 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 13 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 14 |
+
# See the License for the specific language governing permissions and
|
| 15 |
+
# limitations under the License.
|
| 16 |
+
|
| 17 |
+
import typing
|
| 18 |
+
|
| 19 |
+
from tenacity import _utils
|
| 20 |
+
|
| 21 |
+
if typing.TYPE_CHECKING:
|
| 22 |
+
from tenacity import RetryCallState
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def before_nothing(retry_state: "RetryCallState") -> None:
|
| 26 |
+
"""Before call strategy that does nothing."""
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def before_log(
|
| 30 |
+
logger: _utils.LoggerProtocol, log_level: int
|
| 31 |
+
) -> typing.Callable[["RetryCallState"], None]:
|
| 32 |
+
"""Before call strategy that logs to some logger the attempt."""
|
| 33 |
+
|
| 34 |
+
def log_it(retry_state: "RetryCallState") -> None:
|
| 35 |
+
if retry_state.fn is None:
|
| 36 |
+
# NOTE(sileht): can't really happen, but we must please mypy
|
| 37 |
+
fn_name = "<unknown>"
|
| 38 |
+
else:
|
| 39 |
+
fn_name = _utils.get_callback_name(retry_state.fn)
|
| 40 |
+
logger.log(
|
| 41 |
+
log_level,
|
| 42 |
+
f"Starting call to '{fn_name}', "
|
| 43 |
+
f"this is the {_utils.to_ordinal(retry_state.attempt_number)} time calling it.",
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
return log_it
|
loan-advisor-agent/.venv/Lib/site-packages/tenacity/before_sleep.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2016 Julien Danjou
|
| 2 |
+
# Copyright 2016 Joshua Harlow
|
| 3 |
+
# Copyright 2013-2014 Ray Holder
|
| 4 |
+
#
|
| 5 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 6 |
+
# you may not use this file except in compliance with the License.
|
| 7 |
+
# You may obtain a copy of the License at
|
| 8 |
+
#
|
| 9 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 10 |
+
#
|
| 11 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 12 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 13 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 14 |
+
# See the License for the specific language governing permissions and
|
| 15 |
+
# limitations under the License.
|
| 16 |
+
|
| 17 |
+
import typing
|
| 18 |
+
|
| 19 |
+
from tenacity import _utils
|
| 20 |
+
|
| 21 |
+
if typing.TYPE_CHECKING:
|
| 22 |
+
from tenacity import RetryCallState
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def before_sleep_nothing(retry_state: "RetryCallState") -> None:
|
| 26 |
+
"""Before sleep strategy that does nothing."""
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def before_sleep_log(
|
| 30 |
+
logger: _utils.LoggerProtocol,
|
| 31 |
+
log_level: int,
|
| 32 |
+
exc_info: bool = False,
|
| 33 |
+
sec_format: str = "%.3g",
|
| 34 |
+
) -> typing.Callable[["RetryCallState"], None]:
|
| 35 |
+
"""Before sleep strategy that logs to some logger the attempt."""
|
| 36 |
+
|
| 37 |
+
def log_it(retry_state: "RetryCallState") -> None:
|
| 38 |
+
local_exc_info: BaseException | bool | None
|
| 39 |
+
|
| 40 |
+
if retry_state.outcome is None:
|
| 41 |
+
raise RuntimeError("log_it() called before outcome was set")
|
| 42 |
+
|
| 43 |
+
if retry_state.next_action is None:
|
| 44 |
+
raise RuntimeError("log_it() called before next_action was set")
|
| 45 |
+
|
| 46 |
+
if retry_state.outcome.failed:
|
| 47 |
+
ex = retry_state.outcome.exception()
|
| 48 |
+
verb, value = "raised", f"{ex.__class__.__name__}: {ex}"
|
| 49 |
+
|
| 50 |
+
if exc_info:
|
| 51 |
+
local_exc_info = retry_state.outcome.exception()
|
| 52 |
+
else:
|
| 53 |
+
local_exc_info = False
|
| 54 |
+
else:
|
| 55 |
+
verb, value = "returned", retry_state.outcome.result()
|
| 56 |
+
local_exc_info = False # exc_info does not apply when no exception
|
| 57 |
+
|
| 58 |
+
if retry_state.fn is None:
|
| 59 |
+
# NOTE(sileht): can't really happen, but we must please mypy
|
| 60 |
+
fn_name = "<unknown>"
|
| 61 |
+
else:
|
| 62 |
+
fn_name = _utils.get_callback_name(retry_state.fn)
|
| 63 |
+
|
| 64 |
+
logger.log(
|
| 65 |
+
log_level,
|
| 66 |
+
f"Retrying {fn_name} "
|
| 67 |
+
f"in {sec_format % retry_state.next_action.sleep} seconds as it {verb} {value}.",
|
| 68 |
+
exc_info=local_exc_info,
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
return log_it
|
loan-advisor-agent/.venv/Lib/site-packages/tenacity/nap.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2016 Étienne Bersac
|
| 2 |
+
# Copyright 2016 Julien Danjou
|
| 3 |
+
# Copyright 2016 Joshua Harlow
|
| 4 |
+
# Copyright 2013-2014 Ray Holder
|
| 5 |
+
#
|
| 6 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 7 |
+
# you may not use this file except in compliance with the License.
|
| 8 |
+
# You may obtain a copy of the License at
|
| 9 |
+
#
|
| 10 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 11 |
+
#
|
| 12 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 13 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 14 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 15 |
+
# See the License for the specific language governing permissions and
|
| 16 |
+
# limitations under the License.
|
| 17 |
+
|
| 18 |
+
import time
|
| 19 |
+
import typing
|
| 20 |
+
|
| 21 |
+
if typing.TYPE_CHECKING:
|
| 22 |
+
import threading
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def sleep(seconds: float) -> None:
|
| 26 |
+
"""
|
| 27 |
+
Sleep strategy that delays execution for a given number of seconds.
|
| 28 |
+
|
| 29 |
+
This is the default strategy, and may be mocked out for unit testing.
|
| 30 |
+
"""
|
| 31 |
+
time.sleep(seconds)
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
class sleep_using_event:
|
| 35 |
+
"""Sleep strategy that waits on an event to be set."""
|
| 36 |
+
|
| 37 |
+
def __init__(self, event: "threading.Event") -> None:
|
| 38 |
+
self.event = event
|
| 39 |
+
|
| 40 |
+
def __call__(self, timeout: typing.Optional[float]) -> None:
|
| 41 |
+
# NOTE(harlowja): this may *not* actually wait for timeout
|
| 42 |
+
# seconds if the event is set (ie this may eject out early).
|
| 43 |
+
self.event.wait(timeout=timeout)
|
loan-advisor-agent/.venv/Lib/site-packages/tenacity/py.typed
ADDED
|
File without changes
|
loan-advisor-agent/.venv/Lib/site-packages/tenacity/retry.py
ADDED
|
@@ -0,0 +1,282 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2016–2021 Julien Danjou
|
| 2 |
+
# Copyright 2016 Joshua Harlow
|
| 3 |
+
# Copyright 2013-2014 Ray Holder
|
| 4 |
+
#
|
| 5 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 6 |
+
# you may not use this file except in compliance with the License.
|
| 7 |
+
# You may obtain a copy of the License at
|
| 8 |
+
#
|
| 9 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 10 |
+
#
|
| 11 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 12 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 13 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 14 |
+
# See the License for the specific language governing permissions and
|
| 15 |
+
# limitations under the License.
|
| 16 |
+
|
| 17 |
+
import abc
|
| 18 |
+
import re
|
| 19 |
+
import typing
|
| 20 |
+
|
| 21 |
+
if typing.TYPE_CHECKING:
|
| 22 |
+
from tenacity import RetryCallState
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
class retry_base(abc.ABC):
|
| 26 |
+
"""Abstract base class for retry strategies."""
|
| 27 |
+
|
| 28 |
+
@abc.abstractmethod
|
| 29 |
+
def __call__(self, retry_state: "RetryCallState") -> bool:
|
| 30 |
+
pass
|
| 31 |
+
|
| 32 |
+
def __and__(self, other: "retry_base") -> "retry_all":
|
| 33 |
+
return other.__rand__(self)
|
| 34 |
+
|
| 35 |
+
def __rand__(self, other: "retry_base") -> "retry_all":
|
| 36 |
+
return retry_all(other, self)
|
| 37 |
+
|
| 38 |
+
def __or__(self, other: "retry_base") -> "retry_any":
|
| 39 |
+
return other.__ror__(self)
|
| 40 |
+
|
| 41 |
+
def __ror__(self, other: "retry_base") -> "retry_any":
|
| 42 |
+
return retry_any(other, self)
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
RetryBaseT = typing.Union[retry_base, typing.Callable[["RetryCallState"], bool]]
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
class _retry_never(retry_base):
|
| 49 |
+
"""Retry strategy that never rejects any result."""
|
| 50 |
+
|
| 51 |
+
def __call__(self, retry_state: "RetryCallState") -> bool:
|
| 52 |
+
return False
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
retry_never = _retry_never()
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
class _retry_always(retry_base):
|
| 59 |
+
"""Retry strategy that always rejects any result."""
|
| 60 |
+
|
| 61 |
+
def __call__(self, retry_state: "RetryCallState") -> bool:
|
| 62 |
+
return True
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
retry_always = _retry_always()
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
class retry_if_exception(retry_base):
|
| 69 |
+
"""Retry strategy that retries if an exception verifies a predicate."""
|
| 70 |
+
|
| 71 |
+
def __init__(self, predicate: typing.Callable[[BaseException], bool]) -> None:
|
| 72 |
+
self.predicate = predicate
|
| 73 |
+
|
| 74 |
+
def __call__(self, retry_state: "RetryCallState") -> bool:
|
| 75 |
+
if retry_state.outcome is None:
|
| 76 |
+
raise RuntimeError("__call__() called before outcome was set")
|
| 77 |
+
|
| 78 |
+
if retry_state.outcome.failed:
|
| 79 |
+
exception = retry_state.outcome.exception()
|
| 80 |
+
if exception is None:
|
| 81 |
+
raise RuntimeError("outcome failed but the exception is None")
|
| 82 |
+
return self.predicate(exception)
|
| 83 |
+
else:
|
| 84 |
+
return False
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
class retry_if_exception_type(retry_if_exception):
|
| 88 |
+
"""Retries if an exception has been raised of one or more types."""
|
| 89 |
+
|
| 90 |
+
def __init__(
|
| 91 |
+
self,
|
| 92 |
+
exception_types: typing.Union[
|
| 93 |
+
typing.Type[BaseException],
|
| 94 |
+
typing.Tuple[typing.Type[BaseException], ...],
|
| 95 |
+
] = Exception,
|
| 96 |
+
) -> None:
|
| 97 |
+
self.exception_types = exception_types
|
| 98 |
+
super().__init__(lambda e: isinstance(e, exception_types))
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
class retry_if_not_exception_type(retry_if_exception):
|
| 102 |
+
"""Retries except an exception has been raised of one or more types."""
|
| 103 |
+
|
| 104 |
+
def __init__(
|
| 105 |
+
self,
|
| 106 |
+
exception_types: typing.Union[
|
| 107 |
+
typing.Type[BaseException],
|
| 108 |
+
typing.Tuple[typing.Type[BaseException], ...],
|
| 109 |
+
] = Exception,
|
| 110 |
+
) -> None:
|
| 111 |
+
self.exception_types = exception_types
|
| 112 |
+
super().__init__(lambda e: not isinstance(e, exception_types))
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
class retry_unless_exception_type(retry_if_exception):
|
| 116 |
+
"""Retries until an exception is raised of one or more types."""
|
| 117 |
+
|
| 118 |
+
def __init__(
|
| 119 |
+
self,
|
| 120 |
+
exception_types: typing.Union[
|
| 121 |
+
typing.Type[BaseException],
|
| 122 |
+
typing.Tuple[typing.Type[BaseException], ...],
|
| 123 |
+
] = Exception,
|
| 124 |
+
) -> None:
|
| 125 |
+
self.exception_types = exception_types
|
| 126 |
+
super().__init__(lambda e: not isinstance(e, exception_types))
|
| 127 |
+
|
| 128 |
+
def __call__(self, retry_state: "RetryCallState") -> bool:
|
| 129 |
+
if retry_state.outcome is None:
|
| 130 |
+
raise RuntimeError("__call__() called before outcome was set")
|
| 131 |
+
|
| 132 |
+
# always retry if no exception was raised
|
| 133 |
+
if not retry_state.outcome.failed:
|
| 134 |
+
return True
|
| 135 |
+
|
| 136 |
+
exception = retry_state.outcome.exception()
|
| 137 |
+
if exception is None:
|
| 138 |
+
raise RuntimeError("outcome failed but the exception is None")
|
| 139 |
+
return self.predicate(exception)
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
class retry_if_exception_cause_type(retry_base):
|
| 143 |
+
"""Retries if any of the causes of the raised exception is of one or more types.
|
| 144 |
+
|
| 145 |
+
The check on the type of the cause of the exception is done recursively (until finding
|
| 146 |
+
an exception in the chain that has no `__cause__`)
|
| 147 |
+
"""
|
| 148 |
+
|
| 149 |
+
def __init__(
|
| 150 |
+
self,
|
| 151 |
+
exception_types: typing.Union[
|
| 152 |
+
typing.Type[BaseException],
|
| 153 |
+
typing.Tuple[typing.Type[BaseException], ...],
|
| 154 |
+
] = Exception,
|
| 155 |
+
) -> None:
|
| 156 |
+
self.exception_cause_types = exception_types
|
| 157 |
+
|
| 158 |
+
def __call__(self, retry_state: "RetryCallState") -> bool:
|
| 159 |
+
if retry_state.outcome is None:
|
| 160 |
+
raise RuntimeError("__call__ called before outcome was set")
|
| 161 |
+
|
| 162 |
+
if retry_state.outcome.failed:
|
| 163 |
+
exc = retry_state.outcome.exception()
|
| 164 |
+
while exc is not None:
|
| 165 |
+
if isinstance(exc.__cause__, self.exception_cause_types):
|
| 166 |
+
return True
|
| 167 |
+
exc = exc.__cause__
|
| 168 |
+
|
| 169 |
+
return False
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
class retry_if_result(retry_base):
|
| 173 |
+
"""Retries if the result verifies a predicate."""
|
| 174 |
+
|
| 175 |
+
def __init__(self, predicate: typing.Callable[[typing.Any], bool]) -> None:
|
| 176 |
+
self.predicate = predicate
|
| 177 |
+
|
| 178 |
+
def __call__(self, retry_state: "RetryCallState") -> bool:
|
| 179 |
+
if retry_state.outcome is None:
|
| 180 |
+
raise RuntimeError("__call__() called before outcome was set")
|
| 181 |
+
|
| 182 |
+
if not retry_state.outcome.failed:
|
| 183 |
+
return self.predicate(retry_state.outcome.result())
|
| 184 |
+
else:
|
| 185 |
+
return False
|
| 186 |
+
|
| 187 |
+
|
| 188 |
+
class retry_if_not_result(retry_base):
|
| 189 |
+
"""Retries if the result refutes a predicate."""
|
| 190 |
+
|
| 191 |
+
def __init__(self, predicate: typing.Callable[[typing.Any], bool]) -> None:
|
| 192 |
+
self.predicate = predicate
|
| 193 |
+
|
| 194 |
+
def __call__(self, retry_state: "RetryCallState") -> bool:
|
| 195 |
+
if retry_state.outcome is None:
|
| 196 |
+
raise RuntimeError("__call__() called before outcome was set")
|
| 197 |
+
|
| 198 |
+
if not retry_state.outcome.failed:
|
| 199 |
+
return not self.predicate(retry_state.outcome.result())
|
| 200 |
+
else:
|
| 201 |
+
return False
|
| 202 |
+
|
| 203 |
+
|
| 204 |
+
class retry_if_exception_message(retry_if_exception):
|
| 205 |
+
"""Retries if an exception message equals or matches."""
|
| 206 |
+
|
| 207 |
+
def __init__(
|
| 208 |
+
self,
|
| 209 |
+
message: typing.Optional[str] = None,
|
| 210 |
+
match: typing.Union[None, str, typing.Pattern[str]] = None,
|
| 211 |
+
) -> None:
|
| 212 |
+
if message and match:
|
| 213 |
+
raise TypeError(
|
| 214 |
+
f"{self.__class__.__name__}() takes either 'message' or 'match', not both"
|
| 215 |
+
)
|
| 216 |
+
|
| 217 |
+
# set predicate
|
| 218 |
+
if message:
|
| 219 |
+
|
| 220 |
+
def message_fnc(exception: BaseException) -> bool:
|
| 221 |
+
return message == str(exception)
|
| 222 |
+
|
| 223 |
+
predicate = message_fnc
|
| 224 |
+
elif match:
|
| 225 |
+
prog = re.compile(match)
|
| 226 |
+
|
| 227 |
+
def match_fnc(exception: BaseException) -> bool:
|
| 228 |
+
return bool(prog.match(str(exception)))
|
| 229 |
+
|
| 230 |
+
predicate = match_fnc
|
| 231 |
+
else:
|
| 232 |
+
raise TypeError(
|
| 233 |
+
f"{self.__class__.__name__}() missing 1 required argument 'message' or 'match'"
|
| 234 |
+
)
|
| 235 |
+
|
| 236 |
+
super().__init__(predicate)
|
| 237 |
+
|
| 238 |
+
|
| 239 |
+
class retry_if_not_exception_message(retry_if_exception_message):
|
| 240 |
+
"""Retries until an exception message equals or matches."""
|
| 241 |
+
|
| 242 |
+
def __init__(
|
| 243 |
+
self,
|
| 244 |
+
message: typing.Optional[str] = None,
|
| 245 |
+
match: typing.Union[None, str, typing.Pattern[str]] = None,
|
| 246 |
+
) -> None:
|
| 247 |
+
super().__init__(message, match)
|
| 248 |
+
# invert predicate
|
| 249 |
+
if_predicate = self.predicate
|
| 250 |
+
self.predicate = lambda *args_, **kwargs_: not if_predicate(*args_, **kwargs_)
|
| 251 |
+
|
| 252 |
+
def __call__(self, retry_state: "RetryCallState") -> bool:
|
| 253 |
+
if retry_state.outcome is None:
|
| 254 |
+
raise RuntimeError("__call__() called before outcome was set")
|
| 255 |
+
|
| 256 |
+
if not retry_state.outcome.failed:
|
| 257 |
+
return True
|
| 258 |
+
|
| 259 |
+
exception = retry_state.outcome.exception()
|
| 260 |
+
if exception is None:
|
| 261 |
+
raise RuntimeError("outcome failed but the exception is None")
|
| 262 |
+
return self.predicate(exception)
|
| 263 |
+
|
| 264 |
+
|
| 265 |
+
class retry_any(retry_base):
|
| 266 |
+
"""Retries if any of the retries condition is valid."""
|
| 267 |
+
|
| 268 |
+
def __init__(self, *retries: retry_base) -> None:
|
| 269 |
+
self.retries = retries
|
| 270 |
+
|
| 271 |
+
def __call__(self, retry_state: "RetryCallState") -> bool:
|
| 272 |
+
return any(r(retry_state) for r in self.retries)
|
| 273 |
+
|
| 274 |
+
|
| 275 |
+
class retry_all(retry_base):
|
| 276 |
+
"""Retries if all the retries condition are valid."""
|
| 277 |
+
|
| 278 |
+
def __init__(self, *retries: retry_base) -> None:
|
| 279 |
+
self.retries = retries
|
| 280 |
+
|
| 281 |
+
def __call__(self, retry_state: "RetryCallState") -> bool:
|
| 282 |
+
return all(r(retry_state) for r in self.retries)
|
loan-advisor-agent/.venv/Lib/site-packages/tenacity/stop.py
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2016–2021 Julien Danjou
|
| 2 |
+
# Copyright 2016 Joshua Harlow
|
| 3 |
+
# Copyright 2013-2014 Ray Holder
|
| 4 |
+
#
|
| 5 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 6 |
+
# you may not use this file except in compliance with the License.
|
| 7 |
+
# You may obtain a copy of the License at
|
| 8 |
+
#
|
| 9 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 10 |
+
#
|
| 11 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 12 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 13 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 14 |
+
# See the License for the specific language governing permissions and
|
| 15 |
+
# limitations under the License.
|
| 16 |
+
import abc
|
| 17 |
+
import typing
|
| 18 |
+
|
| 19 |
+
from tenacity import _utils
|
| 20 |
+
|
| 21 |
+
if typing.TYPE_CHECKING:
|
| 22 |
+
import threading
|
| 23 |
+
|
| 24 |
+
from tenacity import RetryCallState
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
class stop_base(abc.ABC):
|
| 28 |
+
"""Abstract base class for stop strategies."""
|
| 29 |
+
|
| 30 |
+
@abc.abstractmethod
|
| 31 |
+
def __call__(self, retry_state: "RetryCallState") -> bool:
|
| 32 |
+
pass
|
| 33 |
+
|
| 34 |
+
def __and__(self, other: "stop_base") -> "stop_all":
|
| 35 |
+
return stop_all(self, other)
|
| 36 |
+
|
| 37 |
+
def __or__(self, other: "stop_base") -> "stop_any":
|
| 38 |
+
return stop_any(self, other)
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
StopBaseT = typing.Union[stop_base, typing.Callable[["RetryCallState"], bool]]
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
class stop_any(stop_base):
|
| 45 |
+
"""Stop if any of the stop condition is valid."""
|
| 46 |
+
|
| 47 |
+
def __init__(self, *stops: stop_base) -> None:
|
| 48 |
+
self.stops = stops
|
| 49 |
+
|
| 50 |
+
def __call__(self, retry_state: "RetryCallState") -> bool:
|
| 51 |
+
return any(x(retry_state) for x in self.stops)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
class stop_all(stop_base):
|
| 55 |
+
"""Stop if all the stop conditions are valid."""
|
| 56 |
+
|
| 57 |
+
def __init__(self, *stops: stop_base) -> None:
|
| 58 |
+
self.stops = stops
|
| 59 |
+
|
| 60 |
+
def __call__(self, retry_state: "RetryCallState") -> bool:
|
| 61 |
+
return all(x(retry_state) for x in self.stops)
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
class _stop_never(stop_base):
|
| 65 |
+
"""Never stop."""
|
| 66 |
+
|
| 67 |
+
def __call__(self, retry_state: "RetryCallState") -> bool:
|
| 68 |
+
return False
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
stop_never = _stop_never()
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
class stop_when_event_set(stop_base):
|
| 75 |
+
"""Stop when the given event is set."""
|
| 76 |
+
|
| 77 |
+
def __init__(self, event: "threading.Event") -> None:
|
| 78 |
+
self.event = event
|
| 79 |
+
|
| 80 |
+
def __call__(self, retry_state: "RetryCallState") -> bool:
|
| 81 |
+
return self.event.is_set()
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
class stop_after_attempt(stop_base):
|
| 85 |
+
"""Stop when the previous attempt >= max_attempt."""
|
| 86 |
+
|
| 87 |
+
def __init__(self, max_attempt_number: int) -> None:
|
| 88 |
+
self.max_attempt_number = max_attempt_number
|
| 89 |
+
|
| 90 |
+
def __call__(self, retry_state: "RetryCallState") -> bool:
|
| 91 |
+
return retry_state.attempt_number >= self.max_attempt_number
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
class stop_after_delay(stop_base):
|
| 95 |
+
"""
|
| 96 |
+
Stop when the time from the first attempt >= limit.
|
| 97 |
+
|
| 98 |
+
Note: `max_delay` will be exceeded, so when used with a `wait`, the actual total delay will be greater
|
| 99 |
+
than `max_delay` by some of the final sleep period before `max_delay` is exceeded.
|
| 100 |
+
|
| 101 |
+
If you need stricter timing with waits, consider `stop_before_delay` instead.
|
| 102 |
+
"""
|
| 103 |
+
|
| 104 |
+
def __init__(self, max_delay: _utils.time_unit_type) -> None:
|
| 105 |
+
self.max_delay = _utils.to_seconds(max_delay)
|
| 106 |
+
|
| 107 |
+
def __call__(self, retry_state: "RetryCallState") -> bool:
|
| 108 |
+
if retry_state.seconds_since_start is None:
|
| 109 |
+
raise RuntimeError("__call__() called but seconds_since_start is not set")
|
| 110 |
+
return retry_state.seconds_since_start >= self.max_delay
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
class stop_before_delay(stop_base):
|
| 114 |
+
"""
|
| 115 |
+
Stop right before the next attempt would take place after the time from the first attempt >= limit.
|
| 116 |
+
|
| 117 |
+
Most useful when you are using with a `wait` function like wait_random_exponential, but need to make
|
| 118 |
+
sure that the max_delay is not exceeded.
|
| 119 |
+
"""
|
| 120 |
+
|
| 121 |
+
def __init__(self, max_delay: _utils.time_unit_type) -> None:
|
| 122 |
+
self.max_delay = _utils.to_seconds(max_delay)
|
| 123 |
+
|
| 124 |
+
def __call__(self, retry_state: "RetryCallState") -> bool:
|
| 125 |
+
if retry_state.seconds_since_start is None:
|
| 126 |
+
raise RuntimeError("__call__() called but seconds_since_start is not set")
|
| 127 |
+
return (
|
| 128 |
+
retry_state.seconds_since_start + retry_state.upcoming_sleep
|
| 129 |
+
>= self.max_delay
|
| 130 |
+
)
|
loan-advisor-agent/.venv/Lib/site-packages/tenacity/tornadoweb.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2017 Elisey Zanko
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
|
| 15 |
+
import sys
|
| 16 |
+
import typing
|
| 17 |
+
|
| 18 |
+
from tenacity import BaseRetrying
|
| 19 |
+
from tenacity import DoAttempt
|
| 20 |
+
from tenacity import DoSleep
|
| 21 |
+
from tenacity import RetryCallState
|
| 22 |
+
|
| 23 |
+
from tornado import gen
|
| 24 |
+
|
| 25 |
+
if typing.TYPE_CHECKING:
|
| 26 |
+
from tornado.concurrent import Future
|
| 27 |
+
|
| 28 |
+
_RetValT = typing.TypeVar("_RetValT")
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
class TornadoRetrying(BaseRetrying):
|
| 32 |
+
def __init__(
|
| 33 |
+
self,
|
| 34 |
+
sleep: "typing.Callable[[float], Future[None]]" = gen.sleep,
|
| 35 |
+
**kwargs: typing.Any,
|
| 36 |
+
) -> None:
|
| 37 |
+
super().__init__(**kwargs)
|
| 38 |
+
self.sleep = sleep
|
| 39 |
+
|
| 40 |
+
@gen.coroutine # type: ignore[untyped-decorator]
|
| 41 |
+
def __call__(
|
| 42 |
+
self,
|
| 43 |
+
fn: "typing.Callable[..., typing.Union[typing.Generator[typing.Any, typing.Any, _RetValT], Future[_RetValT]]]",
|
| 44 |
+
*args: typing.Any,
|
| 45 |
+
**kwargs: typing.Any,
|
| 46 |
+
) -> "typing.Generator[typing.Any, typing.Any, _RetValT]":
|
| 47 |
+
self.begin()
|
| 48 |
+
|
| 49 |
+
retry_state = RetryCallState(retry_object=self, fn=fn, args=args, kwargs=kwargs)
|
| 50 |
+
while True:
|
| 51 |
+
do = self.iter(retry_state=retry_state)
|
| 52 |
+
if isinstance(do, DoAttempt):
|
| 53 |
+
try:
|
| 54 |
+
result = yield fn(*args, **kwargs)
|
| 55 |
+
except BaseException: # noqa: B902
|
| 56 |
+
retry_state.set_exception(sys.exc_info()) # type: ignore[arg-type]
|
| 57 |
+
else:
|
| 58 |
+
retry_state.set_result(result)
|
| 59 |
+
elif isinstance(do, DoSleep):
|
| 60 |
+
retry_state.prepare_for_next_attempt()
|
| 61 |
+
yield self.sleep(do)
|
| 62 |
+
else:
|
| 63 |
+
raise gen.Return(do)
|
loan-advisor-agent/.venv/Lib/site-packages/tenacity/wait.py
ADDED
|
@@ -0,0 +1,273 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2016–2021 Julien Danjou
|
| 2 |
+
# Copyright 2016 Joshua Harlow
|
| 3 |
+
# Copyright 2013-2014 Ray Holder
|
| 4 |
+
#
|
| 5 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 6 |
+
# you may not use this file except in compliance with the License.
|
| 7 |
+
# You may obtain a copy of the License at
|
| 8 |
+
#
|
| 9 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 10 |
+
#
|
| 11 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 12 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 13 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 14 |
+
# See the License for the specific language governing permissions and
|
| 15 |
+
# limitations under the License.
|
| 16 |
+
|
| 17 |
+
import abc
|
| 18 |
+
import random
|
| 19 |
+
import typing
|
| 20 |
+
|
| 21 |
+
from tenacity import _utils
|
| 22 |
+
|
| 23 |
+
if typing.TYPE_CHECKING:
|
| 24 |
+
from tenacity import RetryCallState
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
class wait_base(abc.ABC):
|
| 28 |
+
"""Abstract base class for wait strategies."""
|
| 29 |
+
|
| 30 |
+
@abc.abstractmethod
|
| 31 |
+
def __call__(self, retry_state: "RetryCallState") -> float:
|
| 32 |
+
pass
|
| 33 |
+
|
| 34 |
+
def __add__(self, other: "wait_base") -> "wait_combine":
|
| 35 |
+
return wait_combine(self, other)
|
| 36 |
+
|
| 37 |
+
def __radd__(self, other: "wait_base") -> typing.Union["wait_combine", "wait_base"]:
|
| 38 |
+
# make it possible to use multiple waits with the built-in sum function
|
| 39 |
+
if other == 0: # type: ignore[comparison-overlap]
|
| 40 |
+
return self
|
| 41 |
+
return self.__add__(other)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
WaitBaseT = typing.Union[
|
| 45 |
+
wait_base, typing.Callable[["RetryCallState"], typing.Union[float, int]]
|
| 46 |
+
]
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
class wait_fixed(wait_base):
|
| 50 |
+
"""Wait strategy that waits a fixed amount of time between each retry."""
|
| 51 |
+
|
| 52 |
+
def __init__(self, wait: _utils.time_unit_type) -> None:
|
| 53 |
+
self.wait_fixed = _utils.to_seconds(wait)
|
| 54 |
+
|
| 55 |
+
def __call__(self, retry_state: "RetryCallState") -> float:
|
| 56 |
+
return self.wait_fixed
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
class wait_none(wait_fixed):
|
| 60 |
+
"""Wait strategy that doesn't wait at all before retrying."""
|
| 61 |
+
|
| 62 |
+
def __init__(self) -> None:
|
| 63 |
+
super().__init__(0)
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
class wait_random(wait_base):
|
| 67 |
+
"""Wait strategy that waits a random amount of time between min/max."""
|
| 68 |
+
|
| 69 |
+
def __init__(
|
| 70 |
+
self, min: _utils.time_unit_type = 0, max: _utils.time_unit_type = 1
|
| 71 |
+
) -> None: # noqa
|
| 72 |
+
self.wait_random_min = _utils.to_seconds(min)
|
| 73 |
+
self.wait_random_max = _utils.to_seconds(max)
|
| 74 |
+
|
| 75 |
+
def __call__(self, retry_state: "RetryCallState") -> float:
|
| 76 |
+
return self.wait_random_min + (
|
| 77 |
+
random.random() * (self.wait_random_max - self.wait_random_min)
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
class wait_combine(wait_base):
|
| 82 |
+
"""Combine several waiting strategies."""
|
| 83 |
+
|
| 84 |
+
def __init__(self, *strategies: wait_base) -> None:
|
| 85 |
+
self.wait_funcs = strategies
|
| 86 |
+
|
| 87 |
+
def __call__(self, retry_state: "RetryCallState") -> float:
|
| 88 |
+
return sum(x(retry_state=retry_state) for x in self.wait_funcs)
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
class wait_chain(wait_base):
|
| 92 |
+
"""Chain two or more waiting strategies.
|
| 93 |
+
|
| 94 |
+
If all strategies are exhausted, the very last strategy is used
|
| 95 |
+
thereafter.
|
| 96 |
+
|
| 97 |
+
For example::
|
| 98 |
+
|
| 99 |
+
@retry(wait=wait_chain(*[wait_fixed(1) for i in range(3)] +
|
| 100 |
+
[wait_fixed(2) for j in range(5)] +
|
| 101 |
+
[wait_fixed(5) for k in range(4)]))
|
| 102 |
+
def wait_chained():
|
| 103 |
+
print("Wait 1s for 3 attempts, 2s for 5 attempts and 5s "
|
| 104 |
+
"thereafter.")
|
| 105 |
+
"""
|
| 106 |
+
|
| 107 |
+
def __init__(self, *strategies: wait_base) -> None:
|
| 108 |
+
self.strategies = strategies
|
| 109 |
+
|
| 110 |
+
def __call__(self, retry_state: "RetryCallState") -> float:
|
| 111 |
+
wait_func_no = min(max(retry_state.attempt_number, 1), len(self.strategies))
|
| 112 |
+
wait_func = self.strategies[wait_func_no - 1]
|
| 113 |
+
return wait_func(retry_state=retry_state)
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
class wait_exception(wait_base):
|
| 117 |
+
"""Wait strategy that waits the amount of time returned by the predicate.
|
| 118 |
+
|
| 119 |
+
The predicate is passed the exception object. Based on the exception, the
|
| 120 |
+
user can decide how much time to wait before retrying.
|
| 121 |
+
|
| 122 |
+
For example::
|
| 123 |
+
|
| 124 |
+
def http_error(exception: BaseException) -> float:
|
| 125 |
+
if (
|
| 126 |
+
isinstance(exception, requests.HTTPError)
|
| 127 |
+
and exception.response.status_code == requests.codes.too_many_requests
|
| 128 |
+
):
|
| 129 |
+
return float(exception.response.headers.get("Retry-After", "1"))
|
| 130 |
+
return 60.0
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
@retry(
|
| 134 |
+
stop=stop_after_attempt(3),
|
| 135 |
+
wait=wait_exception(http_error),
|
| 136 |
+
)
|
| 137 |
+
def http_get_request(url: str) -> None:
|
| 138 |
+
response = requests.get(url)
|
| 139 |
+
response.raise_for_status()
|
| 140 |
+
"""
|
| 141 |
+
|
| 142 |
+
def __init__(self, predicate: typing.Callable[[BaseException], float]) -> None:
|
| 143 |
+
self.predicate = predicate
|
| 144 |
+
|
| 145 |
+
def __call__(self, retry_state: "RetryCallState") -> float:
|
| 146 |
+
if retry_state.outcome is None:
|
| 147 |
+
raise RuntimeError("__call__() called before outcome was set")
|
| 148 |
+
|
| 149 |
+
exception = retry_state.outcome.exception()
|
| 150 |
+
if exception is None:
|
| 151 |
+
raise RuntimeError("outcome failed but the exception is None")
|
| 152 |
+
return self.predicate(exception)
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
class wait_incrementing(wait_base):
|
| 156 |
+
"""Wait an incremental amount of time after each attempt.
|
| 157 |
+
|
| 158 |
+
Starting at a starting value and incrementing by a value for each attempt
|
| 159 |
+
(and restricting the upper limit to some maximum value).
|
| 160 |
+
"""
|
| 161 |
+
|
| 162 |
+
def __init__(
|
| 163 |
+
self,
|
| 164 |
+
start: _utils.time_unit_type = 0,
|
| 165 |
+
increment: _utils.time_unit_type = 100,
|
| 166 |
+
max: _utils.time_unit_type = _utils.MAX_WAIT, # noqa
|
| 167 |
+
) -> None:
|
| 168 |
+
self.start = _utils.to_seconds(start)
|
| 169 |
+
self.increment = _utils.to_seconds(increment)
|
| 170 |
+
self.max = _utils.to_seconds(max)
|
| 171 |
+
|
| 172 |
+
def __call__(self, retry_state: "RetryCallState") -> float:
|
| 173 |
+
result = self.start + (self.increment * (retry_state.attempt_number - 1))
|
| 174 |
+
return max(0, min(result, self.max))
|
| 175 |
+
|
| 176 |
+
|
| 177 |
+
class wait_exponential(wait_base):
|
| 178 |
+
"""Wait strategy that applies exponential backoff.
|
| 179 |
+
|
| 180 |
+
It allows for a customized multiplier and an ability to restrict the
|
| 181 |
+
upper and lower limits to some maximum and minimum value.
|
| 182 |
+
|
| 183 |
+
The intervals are fixed (i.e. there is no jitter), so this strategy is
|
| 184 |
+
suitable for balancing retries against latency when a required resource is
|
| 185 |
+
unavailable for an unknown duration, but *not* suitable for resolving
|
| 186 |
+
contention between multiple processes for a shared resource. Use
|
| 187 |
+
wait_random_exponential for the latter case.
|
| 188 |
+
"""
|
| 189 |
+
|
| 190 |
+
def __init__(
|
| 191 |
+
self,
|
| 192 |
+
multiplier: typing.Union[int, float] = 1,
|
| 193 |
+
max: _utils.time_unit_type = _utils.MAX_WAIT, # noqa
|
| 194 |
+
exp_base: typing.Union[int, float] = 2,
|
| 195 |
+
min: _utils.time_unit_type = 0, # noqa
|
| 196 |
+
) -> None:
|
| 197 |
+
self.multiplier = multiplier
|
| 198 |
+
self.min = _utils.to_seconds(min)
|
| 199 |
+
self.max = _utils.to_seconds(max)
|
| 200 |
+
self.exp_base = exp_base
|
| 201 |
+
|
| 202 |
+
def __call__(self, retry_state: "RetryCallState") -> float:
|
| 203 |
+
try:
|
| 204 |
+
exp = self.exp_base ** (retry_state.attempt_number - 1)
|
| 205 |
+
result = self.multiplier * exp
|
| 206 |
+
except OverflowError:
|
| 207 |
+
return self.max
|
| 208 |
+
return max(max(0, self.min), min(result, self.max))
|
| 209 |
+
|
| 210 |
+
|
| 211 |
+
class wait_random_exponential(wait_exponential):
|
| 212 |
+
"""Random wait with exponentially widening window.
|
| 213 |
+
|
| 214 |
+
An exponential backoff strategy used to mediate contention between multiple
|
| 215 |
+
uncoordinated processes for a shared resource in distributed systems. This
|
| 216 |
+
is the sense in which "exponential backoff" is meant in e.g. Ethernet
|
| 217 |
+
networking, and corresponds to the "Full Jitter" algorithm described in
|
| 218 |
+
this blog post:
|
| 219 |
+
|
| 220 |
+
https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/
|
| 221 |
+
|
| 222 |
+
Each retry occurs at a random time in a geometrically expanding interval.
|
| 223 |
+
It allows for a custom multiplier and an ability to restrict the upper
|
| 224 |
+
limit of the random interval to some maximum value.
|
| 225 |
+
|
| 226 |
+
Example::
|
| 227 |
+
|
| 228 |
+
wait_random_exponential(multiplier=0.5, # initial window 0.5s
|
| 229 |
+
max=60) # max 60s timeout
|
| 230 |
+
|
| 231 |
+
When waiting for an unavailable resource to become available again, as
|
| 232 |
+
opposed to trying to resolve contention for a shared resource, the
|
| 233 |
+
wait_exponential strategy (which uses a fixed interval) may be preferable.
|
| 234 |
+
|
| 235 |
+
"""
|
| 236 |
+
|
| 237 |
+
def __call__(self, retry_state: "RetryCallState") -> float:
|
| 238 |
+
high = super().__call__(retry_state=retry_state)
|
| 239 |
+
return random.uniform(self.min, high)
|
| 240 |
+
|
| 241 |
+
|
| 242 |
+
class wait_exponential_jitter(wait_base):
|
| 243 |
+
"""Wait strategy that applies exponential backoff and jitter.
|
| 244 |
+
|
| 245 |
+
It allows for a customized initial wait, maximum wait and jitter.
|
| 246 |
+
|
| 247 |
+
This implements the strategy described here:
|
| 248 |
+
https://cloud.google.com/storage/docs/retry-strategy
|
| 249 |
+
|
| 250 |
+
The wait time is min(initial * 2**n + random.uniform(0, jitter), maximum)
|
| 251 |
+
where n is the retry count.
|
| 252 |
+
"""
|
| 253 |
+
|
| 254 |
+
def __init__(
|
| 255 |
+
self,
|
| 256 |
+
initial: float = 1,
|
| 257 |
+
max: float = _utils.MAX_WAIT, # noqa
|
| 258 |
+
exp_base: float = 2,
|
| 259 |
+
jitter: float = 1,
|
| 260 |
+
) -> None:
|
| 261 |
+
self.initial = initial
|
| 262 |
+
self.max = max
|
| 263 |
+
self.exp_base = exp_base
|
| 264 |
+
self.jitter = jitter
|
| 265 |
+
|
| 266 |
+
def __call__(self, retry_state: "RetryCallState") -> float:
|
| 267 |
+
jitter = random.uniform(0, self.jitter)
|
| 268 |
+
try:
|
| 269 |
+
exp = self.exp_base ** (retry_state.attempt_number - 1)
|
| 270 |
+
result = self.initial * exp + jitter
|
| 271 |
+
except OverflowError:
|
| 272 |
+
result = self.max
|
| 273 |
+
return max(0, min(result, self.max))
|