Artvv commited on
Commit
f01c3d9
·
verified ·
1 Parent(s): a254eeb

Upload src/persistentpoker_bench/retries.py with huggingface_hub

Browse files
src/persistentpoker_bench/retries.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import time
4
+ from dataclasses import dataclass
5
+ from typing import Callable, TypeVar
6
+
7
+ T = TypeVar("T")
8
+
9
+
10
+ @dataclass(frozen=True, slots=True)
11
+ class RetryPolicy:
12
+ max_attempts: int = 3
13
+ initial_delay_seconds: float = 0.25
14
+ backoff_multiplier: float = 2.0
15
+ retry_on_parse_failure: bool = True
16
+
17
+ def __post_init__(self) -> None:
18
+ if self.max_attempts < 1:
19
+ raise ValueError("max_attempts must be at least 1.")
20
+ if self.initial_delay_seconds < 0:
21
+ raise ValueError("initial_delay_seconds cannot be negative.")
22
+ if self.backoff_multiplier < 1:
23
+ raise ValueError("backoff_multiplier must be at least 1.")
24
+
25
+
26
+ def run_with_retries(
27
+ operation: Callable[[], T],
28
+ *,
29
+ policy: RetryPolicy,
30
+ is_retryable_exception: Callable[[Exception], bool],
31
+ ) -> tuple[T, int]:
32
+ attempt = 0
33
+ delay = policy.initial_delay_seconds
34
+
35
+ while True:
36
+ attempt += 1
37
+ try:
38
+ return operation(), attempt
39
+ except Exception as exc:
40
+ if attempt >= policy.max_attempts or not is_retryable_exception(exc):
41
+ raise
42
+ if delay > 0:
43
+ time.sleep(delay)
44
+ delay *= policy.backoff_multiplier
45
+