Fix rate limiter Redis fallback for healthcheck#191
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.
Problem
The
/healthendpoint returns HTTP 500 on every healthcheck because the slowapi rate limiter middleware attempts to connect toredis://redis:6379— the hardcoded internal Docker Compose hostname — when no Redis service exists in the deployment. The DNS lookup fails withredis.exceptions.ConnectionError: Error -2 connecting to redis:6379. Name or service not known, which propagates through the middleware stack and crashes the response. The previous guard (if os.getenv("RAILWAY_ENVIRONMENT") and redis_url.startswith("redis://redis:")) only fired when theRAILWAY_ENVIRONMENTvariable was present, leaving a gap for Railway deployments where that variable may not be set.Solution
Updated
_rate_limit_storage_uri()inbackend/app/middleware/rate_limiting.pyto unconditionally return"memory://"whenever the resolved Redis URL starts withredis://redis:, removing the dependency onRAILWAY_ENVIRONMENTbeing set. Also reordered the env var lookup to checkREDIS_URL(Railway's standard variable) beforeHYPERCODE_REDIS_URL, and fixed_with_redis_dbto return"memory://"instead of re-introducing the bare hostname as a fallback. When a real Redis URL is provided viaREDIS_URLorHYPERCODE_REDIS_URL, it is used as before; otherwise the limiter runs entirely in-memory, which is correct for single-instance deployments.Changes
backend/app/middleware/rate_limiting.pyGenerated by Railway