Singleton
Register effects that should have one active owner in an Effect cluster.
Singleton.make wraps Sharding.registerSingleton in a Layer. When the local runner owns the shard for the singleton's name and shard group, sharding starts the effect; when ownership moves or the layer scope closes, the running fiber is interrupted and the registration is removed.
Creates a layer that registers a singleton effect with
Shardingunder the specified name and optional shard group.When to use
Use to register a cluster-wide background effect as a
Layer, so the effect is started only by the runner that currently owns the singleton's shard.Details
The returned layer requires
Shardingand the services needed byrun, except forScope. The registration is scoped to the layer; closing the layer removes the singleton registration and stops the singleton fiber if it is running.Gotchas
- Registering the same singleton name in the same shard group more than once dies during registration. - A
runeffect that completes normally is kept alive until the registration scope closes or shard ownership moves. - Failures fromrunare converted to defects, so handle expected failures insiderunwhen the singleton should keep running.See
Shardingfor the lower-level service that registers singletons and manages shard ownership