Repository navigation
feat: Retry unprocessed requests in RequestQueueClient.batch_add_requests - #1244
cobanfurkanx wants to merge 2 commits into
Conversation
RequestQueueClient.batch_add_requests
| batch_processed_requests = list[AddedRequest]() | ||
| batch_unprocessed_requests = list[UnprocessedRequest]() | ||
|
|
||
| for attempt in range(1 + max_unprocessed_requests_retries): |
There was a problem hiding this comment.
Important: If a retry round fails after the HTTP client's own retries (for example a 5xx), the method raises, and the requests that earlier rounds added are lost from the result. On master the same call returns the partial result, and the SDK RQ clients treat a raise as "nothing added". The JS client logs the error and returns the processed requests plus the remaining ones as unprocessed. Could you do the same for rounds with attempt > 0, in sync and async (:1039), and add a test?
✍️ Drafted by Claude Code
| """ | ||
| if max_parallel != 1: | ||
| raise NotImplementedError('max_parallel is only supported in async client') | ||
| if max_unprocessed_requests_retries < 0: |
There was a problem hiding this comment.
Suggestion: Please also validate min_delay_between_unprocessed_requests_retries >= timedelta(0) here and in the async twin (:1114). Today a negative value makes the sync time.sleep raise after requests were already added, while the async asyncio.sleep silently skips the backoff.
✍️ Drafted by Claude Code
| DEFAULT_MIN_DELAY_BETWEEN_RETRIES = timedelta(milliseconds=500) | ||
| """Default minimum delay between retries.""" | ||
|
|
||
| DEFAULT_UNPROCESSED_RETRIES_BATCH_ADD_REQUESTS = 3 |
There was a problem hiding this comment.
Note: docs/04_upgrading/upgrading_to_v3.mdx:138 says max_unprocessed_requests_retries and min_delay_between_unprocessed_requests_retries "were no-ops; drop them". This PR brings them back with working behavior, so please update that line.
✍️ Drafted by Claude Code
| unprocessed = batch_response.data.unprocessed_requests | ||
| if not unprocessed: | ||
| return [], unprocessed | ||
| statistics.add_rate_limit_error(attempt + 1) |
There was a problem hiding this comment.
Nit: ClientStatistics.rate_limit_errors in _statistics.py is documented as counting 429 errors. It now also counts batch-add rounds with unprocessed requests (the same as in JS), so please update the docstring.
✍️ Drafted by Claude Code
| return handler | ||
|
|
||
|
|
||
| async def test_batch_add_requests_retries_unprocessed_requests_async(httpserver: HTTPServer) -> None: |
There was a problem hiding this comment.
Suggestion: Please add tests for max_unprocessed_requests_retries=0 (one call, no retry) and for the ValueError on a negative value.
✍️ Drafted by Claude Code
| round_num = 0 | ||
|
|
||
| def handler(request: Request) -> Response: | ||
| nonlocal round_num |
There was a problem hiding this comment.
Nit: CONTRIBUTING.md asks for Mock instead of hand-rolled counters. These nonlocal counters (also at :83 and :270) could be a Mock(side_effect=...) handler.
✍️ Drafted by Claude Code
| return random.uniform(1.0, 2.0) * (2**attempt) * to_seconds(min_delay) | ||
|
|
||
|
|
||
| def _record_batch_attempt_result( |
There was a problem hiding this comment.
Nit: This helper changes processed_requests and processed_unique_keys and also returns a tuple. A pure function that returns all its results would be easier to read.
✍️ Drafted by Claude Code
apify-factory
left a comment
There was a problem hiding this comment.
Looks good, nothing blocking.
🏭 Factory review — ℹ️ not auto-approved
This change was reviewed but cannot be auto-approved: a gate failed, so the review cannot end in an approval.
| check | result |
|---|---|
| Static gates | ❌ 12/16 — author-allowed, same-repo, max-lines, no-risky-content |
| Review findings | ✅ none |
| Reviewer escalation | ❌ Adds new parameters to the public batch_add_requests signature and changes default runtime behavior (3 retries with sleeps of several seconds by default), which a maintainer should sign off on. |
Juror 1 — claude-sonnet-5-5, adversarial |
⏭️ skipped — a gate failed |
Why it was not auto-approved
Failed gates:
author-allowed: cobanfurkanx: not a member of apify/product-engineeringsame-repo: head repo is cobanfurkanx/apify-client-pythonmax-lines: 363 changed lines: over the limit 200 (+304/-59)no-risky-content: src/apify_client/_resource_clients/request_queue.py: network call (network-call)
The comments above are the review itself; this section only explains the approval decision. Push a change and request @apify-factory again to re-run it, or ask a human to review.
Reviewed 867e5e0bd · Review details on the dashboard · workflow run
|
Thanks for the detailed review @vdusek! Addressed all feedback in
|
Summary
DEFAULT_UNPROCESSED_RETRIES_BATCH_ADD_REQUESTS = 3andDEFAULT_MIN_DELAY_BETWEEN_UNPROCESSED_REQUESTS_RETRIES = timedelta(milliseconds=500)insrc/apify_client/_consts.py.RequestQueueClient.batch_add_requests()and asyncRequestQueueClientAsync.batch_add_requests()(along with_batch_add_requests_worker,_serialize_requests,_compute_retry_delay_seconds, and_record_batch_attempt_result) insrc/apify_client/_resource_clients/request_queue.pyto retryunprocessed_requestswith exponential backoff while preserving non-breaking default behavior and recording rate-limit rounds viaself._http_client.stats.add_rate_limit_error(attempt + 1).test_batch_processed_partially_{async,sync}and addtest_batch_add_requests_retries_unprocessed_requests_{async,sync}intests/unit/test_client_request_queue.py.Closes #1134
Verification
pytest tests/unit/test_client_request_queue.py tests/unit/test_statistics.py: 63 passed ([impit]and[httpx2])poe lint(ruff format --check && ruff check): 249 files formatted, all checks passedty check src tests/unit/test_client_request_queue.py&ty check --python-platform linux: all checks passedpoe check-docstrings: async and sync method docstrings are in sync