Repository navigation
Cache /historical per symbol; 503 + Retry-After on PSX rate limits - #5
Merged
Merged
Conversation
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.
Summary
On 2026-09-29 (11:57–12:32 UTC)
GET /stocks/{symbol}/historicalreturned 324 × 500. One client was crawling ~210 symbols at 2–3 req/s. The combined traffic from our single egress IP made PSX answer 429, andPSXRateLimitErrorhad no handler, so the catch-all turned it into a 500.This PR:
/historicalper symbol;PSXRateLimitErrorto503 psx_unavailablewithRetry-After: 60on every endpoint.Behaviour
start/endrange, sliced in the API. The SDK is called withcache=False, which makes this layer the only cache.X-Cache: HIT | MISS | STALE.Ageis sent on HIT and STALE.meta.cachedis true on HIT and STALE.REDIS_URL, e.g. Aiven for Valkey).REDIS_URLthe cache is in-memory only.start/endmust beYYYY-MM-DD. Invalid dates andstart > endnow return422 bad_requestinstead of 500.null, and a null volume gives 502.Configuration
REDIS_URLfastapi cloud env. Only the hostname is ever logged.HISTORICAL_CACHE_MARKET_TTL1800Release notes
sync-openapi.ymlnow runs onv*tags instead of every push tomain, so the public docs update only when a release is made.v0.3.0. Thepsxdata-docsupdate follows the release.Testing
139 tests pass (60 existing + 79 new); ruff and mypy are clean. New test modules:
tests/api/test_cache_freshness.py: window edges, weekends, UTC.tests/api/test_cache_codec.py: round-trip, malformed envelopes.tests/api/test_cache_store.py: LRU, fail-open Redis with fakeredis, tiering.tests/api/test_cache_historical.py: hit, miss, expiry, stale-on-429 and cooldown, stale-on-unavailable, single-flight, restart via L2.tests/api/test_cache_factory.py: env parsing, badREDIS_URLwithout leaking the secret, lifespan.tests/api/test_historical_endpoint.py: headers, slicing, 422, 503, Redis down, OpenAPI.A local smoke test against live PSX gave MISS, then HIT, then 422 on
start=20260920.