IsolateIdentity class
Which isolate of the app this code is running in.
An app with AppConfig.workers greater than 1 runs the same program in
several isolates. They are identical by construction, which is the point —
and also the problem, because some things an app talks to identify their
clients by name. A message broker that tracks unacknowledged work per
consumer name will hand every worker the same name, and each worker's
pending messages become invisible to the others. Telling the isolates apart
requires knowing which one you are.
Statics in Dart are per-isolate. Each isolate gets its own copy of
top-level and static state — nothing is shared, and there is no race to
guard against. That is not a caveat, it is the whole mechanism: a static
can describe "which isolate am I" precisely because every isolate has a
different one. It is the same reason the private _revaliIsWorker flag in
the generated server file works.
Read it from anywhere, including code that has no way to be handed
configuration — an AppConfig.createBroker override, for instance, takes
no arguments.
The common use is work that must happen once for the process rather than once per isolate:
@override
Future<void> configureDependencies(DI di) async {
if (!IsolateIdentity.current.isWorker) {
di.registerSingleton<NightlyReport>(NightlyReport()..start());
}
}
Note that RedisBroker names its own consumers from this — appending
index to the name it was given — so an app does not have to work the
index into consumerName itself. Doing so anyway produces orders-1-1.
Unset, it describes the parent of a single-isolate app: index 0, not a
worker, one isolate in total. So a unit test, a dart test run, or an app
that never spawns workers observes something true without configuring
anything, and there is no null to handle.
Constructors
- IsolateIdentity({required int index, required int workerCount})
-
const
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- index → int
-
Position in the fleet:
0is the parent,1..workerCount - 1a worker.final - isWorker → bool
-
Whether this is a spawned worker rather than the parent.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- workerCount → int
-
How many isolates the app was configured for —
AppConfig.workers.final
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
override
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Properties
- current → IsolateIdentity
-
The identity of the isolate this code is running in.
no setter
Static Methods
-
setCurrentForGeneratedCode(
IsolateIdentity identity) → void - Framework plumbing — called by generated code, not by app authors.
Constants
- single → const IsolateIdentity
- The parent isolate of an app that runs in one isolate.