Redis on redis-py (the pyfect-dev[redis] extra), Effect's
NodeRedis::
layer = redis_py.layer("redis://localhost:6379")
program.pipe(fx.provide(persistence.layer_redis.pipe(fx.provide(layer))))The layer connects when it is built (failing with RedisError if the
server cannot be reached, without retrying) and closes the client when its
scope closes. It provides Redis and RedisPy, whose client is
the redis.asyncio.Redis itself and use(f) runs f(client).
The client runs on an asyncio loop on a thread of its own and answers raw
replies (no redis-py response callbacks), decoded as text unless
decode_responses=False is passed. reserve opens connections of its
own that do not reconnect: run blocking commands there, as a blocking
command on the shared client holds a connection of its pool.
Interrupting subscribe or reserve while its connection is opening
(or SUBSCRIBE is unanswered) cancels the attempt and closes it.
Re-exports#
Classes#
Functions#
make#
make(url=None, *, pool=None, **options) -> Effect[Context[Redis | RedisPy], RedisError, Scope]A redis-py client for url (or options, its keyword
arguments), connected now and closed with the scope, as Redis and
RedisPy. pool sizes the connections behind reserve.
| Parameter | Type | Description |
|---|---|---|
url | str | None = None | |
pool | PoolOptions | None = None | |
options | Any = {} |
Returns Effect[Context[Redis | RedisPy], RedisError, Scope]
def make(
url: str | None = None, *, pool: PoolOptions | None = None, **options: Any
) -> Effect[Context[Redis | RedisPy], RedisError, Scope]:
"""A redis-py client for ``url`` (or ``options``, its keyword
arguments), connected now and closed with the scope, as ``Redis`` and
``RedisPy``. ``pool`` sizes the connections behind ``reserve``."""
def build() -> Generator[Effect[Any, RedisError, Any], Any, Context[Redis | RedisPy]]:
loop = yield from acquire_release(sync(_Loop), lambda loop, _: loop.stop())
client = yield from acquire_release(
loop.run(lambda: _make_client(url, dict(options)), waits_for="work"),
lambda client, _: loop.run(client.aclose, waits_for="work").ignore(),
)
async def probe() -> None:
connection = await _open_connection(client)
await connection.disconnect()
yield from loop.run(probe, waits_for="work")
# redis-py leaves some of these signatures untyped.
untyped: Any = client
def send(command: str, /, *args: str) -> Effect[RedisReply, RedisError]:
async def run() -> RedisReply:
return cast("RedisReply", await untyped.execute_command(command, *args))
return loop.run(run, waits_for=_command_waits_for(command, args))
async def connect() -> OpenConnection:
reserved = _Reserved(loop, await _open_connection(client))
return OpenConnection(reserved.send, reserved.close)
def subscribe(
channel: str, on_message: Callable[[RedisMessage], bool]
) -> Effect[Effect[None, RedisError], RedisError, Scope]:
async def make_subscription() -> PubSub:
subscription: PubSub = untyped.pubsub()
return subscription
def connected(subscription: PubSub) -> Effect[PubSub, RedisError]:
# Only making the subscriber is uninterruptible, so a stalled
# connection or ``SUBSCRIBE`` can be interrupted; the scope
# closes it.
async def open_subscription() -> PubSub:
await subscription.subscribe(channel)
return subscription
return loop.run(open_subscription, waits_for="work")
def deliver(subscription: PubSub) -> Effect[None, RedisError]:
async def next_message() -> Mapping[str, Any] | None:
reading: Any = subscription
message: Mapping[str, Any] | None = await reading.get_message(
ignore_subscribe_messages=True, timeout=None
)
return message
def body() -> Generator[Effect[Any, RedisError], Any]:
while True:
message = yield from loop.run(next_message, waits_for="event")
if message is None or message["type"] != "message":
continue
if not on_message(RedisMessage(message["channel"], message["data"])):
return
return gen(body)
return (
acquire_release(
loop.run(make_subscription, waits_for="work"),
lambda subscription, _: loop.run(
subscription.aclose, waits_for="work"
).ignore(),
)
.flat_map(connected)
.map(deliver)
)
# The pool opens connections uninterruptibly; opening one can be
# interrupted (and the attempt is cancelled) all the same.
redis = yield from make_redis(
send=send,
subscribe=subscribe,
connect=loop.run(connect, waits_for="work").interruptible(),
pool=pool,
)
return Context.empty().add(Redis, redis).add(RedisPy, RedisPyService(client, loop))
return gen(build)layer#
layer(url=None, *, pool=None, **options) -> Layer[Redis | RedisPy, RedisError]Provides Redis and RedisPy (see make).
| Parameter | Type | Description |
|---|---|---|
url | str | None = None | |
pool | PoolOptions | None = None | |
options | Any = {} |
Returns Layer[Redis | RedisPy, RedisError]
def layer(
url: str | None = None, *, pool: PoolOptions | None = None, **options: Any
) -> Layer[Redis | RedisPy, RedisError]:
"""Provides ``Redis`` and ``RedisPy`` (see ``make``)."""
return effect_context(make(url, pool=pool, **options))layer_config#
layer_config(options) -> Layer[Redis | RedisPy, RedisError | ConfigError]layer with its options read from the ConfigProvider: a
Config of them, or a mapping from option names (url, pool
and redis-py's) to their Config (Effect's Config.Wrap).
| Parameter | Type | Description |
|---|---|---|
options | Config[Mapping[str, Any]] | Mapping[str, Any] |
Returns Layer[Redis | RedisPy, RedisError | ConfigError]
def layer_config(
options: Config[Mapping[str, Any]] | Mapping[str, Any],
) -> Layer[Redis | RedisPy, RedisError | ConfigError]:
"""``layer`` with its options read from the ``ConfigProvider``: a
``Config`` of them, or a mapping from option names (``url``, ``pool``
and redis-py's) to their ``Config`` (Effect's ``Config.Wrap``)."""
read: Effect[Mapping[str, Any], ConfigError] = unwrap(options)
built: Effect[Context[Redis | RedisPy], RedisError | ConfigError, Scope] = read.flat_map(
lambda fields: make(**fields)
)
return effect_context(built)