Conversation
Deployments whose connection parameters are not static environment variables cannot use the settings-built pool: Sentinel-managed masters (SentinelConnectionPool), passwords issued at runtime by a secret manager, or any pool needing programmatic construction. Today the only way in is reaching into the private _PoolState, which pins consumers to internals. This adds one public seam: - FastAPIRedis(app).lifespan(pool_factory=...) — a zero-arg sync or async callable returning the redis.asyncio.ConnectionPool to use. - Plain FastAPI(lifespan=redis_lifespan) users can set app.state.redis_pool_factory directly. The lifespan owns the returned pool and closes it on shutdown, same as the settings-built one. Cluster mode raises ValueError rather than silently ignoring the override. Every caching / rate-limiting dependency is unchanged — they already resolve through the pool state. Includes unit tests: sync factory, async factory, state-attribute path, cluster refusal, and a regression guard for the default path.
tishun
self-requested a review
August 19, 2026 12:40
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.
Motivation
Deployments whose connection parameters are not static environment variables cannot use the settings-built pool:
SentinelConnectionPoolbuilt fromSentinel.master_for(...), which noREDIS_URL/KV setting can express.Today the only way in is reaching into the private
_PoolStateand assigningapp.state._redisfrom a custom lifespan — which works, but pins consumers to internals (we are running exactly that in production against 0.8.0 and would like to stop).Design
One public seam, no behavior change for existing users:
or, for plain
FastAPI(lifespan=redis_lifespan)users:redis.asyncio.ConnectionPoolto use (async matters: fetching a credential at startup is I/O).ValueErrorrather than silently ignoring the override.Tests
tests/unit/test_pool_injection.py: sync factory, async factory, state-attribute path, cluster refusal, and a regression guard for the default settings-built path. Full unit run: 108 passed.🤖 Generated with Claude Code