API Reference
CallLimiter
call_limiter.CallLimiter
High-Performance Rate Limiter.
Modes
allow_burst=True Fixed-window burst limiter. Example: calls=5, period=10
Allows:
5 immediate calls
then blocks until next 10-second window.
allow_burst=False Strict drip/paced limiter. Evenly spaces calls across the period.
Features
- Thread-safe
- High-precision sleep
- Adaptive OS jitter compensation
- CPU efficient (minimal spin waiting)
Source code in call_limiter/limiter.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | |
wait()
Block until a call is allowed.
Source code in call_limiter/limiter.py
CallRetry
call_limiter.CallRetry
A configurable retry decorator for resilient function execution.
Wraps a function to automatically retry on specified exceptions, with a fixed delay between attempts. Supports optional logging on each retry and a fallback function when all retries are exhausted.
Can be used as a decorator or called directly to wrap a function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
retry_count
|
int
|
Maximum number of retries after the initial attempt.
Total attempts will be |
5
|
retry_interval
|
float
|
Delay in seconds between retry attempts. |
1.0
|
retry_exceptions
|
Tuple[Type[Exception], ...]
|
Tuple of exception types that trigger a retry. Any exception not in this tuple will propagate immediately. |
(Exception,)
|
on_retry
|
Optional[Callable[[Exception, int], None]]
|
Optional callback invoked on each retry. Receives the caught exception and the current attempt number (1-indexed). |
None
|
fallback
|
Optional[Callable[[Exception], Any]]
|
Optional function called when all retries are exhausted. Receives the last exception, and its return value is used as the overall result. If not provided, the last exception is raised. |
None
|
Examples:
Basic retry with fallback:
>>> retry = CallRetry(
... retry_count=3,
... retry_interval=1.0,
... retry_exceptions=(ValueError,),
... on_retry=lambda e, n: print(f"Retry {n}: {e}"),
... fallback=lambda e: "default"
... )
>>> resilient_func = retry(my_function)
Retry without fallback (raises on exhaustion):
Source code in call_limiter/limiter.py
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | |
__call__(func)
Decorate a function to apply retry logic on each call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable
|
The function to wrap with retry logic. |
required |
Returns:
| Type | Description |
|---|---|
Callable
|
A wrapped function that retries on configured exceptions. |
Source code in call_limiter/limiter.py
ResilientLimiter
call_limiter.ResilientLimiter
A rate limiter with built-in retry logic for resilient function execution.
Combines CallLimiter and CallRetry so that every call — including
retries — respects the configured rate limit. This prevents retry storms
from overwhelming a rate-limited service.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
calls
|
int
|
Maximum number of calls allowed per period. |
required |
period
|
float
|
Time window in seconds for the rate limit. |
1.0
|
allow_burst
|
bool
|
If True, calls can fire immediately up to capacity. If False, calls are evenly spaced (drip mode). |
False
|
retry_count
|
int
|
Maximum number of retries after the initial attempt. |
3
|
retry_interval
|
float
|
Extra delay in seconds between retry attempts, added on top of the rate limiter's pacing. Defaults to 0 because the rate limiter already enforces pacing. |
0
|
retry_exceptions
|
Tuple[Type[Exception], ...]
|
Tuple of exception types that trigger a retry. |
(Exception,)
|
on_retry
|
Optional[Callable[[Exception, int], None]]
|
Optional callback invoked on each retry. Receives the caught exception and the current attempt number (1-indexed). If not provided, retries happen silently. |
None
|
fallback
|
Optional[Callable[[Exception], Any]]
|
Optional function called when all retries are exhausted. Receives the last exception, and its return value is used as the overall result. If not provided, the last exception is raised. |
None
|
Examples:
Rate-limited function with retry and fallback:
>>> limiter = ResilientLimiter(
... calls=5,
... period=1.0,
... allow_burst=True,
... retry_count=3,
... on_retry=lambda e, n: print(f"Retry {n}: {e}"),
... fallback=lambda e: "default"
... )
>>> @limiter
... def my_function():
... pass
Source code in call_limiter/limiter.py
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 | |
__call__(func)
Decorate a function with rate limiting and retry logic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable
|
The function to wrap. Each call and retry will respect the configured rate limit. |
required |
Returns:
| Type | Description |
|---|---|
Callable
|
A wrapped function with both rate limiting and retry behavior. |