|
| 1 | +"""Ready-made wait strategies and wait creators.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from dataclasses import dataclass, field |
| 6 | +from typing import TYPE_CHECKING, Generic |
| 7 | + |
| 8 | +from aws_durable_execution_sdk_python.config import JitterStrategy, T |
| 9 | + |
| 10 | +if TYPE_CHECKING: |
| 11 | + from collections.abc import Callable |
| 12 | + |
| 13 | + from aws_durable_execution_sdk_python.serdes import SerDes |
| 14 | + |
| 15 | +Numeric = int | float |
| 16 | + |
| 17 | + |
| 18 | +@dataclass |
| 19 | +class WaitDecision: |
| 20 | + """Decision about whether to wait a step and with what delay.""" |
| 21 | + |
| 22 | + should_wait: bool |
| 23 | + delay_seconds: int |
| 24 | + |
| 25 | + @classmethod |
| 26 | + def wait(cls, delay_seconds: int) -> WaitDecision: |
| 27 | + """Create a wait decision.""" |
| 28 | + return cls(should_wait=True, delay_seconds=delay_seconds) |
| 29 | + |
| 30 | + @classmethod |
| 31 | + def no_wait(cls) -> WaitDecision: |
| 32 | + """Create a no-wait decision.""" |
| 33 | + return cls(should_wait=False, delay_seconds=0) |
| 34 | + |
| 35 | + |
| 36 | +@dataclass |
| 37 | +class WaitStrategyConfig(Generic[T]): |
| 38 | + should_continue_polling: Callable[[T], bool] |
| 39 | + max_attempts: int = 60 |
| 40 | + initial_delay_seconds: int = 5 |
| 41 | + max_delay_seconds: int = 300 # 5 minutes |
| 42 | + backoff_rate: Numeric = 1.5 |
| 43 | + jitter_strategy: JitterStrategy = field(default=JitterStrategy.FULL) |
| 44 | + timeout_seconds: int | None = None # Not implemented yet |
| 45 | + |
| 46 | + |
| 47 | +def create_wait_strategy( |
| 48 | + config: WaitStrategyConfig[T], |
| 49 | +) -> Callable[[T, int], WaitDecision]: |
| 50 | + def wait_strategy(result: T, attempts_made: int) -> WaitDecision: |
| 51 | + # Check if condition is met |
| 52 | + if not config.should_continue_polling(result): |
| 53 | + return WaitDecision.no_wait() |
| 54 | + |
| 55 | + # Check if we've exceeded max attempts |
| 56 | + if attempts_made >= config.max_attempts: |
| 57 | + return WaitDecision.no_wait() |
| 58 | + |
| 59 | + # Calculate delay with exponential backoff |
| 60 | + base_delay = min( |
| 61 | + config.initial_delay_seconds * (config.backoff_rate ** (attempts_made - 1)), |
| 62 | + config.max_delay_seconds, |
| 63 | + ) |
| 64 | + |
| 65 | + # Apply jitter (add jitter to base delay) |
| 66 | + jitter = config.jitter_strategy.compute_jitter(base_delay) |
| 67 | + delay_with_jitter = base_delay + jitter |
| 68 | + |
| 69 | + # Ensure delay is an integer >= 1 |
| 70 | + final_delay = max(1, round(delay_with_jitter)) |
| 71 | + |
| 72 | + return WaitDecision.wait(final_delay) |
| 73 | + |
| 74 | + return wait_strategy |
| 75 | + |
| 76 | + |
| 77 | +@dataclass(frozen=True) |
| 78 | +class WaitForConditionDecision: |
| 79 | + """Decision about whether to continue waiting.""" |
| 80 | + |
| 81 | + should_continue: bool |
| 82 | + delay_seconds: int |
| 83 | + |
| 84 | + @classmethod |
| 85 | + def continue_waiting(cls, delay_seconds: int) -> WaitForConditionDecision: |
| 86 | + """Create a decision to continue waiting for delay_seconds.""" |
| 87 | + return cls(should_continue=True, delay_seconds=delay_seconds) |
| 88 | + |
| 89 | + @classmethod |
| 90 | + def stop_polling(cls) -> WaitForConditionDecision: |
| 91 | + """Create a decision to stop polling.""" |
| 92 | + return cls(should_continue=False, delay_seconds=-1) |
| 93 | + |
| 94 | + |
| 95 | +@dataclass(frozen=True) |
| 96 | +class WaitForConditionConfig(Generic[T]): |
| 97 | + """Configuration for wait_for_condition.""" |
| 98 | + |
| 99 | + wait_strategy: Callable[[T, int], WaitForConditionDecision] |
| 100 | + initial_state: T |
| 101 | + serdes: SerDes | None = None |
0 commit comments