Skip to content

Decides which process runs each view model.

Replicas that share an event store would otherwise all run the same view models and race on their checkpoints. ViewModel.layer takes a lease on each view model before running it: the process that holds the lease runs it, and the others wait as standbys until it is given up.

Defined in: src/Leases.ts:21

  • Shape<"effect-cqrs/Leases", { acquire(name): Effect<Lease, never, Scope>; }, this>

new Leases(_): Leases

Defined in: node_modules/.pnpm/effect@4.0.6/node_modules/effect/dist/Context.d.ts:121

never

Leases

Context.Service<Leases, { /** * Competes for the lease called `name` until the scope closes, which gives it * up. The first attempt is made before this returns. */ acquire(name: string): Effect.Effect<Lease, never, Scope.Scope> }>()("effect-cqrs/Leases").constructor

Defined in: src/Leases.ts:14

readonly changes: Stream<boolean>

Defined in: src/Leases.ts:18

Emits whether the lease is held, then every change.

readonly held: Effect<boolean>

Defined in: src/Leases.ts:16

Whether this process holds the lease now.


Defined in: src/Leases.ts:39

readonly optional interval?: Input

Defined in: src/Leases.ts:44

How often a standby tries to take a lease over, and the holder checks that its connection is alive. Defaults to 5 seconds.

readonly optional prefix?: string

Defined in: src/Leases.ts:46

Prefixes the names, so that several applications can share a database. Defaults to effect-cqrs/.

const layerMemory: Layer.Layer<Leases>

Defined in: src/Leases.ts:37


const makePostgres: (…args) => Effect<{ acquire(name): Effect<Lease, never, Scope>; }, never, Scope | SqlClient>

Defined in: src/Leases.ts:70

Leases on PostgreSQL advisory locks, pg_try_advisory_lock on a hash of the name. The locks belong to one connection that the service reserves from the client’s pool for as long as anything competes, so a process holds them until it gives them up or its connection closes: when it dies, the database releases them and a standby takes over at its next attempt.

A holder whose connection fails stops at its next check, up to interval later, while a standby may already have started: handlers can run in two processes for that long.

…[options?: PostgresOptions]

Effect<{ acquire(name): Effect<Lease, never, Scope>; }, never, Scope | SqlClient>


const memory: Leases["Service"]

Defined in: src/Leases.ts:35

Leases that are always held. It is the default of ViewModel.layer, for a single process or for view models that keep their data in that process.

layerPostgres(options?): Layer<Leases, never, SqlClient>

Defined in: src/Leases.ts:156

Leases on PostgreSQL advisory locks, see makePostgres.

PostgresOptions

Layer<Leases, never, SqlClient>