fix: bump redis floor to 5.0.1, disconnect pool on failed connect(), doc/type nits (redis-5/6/7/8); v0.1.4

redis-5: redis>=5 admitted 5.0.0, which lacks async aclose() and crashes on
teardown; floor bumped to >=5.0.1 (confirmed 5.0.1 is where aclose() lands).
redis-6: connect()/__aenter__ now disconnects the pool before re-raising on a
failed ping, so a discarded RedisDB from a failed `async with` doesn't hold a
live pool. redis-7: docstring/README conflated repo name (redis) with
distribution name (redis_store). redis-8: pool_timeout hinted Optional[float]
to match that None is accepted (wait forever). Also compresses the module and
__init__ docstrings (no behavior change), keeping the SSLConnection-mapping
and pool-disconnect-on-failure notes.

Signed-off-by: disqualifier <dev@disqualifier.me>
This commit is contained in:
2026-07-02 23:34:18 -04:00
parent 4688dd4181
commit 982809a515
4 changed files with 48 additions and 46 deletions
+11 -8
View File
@@ -4,7 +4,7 @@ Async Redis wrapper over redis-py's asyncio client — a small, config-free, **f
key/value + hash + ttl surface with a raw escape hatch for everything else. First of the key/value + hash + ttl surface with a raw escape hatch for everything else. First of the
datastore trio (`redis` / `psql` / `mysql`), a sibling of the `mongo` lib. datastore trio (`redis` / `psql` / `mysql`), a sibling of the `mongo` lib.
> **Import name ≠ repo name.** The repo/distribution is **`redis`**, but you import > **Import name ≠ repo name.** The repo is **`redis`**, the distribution is
> **`redis_store`** — the driver package owns the `redis` import namespace, so the lib > **`redis_store`** — the driver package owns the `redis` import namespace, so the lib
> can't also be `redis`. Install resolves `redis.git`; code does > can't also be `redis`. Install resolves `redis.git`; code does
> `from redis_store import RedisDB`. > `from redis_store import RedisDB`.
@@ -14,19 +14,19 @@ datastore trio (`redis` / `psql` / `mysql`), a sibling of the `mongo` lib.
`requirements.txt`: `requirements.txt`:
``` ```
redis_store @ git+ssh://git@git.rethinkstudios.io/rethink-public/redis.git@v0.1.3 redis_store @ git+ssh://git@git.rethinkstudios.io/rethink-public/redis.git@v0.1.4
``` ```
Direct: Direct:
```bash ```bash
pip install "redis_store @ git+ssh://git@git.rethinkstudios.io/rethink-public/redis.git@v0.1.3" pip install "redis_store @ git+ssh://git@git.rethinkstudios.io/rethink-public/redis.git@v0.1.4"
``` ```
Pulls `redis>=5` (redis-py, which ships the asyncio client — **not** the dead standalone Pulls `redis>=5.0.1` (redis-py, which ships the asyncio client — **not** the dead standalone
`aioredis`). `aioredis`; `5.0.1` is the floor because `5.0.0` lacks the async client's `aclose()`).
Drop the `@v0.1.3` suffix from the line above to install the latest unpinned. Drop the `@v0.1.4` suffix from the line above to install the latest unpinned.
## Usage ## Usage
@@ -55,6 +55,8 @@ async with RedisDB(host="localhost") as kv:
``` ```
One client/pool per process — build it once, attach it to your app (`app.kv = ...`), share it. One client/pool per process — build it once, attach it to your app (`app.kv = ...`), share it.
A failed `connect()`/`__aenter__` ping disconnects the pool before re-raising, so a
discarded `RedisDB` from a failed `async with` doesn't hold a live pool.
## Type contract ## Type contract
@@ -63,8 +65,9 @@ One client/pool per process — build it once, attach it to your app (`app.kv =
Counters and counts (`incr`/`decr`/`exists`/`ttl`) always return `int` regardless. Counters and counts (`incr`/`decr`/`exists`/`ttl`) always return `int` regardless.
Pool sizing/timeout are `max_connections=` and `pool_timeout=` (how long a caller waits for Pool sizing/timeout are `max_connections=` and `pool_timeout=` (how long a caller waits for
a free connection when the pool is saturated). Pass the driver's own `timeout=` in a free connection when the pool is saturated; `Optional[float]`, `None` waits forever).
`pool_kwargs` and construction raises a clear `ValueError` — use `pool_timeout=`. Pass the driver's own `timeout=` in `pool_kwargs` and construction raises a clear
`ValueError` — use `pool_timeout=`.
`ssl=True` maps to `connection_class=redis.asyncio.SSLConnection`, so TLS works via the `ssl=True` maps to `connection_class=redis.asyncio.SSLConnection`, so TLS works via the
documented `pool_kwargs` path (the default connection class has no `ssl` parameter and documented `pool_kwargs` path (the default connection class has no `ssl` parameter and
+2 -2
View File
@@ -4,11 +4,11 @@ build-backend = "hatchling.build"
[project] [project]
name = "redis_store" name = "redis_store"
version = "0.1.3" version = "0.1.4"
description = "async redis wrapper over redis-py asyncio with a raw escape hatch, fail-loud and config-free" description = "async redis wrapper over redis-py asyncio with a raw escape hatch, fail-loud and config-free"
requires-python = ">=3.10" requires-python = ">=3.10"
dependencies = [ dependencies = [
"redis>=5", "redis>=5.0.1",
] ]
[tool.hatch.build.targets.wheel] [tool.hatch.build.targets.wheel]
+3 -1
View File
@@ -1,3 +1,5 @@
from .redis_store import RedisDB from .redis_store import RedisDB
__all__ = ["RedisDB"] __version__ = "0.1.4"
__all__ = ["RedisDB", "__version__"]
+32 -35
View File
@@ -12,30 +12,26 @@ context manager:
async with RedisDB(host="localhost") as kv: async with RedisDB(host="localhost") as kv:
await kv.incr("hits") await kv.incr("hits")
lifecycle: lifecycle: construction is sync, opens no socket (pool connects lazily). connect()
construction is sync and opens no socket (the pool connects lazily). connect() pings so bad host/port/auth fails loud immediately, returns self. close() tears
issues a ping() so a bad host/port/auth fails loud immediately rather than on the down client + pool (client.aclose() + pool.disconnect()). a failed connect()/
first real op, and returns self for the one-liner above. close() tears the client __aenter__ ping also disconnects the pool before re-raising, so a discarded
and pool down (await client.aclose() + pool.disconnect()). RedisDB from a failed `async with` doesn't hold a live pool.
type contract: type contract: decode_responses=True by default -> str values, None for absent key.
decode_responses=True by default, so keys and string values come back as str (and decode_responses=False for raw bytes. counters (incr/decr/exists/ttl) always int.
None for an absent key). pass decode_responses=False at construction if you need raw
bytes. counters (incr/decr/exists/ttl) always return int regardless of this flag.
errors (FAIL LOUD — unlike the mongo lib's swallow-and-default): errors (FAIL LOUD — unlike mongo's swallow-and-default): every wrapped method catches
every wrapped method catches the driver's RedisError, logs it via RedisError, logs via getLogger(__name__), re-raises. None/[]/{} is only ever a
getLogger(__name__), and re-raises. a None/[]/{} return is only ever a real result real result, never a swallowed failure. unwrapped ops (pipelines, pub/sub, scan,
(absent key, empty hash) — never a swallowed failure. for anything not wrapped Lua, ...) go through the raw `.client` escape hatch: full driver surface, raises.
(pipelines, pub/sub, scan, Lua, ...) use the raw `.client` escape hatch, which
exposes the full redis.asyncio.Redis surface and raises untouched.
notes: notes:
- import is `redis_store`; the repo/distribution is `redis` (the driver owns the - import is `redis_store`; repo is `redis`, distribution is `redis_store` (the
`redis` import name, so the package can't also be `redis`) driver owns the `redis` import name, so the package can't also be `redis`)
- pub/sub and pipeline helpers are intentionally not wrapped yet — use `.client` - pub/sub and pipeline helpers intentionally not wrapped yet — use `.client`
- ssl=True maps to connection_class=redis.asyncio.SSLConnection so TLS actually - ssl=True maps to connection_class=redis.asyncio.SSLConnection so TLS works via
works via the documented pool_kwargs path; pass ssl-prefixed kwargs alongside it the documented pool_kwargs path; pass ssl-prefixed kwargs alongside it
(ssl_ca_certs, ssl_certfile, ...) to configure the handshake (ssl_ca_certs, ssl_certfile, ...) to configure the handshake
""" """
@@ -59,32 +55,32 @@ class RedisDB:
password: Optional[str] = None, password: Optional[str] = None,
*, *,
max_connections: int = 10, max_connections: int = 10,
pool_timeout: float = 30.0, pool_timeout: Optional[float] = 30.0,
decode_responses: bool = True, decode_responses: bool = True,
**pool_kwargs, **pool_kwargs,
): ):
"""build the (not-yet-connected) pool + client; no I/O happens here """build the (not-yet-connected) pool + client; no I/O happens here
host/port/db/password/max_connections are injected by the caller. extra host/port/db/password/max_connections injected by the caller. extra
pool_kwargs pass through to the connection pool (socket_timeout, pool_kwargs pass through to the connection pool (socket_timeout,
socket_connect_timeout, ssl, etc). decode_responses=True returns str; set it socket_connect_timeout, ssl, etc). decode_responses=True returns str, False
False for raw bytes. raw bytes.
a BlockingConnectionPool is used so that, under concurrency exceeding a BlockingConnectionPool is used so callers WAIT up to pool_timeout seconds
max_connections, callers WAIT up to pool_timeout seconds for a free connection for a free connection under concurrency exceeding max_connections (matching
(matching motor/mongo's blocking pool) rather than the plain pool's behavior of motor/mongo's blocking pool), instead of raising MaxConnectionsError
raising MaxConnectionsError immediately. pool_timeout=None waits forever. immediately. pool_timeout=None waits forever.
`timeout` is the pool's own kwarg (set from pool_timeout here), so passing it in `timeout` is the pool's own kwarg (filled from pool_timeout here); passing it
pool_kwargs would collide reject it with a clear pointer rather than let the in pool_kwargs would collide, so it's rejected with a clear ValueError instead
driver raise a cryptic "multiple values for 'timeout'". of a cryptic driver "multiple values for 'timeout'".
`ssl=True` is mapped to `connection_class=redis.asyncio.SSLConnection` — the `ssl=True` maps to `connection_class=redis.asyncio.SSLConnection` — the
default async Connection class has no `ssl` parameter, so passing it straight default async Connection class has no `ssl` parameter, so passing it straight
through pool_kwargs would defer the failure to connect() as a raw TypeError. through pool_kwargs would defer the failure to connect() as a raw TypeError.
pass ssl-prefixed kwargs (ssl_ca_certs, ssl_certfile, ...) alongside ssl=True to pass ssl-prefixed kwargs (ssl_ca_certs, ssl_certfile, ...) alongside ssl=True
configure the TLS connection; an explicit connection_class in pool_kwargs is to configure TLS; an explicit connection_class in pool_kwargs is left
left untouched. untouched.
""" """
if "timeout" in pool_kwargs: if "timeout" in pool_kwargs:
raise ValueError( raise ValueError(
@@ -115,6 +111,7 @@ class RedisDB:
await self._client.ping() await self._client.ping()
except RedisError: except RedisError:
log.exception("redis.connect() ping failed") log.exception("redis.connect() ping failed")
await self._pool.disconnect()
raise raise
return self return self