feat(utils): retry transient upstream errors in rest_query/post_query#261
Open
Elarwei001 wants to merge 2 commits into
Open
feat(utils): retry transient upstream errors in rest_query/post_query#261Elarwei001 wants to merge 2 commits into
Elarwei001 wants to merge 2 commits into
Conversation
Ensembl REST and other upstream services occasionally return transient 5xx (500/502/503/504) or 429 responses, or drop the connection under load. Every Ensembl-backed module (seq, ref, search, info, blat, and enrichr with ensembl=True) routes through rest_query/post_query, so a single transient blip currently fails the whole call. Add a small _query_with_retry helper and route both functions through it: - Retries on 429/5xx and on connection/timeout errors, with linear backoff (2s, 4s, 6s). - Honors a Retry-After header when the server sends one (capped at 30s). - Does NOT retry 4xx client errors (e.g. 404) — those are returned to the caller unchanged, preserving existing error handling. - After exhausting retries, returns the last response (or re-raises the last connection error) so callers behave exactly as before. Add offline unit tests (TestQueryRetry) covering success-after-retry, connection-error retry, no-retry on 404, retry exhaustion, and Retry-After handling. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Contributor
Author
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #261 +/- ##
==========================================
- Coverage 56.70% 56.66% -0.05%
==========================================
Files 29 29
Lines 9392 9417 +25
==========================================
+ Hits 5326 5336 +10
- Misses 4066 4081 +15 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Add `--durations=25` to pytest addopts so every CI run prints the slowest tests at the end of the log. This makes it easy to spot which (mostly network-bound) tests dominate CI wall-clock time, complementing the request-retry changes in this PR. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Scope
rest_query/post_queryingget/utils.pyare the shared entry points for every Ensembl-backed module (seq,ref,search,info,blat, andenrichrwithensembl=True). They currently issue a single request, so any transient hiccup from the upstream service fails the whole call. Ensembl REST in particular returns intermittent500/502/503/504or429under load, and occasionally drops the connection — which shows up as flaky failures in CI and for users.Change
Add a small
_query_with_retryhelper and route both functions through it:429and5xx(500/502/503/504) and on connection/timeout errors, with linear backoff (2s, 4s, 6s; 4 attempts total).Retry-Afterwhen the server sends one (capped at 30s).4xxclient errors (e.g.404) — returned to the caller unchanged, so existing error handling (if not r.ok: raise ...) is preserved.No public API or return types change — this only adds resilience underneath the existing functions.
Tests
Offline unit tests (
tests/test_utils.py::TestQueryRetry, mockedrequests/sleep— no network) cover: success after retry, connection-error retry, no-retry on404, retry exhaustion, andRetry-Afterhandling.Notes
utils.pyalready has anhttp_jsonretry helper, but it always parses JSON and raises on non-JSON bodies.rest_querycan return either text or JSON and needs the rawResponse, so this helper returns theResponseobject and leaves body handling to the existing call sites.