ShardRegion
Este conteúdo não está disponível em sua língua ainda.
Defined in: src/cluster/sharding/ShardRegion.ts:113
ShardRegion is the node-local router for a sharded type. It talks to the ShardCoordinator to discover the home of each shard, hosts a Shard actor for every shard that lives locally, and forwards everything else to the remote region that owns the target shard. Messages whose shard home is unknown or in handoff are buffered until the coordinator answers.
Entities are grandchildren, not children: the region routes to a shard and
the shard owns the entity actors. What the region deliberately keeps is
the passivation policy — the idle sweep, the maxEntities LRU, and the
shard-level sweep that stops a shard once it has stood empty long enough.
It sees every message on this node, so it is the only place where a
node-wide entity cap can be enforced; a shard only ever sees its own slice.
Deciding here is also what makes stopping a shard loss-free. The region marks the shard and stops routing to it in the same synchronous step as it sends the stop, so nothing it routes can land in a mailbox that is already draining — a shard that timed itself out would have to tell the region afterwards, and everything in that gap would be dropped.
Extends
Section titled “Extends”Actor<TMessage|ShardingMessage|Terminated|Passivate>
Type Parameters
Section titled “Type Parameters”TMessage
Section titled “TMessage”TMessage = unknown
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new ShardRegion<
TMessage>(config):ShardRegion<TMessage>
Defined in: src/cluster/sharding/ShardRegion.ts:159
Parameters
Section titled “Parameters”config
Section titled “config”ShardRegionConfig<TMessage>
Returns
Section titled “Returns”ShardRegion<TMessage>
Overrides
Section titled “Overrides”Properties
Section titled “Properties”config
Section titled “config”
readonlyconfig:ShardRegionConfig<TMessage>
Defined in: src/cluster/sharding/ShardRegion.ts:159
Methods
Section titled “Methods”displayName()
Section titled “displayName()”displayName():
string
Defined in: src/Actor.ts:192
Human-readable name for this actor in log lines and in the DevTools actor tree (#891). Defaults to the full path — which is already the log source, so an actor that doesn’t override this logs exactly as it did before.
override displayName(): string { return `User(${this.entityId})`; }Purely cosmetic. The path stays the identity everywhere that routes,
correlates or aggregates — metric labels, tracing attributes, dead
letters, ActorRef.toString(), every wire identifier — so a display
name is free to be ambiguous, unstable, or shared between actors.
Resolved on every record, not captured once. Two consequences:
keep it cheap and side-effect free, and expect it to be called before
preStart (hence the optional chain — the context is attached after
construction). In exchange a name may be derived from state, and it
updates when that state does. Throwing, or returning anything but a
non-empty string, falls back to the path and warns once: a naming
hook must not be able to take a log line down with it.
ActorOptions.withDisplayName(...) outranks this, for the same reason
withSupervisorStrategy(...) outranks supervisorStrategy
— the spawn site is the more specific statement. It has to: every
Behaviors actor is a TypedActor that inherits this default, so a
method that won would silently swallow the spawn-site value for exactly
the actors that have no subclass to override. For a name that only
becomes known at runtime, this.context.setDisplayName(...) outranks
both.
Returns
Section titled “Returns”string
Inherited from
Section titled “Inherited from”onReceive()
Section titled “onReceive()”onReceive(
message):void
Defined in: src/cluster/sharding/ShardRegion.ts:225
Main message handler. Receives each envelope dequeued from the mailbox. A thrown error (sync or async) is caught by the supervisor.
Parameters
Section titled “Parameters”message
Section titled “message”Terminated | Passivate | ShardingMessage | TMessage
Returns
Section titled “Returns”void
Overrides
Section titled “Overrides”postRestart()
Section titled “postRestart()”postRestart(
_reason):void|Promise<void>
Defined in: src/Actor.ts:152
Called on the fresh instance after a restart. Default: call preStart().
Parameters
Section titled “Parameters”_reason
Section titled “_reason”Error
Returns
Section titled “Returns”void | Promise<void>
Inherited from
Section titled “Inherited from”postStop()
Section titled “postStop()”postStop():
void
Defined in: src/cluster/sharding/ShardRegion.ts:218
Called after the actor has been terminated. Children are already stopped.
Returns
Section titled “Returns”void
Overrides
Section titled “Overrides”preRestart()
Section titled “preRestart()”preRestart(
_reason,_message?):void|Promise<void>
Defined in: src/Actor.ts:119
Called before a restart, on the instance about to be thrown away.
The default calls postStop() and nothing else.
Override to release what the instance holds outside itself — a file handle, an open socket, a broker connection — or to do something other than drop the message that failed.
Stopping this actor’s children is not done here: the framework tears
them down after this hook returns and waits for them before building the
replacement, because postRestart re-runs preStart and a named child
needs its name back. To keep the children instead, see
Actor.stopChildrenOnRestart.
Parameters
Section titled “Parameters”_reason
Section titled “_reason”Error
_message?
Section titled “_message?”Terminated | Passivate | ShardingMessage | TMessage
Returns
Section titled “Returns”void | Promise<void>
Inherited from
Section titled “Inherited from”preStart()
Section titled “preStart()”preStart():
void
Defined in: src/cluster/sharding/ShardRegion.ts:188
Called after construction and before the first message is processed.
Returns
Section titled “Returns”void
Overrides
Section titled “Overrides”stopChildrenOnRestart()
Section titled “stopChildrenOnRestart()”stopChildrenOnRestart():
boolean
Defined in: src/Actor.ts:149
Whether a restart tears this actor’s children down before rebuilding it.
Default: true.
A restart replaces the Actor instance while the cell — and therefore
the child map — survives. Keeping the children was the old behaviour and
it made an ordinary pattern impossible: postRestart re-runs preStart,
so an actor that spawns a named child there hit Child name … is not unique on its first restart and never recovered (#634).
Override to false when the children are expensive to rebuild, hold
state the parent cannot restore, or are supervised independently — a
connection pool, say. They then outlive the restart exactly as before,
and it is on you to make preStart idempotent — by adopting the survivor
from the cell, this.child = this.context.child('name').toNullable() ?? this.context.spawn(Child, 'name'), or with context.spawnAnonymous.
An instance field cannot do it: preStart runs on a fresh instance
after every restart, so this.child ??= … is always unset and re-spawns
into the name the surviving child still holds, which fails the spawn and
restarts the actor again.
This is a separate hook rather than a preRestart override because the
teardown has to be awaited: the new instance cannot be built until the
old children are actually gone, and preRestart has no way to tell the
cell that it started something worth waiting for.
Returns
Section titled “Returns”boolean
Inherited from
Section titled “Inherited from”supervisorStrategy()
Section titled “supervisorStrategy()”supervisorStrategy():
SupervisorStrategy
Defined in: src/Actor.ts:160
Supervisor strategy for this actor’s children. Defaults to restart, up to 10 times per minute, then stop.
Returns
Section titled “Returns”Inherited from
Section titled “Inherited from”settingsToConfig()
Section titled “settingsToConfig()”
staticsettingsToConfig<TMessage>(s,cluster,localResolver):ShardRegionConfig<TMessage>
Defined in: src/cluster/sharding/ShardRegion.ts:161
Type Parameters
Section titled “Type Parameters”TMessage
Section titled “TMessage”TMessage
Parameters
Section titled “Parameters”ShardingOptionsType<TMessage>
cluster
Section titled “cluster”localResolver
Section titled “localResolver”(path) => ActorRef<unknown> | null
Returns
Section titled “Returns”ShardRegionConfig<TMessage>
