@openknot-ts/redis-client
v0.0.6-preview
Published
Redis client resources, connection watch and health check for OpenKnot applications, over node-redis.
Readme
@openknot-ts/redis-client
Manages application-created node-redis command and subscription clients as a Wire resource, with a connection watch and a health check. Types derive from the SDK.
Usage
const redis = redisClient({
requirements: { settings },
create: ({ settings }) => ({
commands: createClient({ url: settings.REDIS_URL }),
subscriber: createClient({ url: settings.REDIS_URL }),
}),
});API
| Export | Description |
| --- | --- |
| redisClient({ requirements?, create }) | The clients as a resource. Resolves to them plus connection. |
| redisHealthCheck({ redis, name?, timeoutMs? }) | The subscriber client must be ready and PING must answer PONG within the check's deadline (one second by default). |
| redisConnectionOf(client) | A ConnectionWatch over a client this resource does not manage. |
| RedisConnections, RedisCommands, RedisSubscriber | Types. |
Lifecycle
- Both clients connect at acquisition. A client's first error before it is ready fails startup; once ready, its native reconnection applies.
- Once ready, a client's first error of each outage goes to
unattendedFailuresfrom@openknot-ts/error-reporting/wire, which logs it asopenknot.redis failedand reports it. The attributeopenknot.redis.clientnames the client,commandsorsubscriber. The errors of its reconnection attempts are not reported again until it is ready once more. - A failed or cancelled startup destroys both. Release closes each once: a ready client first receives the replies to the commands it already sent, a client that is not ready is destroyed at once, and a connection that fails while closing ends the release with its error.
- node-redis's command
timeoutonly covers sending. Setsocket.socketTimeoutwithpingIntervalso a server that stops answering cannot hold a command, or the application's stop. - Consumers share the resource through
requirements; cache adapters never close it.
Connection
connection holds a ConnectionWatch from @openknot-ts/wire per client, connection.commands and connection.subscriber, which @openknot-ts/cache, @openknot-ts/replication and the outbox accept. Work listens to the client it uses:
export const replicaStorage = redisReplicaStore({
requirements: { redis },
commands: ({ redis }) => redis.commands,
connection: ({ redis }) => redis.connection.commands,
});
export const replicaChannel = redisReplicationChannel({
requirements: { redis },
connections: ({ redis }) => redis,
connection: ({ redis }) => redis.connection.subscriber,
});Each watch's watch({ lost, restored }) returns an unsubscribe. It follows its own client: reconnecting or end reports lost once, the next ready reports restored.
Tests
test/resource.test.ts runs without a server. With REDIS_URL set, pnpm --filter @openknot-ts/redis-client test:redis runs test/redis.integration.ts.
