|
4 | 4 | from typing import Any, Callable, Optional, Tuple, TypeVar |
5 | 5 |
|
6 | 6 | from pymongo.errors import AutoReconnect, ConnectionFailure, NetworkTimeout, PyMongoError, ServerSelectionTimeoutError |
7 | | -from tenacity import Retrying, retry_if_exception_type, stop_after_attempt, wait_exponential |
| 7 | + |
| 8 | +try: |
| 9 | + from tenacity import Retrying, retry_if_exception_type, stop_after_attempt, wait_exponential |
| 10 | + |
| 11 | + _has_tenacity = True |
| 12 | +except ImportError: |
| 13 | + _has_tenacity = False |
8 | 14 |
|
9 | 15 | _logger = logging.getLogger(__name__) |
10 | 16 | _T = TypeVar("_T") |
|
19 | 25 |
|
20 | 26 | @dataclass(frozen=True) |
21 | 27 | class RetryConfig: |
22 | | - enabled: bool = True |
| 28 | + enabled: bool = False |
23 | 29 | attempts: int = 3 |
24 | 30 | wait_min: float = 0.1 |
25 | 31 | wait_max: float = 1.0 |
26 | 32 |
|
27 | 33 | @classmethod |
28 | 34 | def from_kwargs(cls, kwargs: dict) -> "RetryConfig": |
29 | | - enabled = bool(kwargs.pop("retry_enabled", True)) |
| 35 | + enabled = bool(kwargs.pop("retry_enabled", False)) |
30 | 36 | attempts = int(kwargs.pop("retry_attempts", 3)) |
31 | 37 | wait_min = float(kwargs.pop("retry_wait_min", 0.1)) |
32 | 38 | wait_max = float(kwargs.pop("retry_wait_max", 1.0)) |
@@ -57,6 +63,13 @@ def execute_with_retry( |
57 | 63 | if not config.enabled or config.attempts <= 1: |
58 | 64 | return operation() |
59 | 65 |
|
| 66 | + if not _has_tenacity: |
| 67 | + _logger.warning( |
| 68 | + "Retry is enabled but 'tenacity' package is not installed. " |
| 69 | + "Falling back to no-retry. Install it with: pip install pymongosql[retry]" |
| 70 | + ) |
| 71 | + return operation() |
| 72 | + |
60 | 73 | def _before_sleep(retry_state: Any) -> None: |
61 | 74 | error = retry_state.outcome.exception() if retry_state.outcome else None |
62 | 75 | _logger.warning( |
|
0 commit comments