Commit 572d07b
refactor: extract rate-limit helpers; add async rate-limit support
Move Retry-After parsing and sleep-decision logic into three shared
module-level helpers in caldav/lib/error.py, adjacent to RateLimitError:
parse_retry_after(header) -> float | None
compute_sleep_seconds(s, default, max) -> float | None
raise_if_rate_limited(status, url, header) -> None (raises on 429/503)
Both sync (DAVClient) and async (AsyncDAVClient) clients now call these
helpers instead of duplicating the logic inline.
Changes per file:
- caldav/lib/error.py: add helpers + necessary imports (datetime, timezone,
parsedate_to_datetime)
- caldav/davclient.py: replace 15-line inline parse block with single
raise_if_rate_limited() call; simplify request() except handler via
compute_sleep_seconds(); remove now-unused datetime/timezone/
parsedate_to_datetime imports
- caldav/async_davclient.py: add rate_limit_handle/default_sleep/max_sleep
params to __init__; split monolithic request() into request() (thin
rate-limit wrapper using asyncio.sleep) + _async_request() (HTTP call
+ auth negotiation); insert raise_if_rate_limited() call in _async_request
- tests/test_caldav_unit.py: add TestRateLimitHelpers (17 tests covering
all three helpers directly)
- tests/test_async_davclient.py: add TestAsyncRateLimiting (9 tests
mirroring the sync TestRateLimiting class, using AsyncMock + patched
asyncio.sleep)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>1 parent 5ed1805 commit 572d07b
5 files changed
Lines changed: 334 additions & 60 deletions
File tree
- caldav
- lib
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| 9 | + | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
| |||
143 | 144 | | |
144 | 145 | | |
145 | 146 | | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
146 | 150 | | |
147 | 151 | | |
148 | 152 | | |
| |||
162 | 166 | | |
163 | 167 | | |
164 | 168 | | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
165 | 176 | | |
166 | 177 | | |
167 | 178 | | |
| |||
247 | 258 | | |
248 | 259 | | |
249 | 260 | | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
250 | 265 | | |
251 | 266 | | |
252 | 267 | | |
| |||
326 | 341 | | |
327 | 342 | | |
328 | 343 | | |
329 | | - | |
| 344 | + | |
330 | 345 | | |
331 | | - | |
332 | | - | |
333 | | - | |
334 | | - | |
335 | | - | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
336 | 364 | | |
337 | | - | |
338 | | - | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
339 | 377 | | |
340 | 378 | | |
341 | 379 | | |
| |||
438 | 476 | | |
439 | 477 | | |
440 | 478 | | |
441 | | - | |
442 | | - | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
443 | 483 | | |
444 | 484 | | |
445 | 485 | | |
| |||
458 | 498 | | |
459 | 499 | | |
460 | 500 | | |
461 | | - | |
| 501 | + | |
462 | 502 | | |
463 | 503 | | |
464 | 504 | | |
| |||
467 | 507 | | |
468 | 508 | | |
469 | 509 | | |
470 | | - | |
471 | | - | |
| 510 | + | |
| 511 | + | |
472 | 512 | | |
473 | | - | |
| 513 | + | |
474 | 514 | | |
475 | 515 | | |
476 | 516 | | |
477 | | - | |
478 | | - | |
479 | 517 | | |
480 | | - | |
481 | | - | |
482 | | - | |
483 | | - | |
484 | | - | |
485 | | - | |
486 | | - | |
487 | | - | |
488 | | - | |
489 | | - | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
490 | 522 | | |
491 | 523 | | |
492 | 524 | | |
493 | 525 | | |
494 | 526 | | |
495 | 527 | | |
496 | 528 | | |
497 | | - | |
| 529 | + | |
498 | 530 | | |
499 | | - | |
| 531 | + | |
500 | 532 | | |
501 | 533 | | |
502 | 534 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
17 | | - | |
18 | 16 | | |
19 | 17 | | |
20 | 18 | | |
| |||
961 | 959 | | |
962 | 960 | | |
963 | 961 | | |
964 | | - | |
965 | | - | |
966 | | - | |
967 | | - | |
| 962 | + | |
| 963 | + | |
| 964 | + | |
| 965 | + | |
968 | 966 | | |
969 | | - | |
| 967 | + | |
970 | 968 | | |
971 | | - | |
972 | | - | |
973 | | - | |
974 | | - | |
975 | | - | |
| 969 | + | |
976 | 970 | | |
977 | 971 | | |
978 | 972 | | |
| |||
1019 | 1013 | | |
1020 | 1014 | | |
1021 | 1015 | | |
1022 | | - | |
1023 | | - | |
1024 | | - | |
1025 | | - | |
1026 | | - | |
1027 | | - | |
1028 | | - | |
1029 | | - | |
1030 | | - | |
1031 | | - | |
1032 | | - | |
1033 | | - | |
1034 | | - | |
1035 | | - | |
1036 | | - | |
1037 | | - | |
1038 | | - | |
1039 | | - | |
1040 | | - | |
1041 | | - | |
| 1016 | + | |
1042 | 1017 | | |
1043 | 1018 | | |
1044 | 1019 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
4 | 7 | | |
5 | 8 | | |
6 | 9 | | |
| |||
149 | 152 | | |
150 | 153 | | |
151 | 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 | + | |
| 205 | + | |
| 206 | + | |
| 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 | + | |
152 | 234 | | |
153 | 235 | | |
154 | 236 | | |
| |||
0 commit comments