@kylewillett/troupe 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +21 -2
- package/README.md +132 -2
- package/dist/ask.d.ts +5 -1
- package/dist/ask.js +16 -0
- package/dist/backstage/index.d.ts +2 -1
- package/dist/backstage/index.js +1 -0
- package/dist/backstage/source.d.ts +1 -1
- package/dist/backstage/source.js +65 -2
- package/dist/backstage/types.d.ts +65 -4
- package/dist/backstage/types.js +2 -1
- package/dist/backstage-ui/assets/index-CG896RLZ.css +1 -0
- package/dist/backstage-ui/assets/index-DF_Xnc1u.js +9 -0
- package/dist/backstage-ui/index.html +2 -2
- package/dist/cell.d.ts +16 -14
- package/dist/cell.js +38 -13
- package/dist/checks.d.ts +10 -0
- package/dist/checks.js +34 -2
- package/dist/cluster/index.d.ts +6 -0
- package/dist/cluster/index.js +4 -0
- package/dist/cluster/leases.d.ts +37 -0
- package/dist/cluster/leases.js +132 -0
- package/dist/cluster/membership.d.ts +100 -0
- package/dist/cluster/membership.js +559 -0
- package/dist/cluster/memory.d.ts +31 -0
- package/dist/cluster/memory.js +187 -0
- package/dist/cluster/placement.d.ts +10 -0
- package/dist/cluster/placement.js +30 -0
- package/dist/cluster/postgres/index.d.ts +1 -0
- package/dist/cluster/postgres/index.js +3 -0
- package/dist/cluster/postgres/schema.d.ts +7 -0
- package/dist/cluster/postgres/schema.js +33 -0
- package/dist/cluster/postgres/schema.sql +18 -0
- package/dist/cluster/postgres/store.d.ts +28 -0
- package/dist/cluster/postgres/store.js +124 -0
- package/dist/cluster/region.d.ts +103 -0
- package/dist/cluster/region.js +843 -0
- package/dist/cluster/remoting.d.ts +79 -0
- package/dist/cluster/remoting.js +755 -0
- package/dist/cluster/shard-memory.d.ts +14 -0
- package/dist/cluster/shard-memory.js +92 -0
- package/dist/cluster/shard-store.d.ts +62 -0
- package/dist/cluster/shard-store.js +36 -0
- package/dist/cluster/shard.d.ts +54 -0
- package/dist/cluster/shard.js +278 -0
- package/dist/cluster/sharding.d.ts +109 -0
- package/dist/cluster/sharding.js +278 -0
- package/dist/cluster/table.d.ts +29 -0
- package/dist/cluster/table.js +74 -0
- package/dist/cluster/transport.d.ts +29 -0
- package/dist/cluster/transport.js +4 -0
- package/dist/cluster/wire.d.ts +90 -0
- package/dist/cluster/wire.js +177 -0
- package/dist/hash.d.ts +7 -0
- package/dist/hash.js +25 -0
- package/dist/internal.d.ts +134 -2
- package/dist/internal.js +12 -1
- package/dist/json.d.ts +44 -0
- package/dist/json.js +143 -0
- package/dist/node/cluster/frames.d.ts +31 -0
- package/dist/node/cluster/frames.js +73 -0
- package/dist/node/cluster/index.d.ts +1 -0
- package/dist/node/cluster/index.js +3 -0
- package/dist/node/cluster/tcp.d.ts +101 -0
- package/dist/node/cluster/tcp.js +416 -0
- package/dist/persistence/actor.js +1 -1
- package/dist/persistence/backend.d.ts +1 -10
- package/dist/persistence/backend.js +4 -34
- package/dist/persistence/postgres/index.d.ts +2 -1
- package/dist/persistence/postgres/index.js +1 -1
- package/dist/persistence/postgres/pool.d.ts +44 -0
- package/dist/persistence/postgres/pool.js +65 -0
- package/dist/persistence/postgres/postgres.d.ts +1 -33
- package/dist/persistence/postgres/postgres.js +4 -63
- package/dist/system.d.ts +1 -1
- package/dist/system.js +190 -24
- package/dist/types.d.ts +86 -3
- package/package.json +25 -4
- package/dist/backstage-ui/assets/index-Bg0MCiqs.css +0 -1
- package/dist/backstage-ui/assets/index-CeefBsDh.js +0 -9
- package/dist/persistence/json.d.ts +0 -7
- package/dist/persistence/json.js +0 -75
|
@@ -0,0 +1,755 @@
|
|
|
1
|
+
// Remoting (decision 0026): `tell` on a remote ref becomes a frame on a connection, and a frame
|
|
2
|
+
// becomes a `tell` on a local ref. One connection per pair of nodes, so messages from one node to
|
|
3
|
+
// one actor arrive in the order they were sent. At most once: nothing is acknowledged or retried,
|
|
4
|
+
// and an ask that loses its message or its reply times out (decision 0009).
|
|
5
|
+
//
|
|
6
|
+
// Each connection starts with a hello each way, and carries nothing else until both have
|
|
7
|
+
// arrived. When two nodes dial each other at once, the connection the lower address dialed wins,
|
|
8
|
+
// so the loser never carries a message. While a peer has no connection, its frames wait in a
|
|
9
|
+
// bounded buffer, and remoting dials again with backoff from the system's scheduler and random.
|
|
10
|
+
import { checkInt, checkKeys } from '../checks.js';
|
|
11
|
+
import { hooksOf } from '../internal.js';
|
|
12
|
+
import { Queue } from '../queue.js';
|
|
13
|
+
import { checkNodeId, createCodec, parseRefUri, } from './wire.js';
|
|
14
|
+
/** The remoting protocol's version, in every hello. A peer on another version is refused. */
|
|
15
|
+
export const PROTOCOL = 1;
|
|
16
|
+
const OPTIONS = new Set(['cluster', 'transport', 'bufferSize', 'maxFrameBytes', 'closeWithSystem']);
|
|
17
|
+
const BACKOFF_MIN_MS = 100;
|
|
18
|
+
const BACKOFF_MAX_MS = 5_000;
|
|
19
|
+
const UID_RANGE = 2 ** 32;
|
|
20
|
+
const DOWNED_FRAME = JSON.stringify({ t: 'downed' });
|
|
21
|
+
/**
|
|
22
|
+
* Starts remoting on `system`: listens on the transport, and makes refs to other nodes' actors
|
|
23
|
+
* work as local ones do.
|
|
24
|
+
* @throws TypeError for options that can't be used.
|
|
25
|
+
* @throws Error if the system already has remoting.
|
|
26
|
+
*/
|
|
27
|
+
export function startRemoting(system, opts) {
|
|
28
|
+
checkKeys(opts, OPTIONS, 'startRemoting');
|
|
29
|
+
const { cluster, transport, bufferSize = 10_000, maxFrameBytes = 1_048_576, closeWithSystem = true, } = opts;
|
|
30
|
+
if (!isTransport(transport)) {
|
|
31
|
+
throw new TypeError('startRemoting: transport must be a Transport');
|
|
32
|
+
}
|
|
33
|
+
checkInt(bufferSize, 'startRemoting: bufferSize', { min: 0 });
|
|
34
|
+
checkInt(maxFrameBytes, 'startRemoting: maxFrameBytes');
|
|
35
|
+
if (typeof closeWithSystem !== 'boolean') {
|
|
36
|
+
throw new TypeError('startRemoting: closeWithSystem must be a boolean');
|
|
37
|
+
}
|
|
38
|
+
const hooks = hooksOf(system);
|
|
39
|
+
const self = Object.freeze({
|
|
40
|
+
cluster,
|
|
41
|
+
address: transport.address,
|
|
42
|
+
uid: Math.floor(hooks.random() * UID_RANGE),
|
|
43
|
+
});
|
|
44
|
+
checkNodeId(self);
|
|
45
|
+
return new Node(system, hooks, self, transport, bufferSize, maxFrameBytes, closeWithSystem).api;
|
|
46
|
+
}
|
|
47
|
+
/** Remoting's state for one node. */
|
|
48
|
+
class Node {
|
|
49
|
+
api;
|
|
50
|
+
#hooks;
|
|
51
|
+
#self;
|
|
52
|
+
#transport;
|
|
53
|
+
#bufferSize;
|
|
54
|
+
#maxFrameBytes;
|
|
55
|
+
#codec;
|
|
56
|
+
#helloFrame;
|
|
57
|
+
#peers = new Map();
|
|
58
|
+
// The address behind every remote ref this node made, so watching one finds its node.
|
|
59
|
+
#remoteRefs = new WeakMap();
|
|
60
|
+
// Remote actors this node's actors and probes watch, by URI.
|
|
61
|
+
#targets = new Map();
|
|
62
|
+
#closed = false;
|
|
63
|
+
#closing;
|
|
64
|
+
#onControl;
|
|
65
|
+
#onPeer;
|
|
66
|
+
#entities;
|
|
67
|
+
constructor(system, hooks, self, transport, bufferSize, maxFrameBytes, closeWithSystem) {
|
|
68
|
+
this.#hooks = hooks;
|
|
69
|
+
this.#self = self;
|
|
70
|
+
this.#transport = transport;
|
|
71
|
+
this.#bufferSize = bufferSize;
|
|
72
|
+
this.#maxFrameBytes = maxFrameBytes;
|
|
73
|
+
this.#codec = createCodec({
|
|
74
|
+
self,
|
|
75
|
+
hooks,
|
|
76
|
+
remote: (uri, address) => this.#remoteRef(uri, address),
|
|
77
|
+
entities: () => this.#entities,
|
|
78
|
+
});
|
|
79
|
+
this.#helloFrame = JSON.stringify({
|
|
80
|
+
t: 'hello',
|
|
81
|
+
cluster: self.cluster,
|
|
82
|
+
protocol: PROTOCOL,
|
|
83
|
+
address: self.address,
|
|
84
|
+
uid: self.uid,
|
|
85
|
+
});
|
|
86
|
+
hooks.setWatchResolver((ref) => this.#watchTarget(ref));
|
|
87
|
+
const whenListening = transport.listen((connection) => {
|
|
88
|
+
this.#accept(connection);
|
|
89
|
+
}, (reason) => {
|
|
90
|
+
hooks.log.warn(`remoting: ${reason}`);
|
|
91
|
+
});
|
|
92
|
+
whenListening.catch((error) => {
|
|
93
|
+
hooks.log.error('remoting: the transport could not listen', {
|
|
94
|
+
address: self.address,
|
|
95
|
+
error,
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
if (closeWithSystem)
|
|
99
|
+
void system.whenTerminated.then(() => this.#close());
|
|
100
|
+
this.api = Object.freeze({
|
|
101
|
+
self,
|
|
102
|
+
codec: this.#codec,
|
|
103
|
+
whenListening,
|
|
104
|
+
ref: (uri) => this.#ref(uri),
|
|
105
|
+
nodeDown: (address, uid) => {
|
|
106
|
+
this.#nodeDown(address, uid);
|
|
107
|
+
},
|
|
108
|
+
signal: (address, frame) => this.#signal(address, frame),
|
|
109
|
+
onControl: (handler) => {
|
|
110
|
+
if (this.#onControl !== undefined)
|
|
111
|
+
throw new Error('remoting: onControl is already set');
|
|
112
|
+
this.#onControl = handler;
|
|
113
|
+
},
|
|
114
|
+
onPeer: (handler) => {
|
|
115
|
+
if (this.#onPeer !== undefined)
|
|
116
|
+
throw new Error('remoting: onPeer is already set');
|
|
117
|
+
this.#onPeer = handler;
|
|
118
|
+
},
|
|
119
|
+
setEntities: (refs) => {
|
|
120
|
+
if (this.#entities !== undefined)
|
|
121
|
+
throw new Error('remoting: entities are already set');
|
|
122
|
+
this.#entities = refs;
|
|
123
|
+
},
|
|
124
|
+
close: () => this.#close(),
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
// ---- refs ----
|
|
128
|
+
#ref(uri) {
|
|
129
|
+
const decoded = this.#codec.decodeValue({ $ref: uri });
|
|
130
|
+
if (!decoded.ok)
|
|
131
|
+
throw new TypeError(`remoting: ${decoded.problem}`);
|
|
132
|
+
return decoded.value;
|
|
133
|
+
}
|
|
134
|
+
#remoteRef(uri, address) {
|
|
135
|
+
const ref = this.#hooks.makeRef(uri, (message) => {
|
|
136
|
+
this.#tell(address.address, uri, message);
|
|
137
|
+
});
|
|
138
|
+
this.#remoteRefs.set(ref, address);
|
|
139
|
+
return ref;
|
|
140
|
+
}
|
|
141
|
+
#tell(address, uri, message) {
|
|
142
|
+
const hooks = this.#hooks;
|
|
143
|
+
if (this.#closed) {
|
|
144
|
+
hooks.deadLetter(uri, message, 'terminated');
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
const encoded = this.#codec.encode(message);
|
|
148
|
+
if (!encoded.ok) {
|
|
149
|
+
this.#unserializable(uri, message, encoded.problem);
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
const sender = hooks.sender();
|
|
153
|
+
const from = sender === undefined ? '' : `,"from":${JSON.stringify(this.#codec.uri(sender))}`;
|
|
154
|
+
const frame = `{"t":"msg","to":${JSON.stringify(uri)}${from},"m":${encoded.text}}`;
|
|
155
|
+
if (frame.length > this.#maxFrameBytes / 3 && utf8Bytes(frame) > this.#maxFrameBytes) {
|
|
156
|
+
this.#unserializable(uri, message, `the frame is ${String(utf8Bytes(frame))} bytes, over maxFrameBytes (${String(this.#maxFrameBytes)})`);
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
this.#send(this.#peer(address), frame, { recipient: uri, message });
|
|
160
|
+
}
|
|
161
|
+
#unserializable(uri, message, problem) {
|
|
162
|
+
this.#hooks.log.warn(`remoting: a message to ${uri} can't be sent: ${problem}`, {
|
|
163
|
+
recipient: uri,
|
|
164
|
+
});
|
|
165
|
+
this.#hooks.deadLetter(uri, message, 'unserializable');
|
|
166
|
+
}
|
|
167
|
+
// ---- peers and connections ----
|
|
168
|
+
#peer(address) {
|
|
169
|
+
let peer = this.#peers.get(address);
|
|
170
|
+
if (peer === undefined) {
|
|
171
|
+
peer = {
|
|
172
|
+
address,
|
|
173
|
+
uid: undefined,
|
|
174
|
+
down: false,
|
|
175
|
+
downUid: undefined,
|
|
176
|
+
link: undefined,
|
|
177
|
+
dialing: undefined,
|
|
178
|
+
waiting: new Queue(),
|
|
179
|
+
attempts: 0,
|
|
180
|
+
cancelRetry: undefined,
|
|
181
|
+
watchedBy: new Map(),
|
|
182
|
+
};
|
|
183
|
+
this.#peers.set(address, peer);
|
|
184
|
+
}
|
|
185
|
+
return peer;
|
|
186
|
+
}
|
|
187
|
+
/** Sends a frame now, or buffers it until the peer has a connection. */
|
|
188
|
+
#send(peer, frame, letter) {
|
|
189
|
+
if (peer.down) {
|
|
190
|
+
this.#drop(letter, 'unreachable');
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
if (peer.link !== undefined) {
|
|
194
|
+
peer.link.connection.send(frame);
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
if (peer.waiting.size >= this.#bufferSize) {
|
|
198
|
+
this.#drop(letter, 'unreachable');
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
peer.waiting.push({ frame, letter });
|
|
202
|
+
this.#dialSoon(peer);
|
|
203
|
+
}
|
|
204
|
+
#signal(address, frame) {
|
|
205
|
+
if (this.#closed)
|
|
206
|
+
return false;
|
|
207
|
+
const peer = this.#peer(address);
|
|
208
|
+
if (peer.down)
|
|
209
|
+
return false;
|
|
210
|
+
if (peer.link !== undefined) {
|
|
211
|
+
peer.link.connection.send(JSON.stringify(frame));
|
|
212
|
+
return true;
|
|
213
|
+
}
|
|
214
|
+
this.#dialSoon(peer);
|
|
215
|
+
return false;
|
|
216
|
+
}
|
|
217
|
+
#drop(letter, reason) {
|
|
218
|
+
if (letter !== undefined)
|
|
219
|
+
this.#hooks.deadLetter(letter.recipient, letter.message, reason);
|
|
220
|
+
}
|
|
221
|
+
#dialSoon(peer) {
|
|
222
|
+
if (this.#closed || peer.down)
|
|
223
|
+
return;
|
|
224
|
+
if (peer.link !== undefined || peer.dialing !== undefined || peer.cancelRetry !== undefined) {
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
if (peer.attempts === 0) {
|
|
228
|
+
this.#dial(peer);
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
// Doubling from the minimum, with jitter so nodes that lost the same peer don't dial together.
|
|
232
|
+
const ceiling = Math.min(BACKOFF_MAX_MS, BACKOFF_MIN_MS * 2 ** (peer.attempts - 1));
|
|
233
|
+
const delay = Math.round(ceiling * (0.5 + this.#hooks.random() / 2));
|
|
234
|
+
peer.cancelRetry = this.#hooks.scheduler.setTimer(() => {
|
|
235
|
+
peer.cancelRetry = undefined;
|
|
236
|
+
// A retry that finds the peer connected, or down, has nothing to do.
|
|
237
|
+
if (peer.dialing === undefined && peer.link === undefined && !this.#closed && !peer.down) {
|
|
238
|
+
this.#dial(peer);
|
|
239
|
+
}
|
|
240
|
+
}, delay);
|
|
241
|
+
}
|
|
242
|
+
#dial(peer) {
|
|
243
|
+
const connection = this.#transport.connect(peer.address);
|
|
244
|
+
const link = {
|
|
245
|
+
connection,
|
|
246
|
+
dialer: this.#self.address,
|
|
247
|
+
outbound: true,
|
|
248
|
+
peer,
|
|
249
|
+
gotHello: false,
|
|
250
|
+
abandoned: false,
|
|
251
|
+
};
|
|
252
|
+
peer.dialing = link;
|
|
253
|
+
this.#wire(link);
|
|
254
|
+
connection.send(this.#helloFrame);
|
|
255
|
+
}
|
|
256
|
+
#accept(connection) {
|
|
257
|
+
if (this.#closed) {
|
|
258
|
+
connection.close();
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
// The dialer's address isn't known until its hello: the empty string stands in until then.
|
|
262
|
+
const link = {
|
|
263
|
+
connection,
|
|
264
|
+
dialer: '',
|
|
265
|
+
outbound: false,
|
|
266
|
+
peer: undefined,
|
|
267
|
+
gotHello: false,
|
|
268
|
+
abandoned: false,
|
|
269
|
+
};
|
|
270
|
+
this.#wire(link);
|
|
271
|
+
}
|
|
272
|
+
#wire(link) {
|
|
273
|
+
link.connection.onFrame((text) => {
|
|
274
|
+
this.#onFrame(link, text);
|
|
275
|
+
});
|
|
276
|
+
link.connection.onClose((error) => {
|
|
277
|
+
this.#onClose(link, error);
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
/** Closes a link on purpose, so its close doesn't count as a failure or start a dial. */
|
|
281
|
+
#abandon(link) {
|
|
282
|
+
link.abandoned = true;
|
|
283
|
+
const peer = link.peer;
|
|
284
|
+
if (peer?.link === link)
|
|
285
|
+
peer.link = undefined;
|
|
286
|
+
if (peer?.dialing === link)
|
|
287
|
+
peer.dialing = undefined;
|
|
288
|
+
link.connection.close();
|
|
289
|
+
}
|
|
290
|
+
#onClose(link, error) {
|
|
291
|
+
if (link.abandoned)
|
|
292
|
+
return;
|
|
293
|
+
link.abandoned = true;
|
|
294
|
+
const peer = link.peer;
|
|
295
|
+
if (peer === undefined)
|
|
296
|
+
return;
|
|
297
|
+
if (peer.link === link) {
|
|
298
|
+
peer.link = undefined;
|
|
299
|
+
this.#hooks.log.debug(`remoting: lost the connection to ${peer.address}`, {
|
|
300
|
+
address: peer.address,
|
|
301
|
+
error,
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
else if (peer.dialing === link) {
|
|
305
|
+
peer.dialing = undefined;
|
|
306
|
+
peer.attempts++;
|
|
307
|
+
}
|
|
308
|
+
else {
|
|
309
|
+
return;
|
|
310
|
+
}
|
|
311
|
+
if (peer.waiting.size > 0)
|
|
312
|
+
this.#dialSoon(peer);
|
|
313
|
+
}
|
|
314
|
+
#establish(peer, link) {
|
|
315
|
+
peer.link = link;
|
|
316
|
+
if (peer.dialing === link)
|
|
317
|
+
peer.dialing = undefined;
|
|
318
|
+
peer.attempts = 0;
|
|
319
|
+
peer.cancelRetry?.();
|
|
320
|
+
peer.cancelRetry = undefined;
|
|
321
|
+
for (let next = peer.waiting.shift(); next !== undefined; next = peer.waiting.shift()) {
|
|
322
|
+
link.connection.send(next.frame);
|
|
323
|
+
}
|
|
324
|
+
// Set by the hello that established the link.
|
|
325
|
+
if (peer.uid !== undefined)
|
|
326
|
+
this.#onPeer?.({ address: peer.address, uid: peer.uid });
|
|
327
|
+
}
|
|
328
|
+
// ---- frames ----
|
|
329
|
+
#onFrame(link, text) {
|
|
330
|
+
if (this.#closed || link.abandoned)
|
|
331
|
+
return;
|
|
332
|
+
let frame;
|
|
333
|
+
try {
|
|
334
|
+
frame = JSON.parse(text);
|
|
335
|
+
}
|
|
336
|
+
catch {
|
|
337
|
+
frame = undefined;
|
|
338
|
+
}
|
|
339
|
+
if (!isRecord(frame) || typeof frame.t !== 'string') {
|
|
340
|
+
this.#refuse(link, `it sent a frame that isn't one: ${preview(text)}`);
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
if (!link.gotHello) {
|
|
344
|
+
if (frame.t !== 'hello') {
|
|
345
|
+
this.#refuse(link, `it sent "${frame.t}" before its hello`);
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
this.#onHello(link, frame);
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
const peer = link.peer;
|
|
352
|
+
if (peer === undefined)
|
|
353
|
+
return;
|
|
354
|
+
switch (frame.t) {
|
|
355
|
+
case 'msg':
|
|
356
|
+
this.#onMessage(peer, frame);
|
|
357
|
+
return;
|
|
358
|
+
case 'watch':
|
|
359
|
+
this.#onWatch(peer, frame);
|
|
360
|
+
return;
|
|
361
|
+
case 'unwatch':
|
|
362
|
+
this.#onUnwatch(peer, frame);
|
|
363
|
+
return;
|
|
364
|
+
case 'terminated':
|
|
365
|
+
this.#onTerminated(frame);
|
|
366
|
+
return;
|
|
367
|
+
default:
|
|
368
|
+
if (this.#onControl !== undefined && peer.uid !== undefined) {
|
|
369
|
+
this.#onControl({ address: peer.address, uid: peer.uid }, frame);
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
this.#hooks.log.warn(`remoting: ignored a "${frame.t}" frame from ${peer.address}`, {
|
|
373
|
+
address: peer.address,
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
/** Logs why a connection is refused, and closes it. `claimed` is the address its hello gave. */
|
|
378
|
+
#refuse(link, why, claimed) {
|
|
379
|
+
const address = link.peer?.address ?? (typeof claimed === 'string' ? claimed : undefined);
|
|
380
|
+
this.#hooks.log.warn(`remoting: refused the connection with ${address ?? 'a peer'}: ${why}`, address === undefined ? {} : { address });
|
|
381
|
+
this.#abandon(link);
|
|
382
|
+
const peer = link.peer;
|
|
383
|
+
// A refused dial counts as a failed one, so the next waits its backoff.
|
|
384
|
+
if (peer !== undefined && link.outbound) {
|
|
385
|
+
peer.attempts++;
|
|
386
|
+
if (peer.waiting.size > 0)
|
|
387
|
+
this.#dialSoon(peer);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
#onHello(link, frame) {
|
|
391
|
+
link.gotHello = true;
|
|
392
|
+
const hello = frame;
|
|
393
|
+
const problem = this.#checkHello(link, hello);
|
|
394
|
+
if (problem !== undefined) {
|
|
395
|
+
// An inbound peer hears this node's hello first, so it can log the mismatch too.
|
|
396
|
+
if (!link.outbound)
|
|
397
|
+
link.connection.send(this.#helloFrame);
|
|
398
|
+
this.#refuse(link, problem, hello.address);
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
const { address, uid } = hello;
|
|
402
|
+
const peer = this.#peer(address);
|
|
403
|
+
if (!link.outbound) {
|
|
404
|
+
link.peer = peer;
|
|
405
|
+
link.dialer = address;
|
|
406
|
+
}
|
|
407
|
+
if (peer.down && peer.downUid === uid) {
|
|
408
|
+
// Told before it's refused, so a node that was downed while it was still running learns
|
|
409
|
+
// that it's down, and leaves (decision 0028).
|
|
410
|
+
if (!link.outbound) {
|
|
411
|
+
link.connection.send(this.#helloFrame);
|
|
412
|
+
link.connection.send(DOWNED_FRAME);
|
|
413
|
+
}
|
|
414
|
+
this.#refuse(link, `${address}#${String(uid)} was downed, and can rejoin only as a new uid`);
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
if (peer.uid !== undefined && peer.uid !== uid) {
|
|
418
|
+
this.#incarnationGone(peer, `restarted as uid ${String(uid)}`, link);
|
|
419
|
+
}
|
|
420
|
+
peer.uid = uid;
|
|
421
|
+
peer.down = false;
|
|
422
|
+
peer.downUid = undefined;
|
|
423
|
+
// Watches made before this node knew the peer's uid, on another incarnation, end now.
|
|
424
|
+
this.#endTargets(address, uid, `${address} is now uid ${String(uid)}`);
|
|
425
|
+
const rival = peer.link ?? peer.dialing;
|
|
426
|
+
if (rival !== undefined && rival !== link) {
|
|
427
|
+
if (keepsOver(rival, link)) {
|
|
428
|
+
this.#hooks.log.debug(`remoting: kept the connection ${rival.dialer} dialed`, {
|
|
429
|
+
address,
|
|
430
|
+
});
|
|
431
|
+
this.#abandon(link);
|
|
432
|
+
return;
|
|
433
|
+
}
|
|
434
|
+
this.#abandon(rival);
|
|
435
|
+
}
|
|
436
|
+
if (!link.outbound)
|
|
437
|
+
link.connection.send(this.#helloFrame);
|
|
438
|
+
this.#establish(peer, link);
|
|
439
|
+
}
|
|
440
|
+
#checkHello(link, hello) {
|
|
441
|
+
const self = this.#self;
|
|
442
|
+
if (hello.cluster !== self.cluster) {
|
|
443
|
+
return `its cluster is ${describeValue(hello.cluster)}, not "${self.cluster}"`;
|
|
444
|
+
}
|
|
445
|
+
if (hello.protocol !== PROTOCOL) {
|
|
446
|
+
return `its protocol is ${describeValue(hello.protocol)}, not ${String(PROTOCOL)}`;
|
|
447
|
+
}
|
|
448
|
+
const { address, uid } = hello;
|
|
449
|
+
if (typeof address !== 'string' || address === '' || /[@#/\s]/.test(address)) {
|
|
450
|
+
return `its address is ${describeValue(address)}`;
|
|
451
|
+
}
|
|
452
|
+
if (typeof uid !== 'number' || !Number.isSafeInteger(uid) || uid < 0) {
|
|
453
|
+
return `its uid is ${describeValue(uid)}`;
|
|
454
|
+
}
|
|
455
|
+
if (address === self.address)
|
|
456
|
+
return 'it has this node’s own address';
|
|
457
|
+
if (link.outbound && address !== link.peer?.address) {
|
|
458
|
+
return `it says it's ${address}, not ${String(link.peer?.address)}`;
|
|
459
|
+
}
|
|
460
|
+
return undefined;
|
|
461
|
+
}
|
|
462
|
+
#onMessage(peer, frame) {
|
|
463
|
+
const hooks = this.#hooks;
|
|
464
|
+
const { to, from, m } = frame;
|
|
465
|
+
const target = typeof to === 'string' ? this.#local(to) : undefined;
|
|
466
|
+
if (target === undefined) {
|
|
467
|
+
hooks.log.warn(`remoting: a message from ${peer.address} has a bad recipient`, {
|
|
468
|
+
address: peer.address,
|
|
469
|
+
to,
|
|
470
|
+
});
|
|
471
|
+
return;
|
|
472
|
+
}
|
|
473
|
+
const decoded = this.#codec.decodeValue(m);
|
|
474
|
+
if (!decoded.ok) {
|
|
475
|
+
hooks.log.warn(`remoting: a message from ${peer.address} to ${String(to)} can't be decoded: ${decoded.problem}`, { address: peer.address });
|
|
476
|
+
hooks.deadLetter(target.recipient, m, 'unserializable');
|
|
477
|
+
return;
|
|
478
|
+
}
|
|
479
|
+
if (target.ref === undefined) {
|
|
480
|
+
hooks.deadLetter(target.recipient, decoded.value, target.reason);
|
|
481
|
+
return;
|
|
482
|
+
}
|
|
483
|
+
const sender = typeof from === 'string' && parseRefUri(from) !== undefined ? from : undefined;
|
|
484
|
+
hooks.tellFrom(target.ref, decoded.value, sender);
|
|
485
|
+
}
|
|
486
|
+
/**
|
|
487
|
+
* The local actor a URI from a peer names, or why there's none: a stopped actor, an ask that's
|
|
488
|
+
* done, or an old incarnation of this node. `undefined` for a URI that isn't this node's.
|
|
489
|
+
*/
|
|
490
|
+
#local(uri) {
|
|
491
|
+
const address = parseRefUri(uri);
|
|
492
|
+
const self = this.#self;
|
|
493
|
+
if (address?.cluster !== self.cluster || address.address !== self.address)
|
|
494
|
+
return undefined;
|
|
495
|
+
if (address.uid !== self.uid)
|
|
496
|
+
return { ref: undefined, recipient: uri, reason: 'stopped' };
|
|
497
|
+
const ref = this.#hooks.lookup(address.path);
|
|
498
|
+
if (ref !== undefined)
|
|
499
|
+
return { ref, recipient: address.path };
|
|
500
|
+
const reason = address.path.startsWith('/temp/') ? 'askCompleted' : 'stopped';
|
|
501
|
+
return { ref: undefined, recipient: address.path, reason };
|
|
502
|
+
}
|
|
503
|
+
// ---- death watch ----
|
|
504
|
+
#watchTarget(ref) {
|
|
505
|
+
if (typeof ref !== 'object' || ref === null)
|
|
506
|
+
return undefined;
|
|
507
|
+
const address = this.#remoteRefs.get(ref);
|
|
508
|
+
if (address === undefined)
|
|
509
|
+
return undefined;
|
|
510
|
+
const uri = ref.path;
|
|
511
|
+
const existing = this.#targets.get(uri);
|
|
512
|
+
if (existing !== undefined)
|
|
513
|
+
return existing;
|
|
514
|
+
const target = new RemoteTarget(ref, address, {
|
|
515
|
+
register: (t) => {
|
|
516
|
+
this.#targets.set(uri, t);
|
|
517
|
+
this.#control(address.address, { t: 'watch', to: uri });
|
|
518
|
+
},
|
|
519
|
+
unregister: (t) => {
|
|
520
|
+
if (this.#targets.get(uri) === t)
|
|
521
|
+
this.#targets.delete(uri);
|
|
522
|
+
this.#control(address.address, { t: 'unwatch', to: uri });
|
|
523
|
+
},
|
|
524
|
+
ended: (t) => {
|
|
525
|
+
if (this.#targets.get(uri) === t)
|
|
526
|
+
this.#targets.delete(uri);
|
|
527
|
+
},
|
|
528
|
+
});
|
|
529
|
+
const peer = this.#peers.get(address.address);
|
|
530
|
+
if (peer !== undefined && (peer.down || (peer.uid !== undefined && peer.uid !== address.uid))) {
|
|
531
|
+
target.end(nodeGone(address.address, peer.down ? 'down' : 'restarted'));
|
|
532
|
+
}
|
|
533
|
+
return target;
|
|
534
|
+
}
|
|
535
|
+
#control(address, frame) {
|
|
536
|
+
if (this.#closed)
|
|
537
|
+
return;
|
|
538
|
+
this.#send(this.#peer(address), JSON.stringify(frame), undefined);
|
|
539
|
+
}
|
|
540
|
+
#onWatch(peer, frame) {
|
|
541
|
+
const { to } = frame;
|
|
542
|
+
if (typeof to !== 'string' || peer.watchedBy.has(to))
|
|
543
|
+
return;
|
|
544
|
+
const local = this.#local(to);
|
|
545
|
+
const target = local?.ref === undefined ? undefined : this.#hooks.watchTarget(local.ref);
|
|
546
|
+
if (target === undefined || target.terminated) {
|
|
547
|
+
this.#control(peer.address, terminatedFrame(to, target?.failure));
|
|
548
|
+
return;
|
|
549
|
+
}
|
|
550
|
+
const watcher = {
|
|
551
|
+
notifyTerminated: (ended) => {
|
|
552
|
+
peer.watchedBy.delete(to);
|
|
553
|
+
this.#control(peer.address, terminatedFrame(to, ended.failure));
|
|
554
|
+
},
|
|
555
|
+
};
|
|
556
|
+
peer.watchedBy.set(to, { target, watcher });
|
|
557
|
+
target.addWatcher(watcher);
|
|
558
|
+
}
|
|
559
|
+
#onUnwatch(peer, frame) {
|
|
560
|
+
const { to } = frame;
|
|
561
|
+
if (typeof to !== 'string')
|
|
562
|
+
return;
|
|
563
|
+
const watch = peer.watchedBy.get(to);
|
|
564
|
+
if (watch === undefined)
|
|
565
|
+
return;
|
|
566
|
+
peer.watchedBy.delete(to);
|
|
567
|
+
watch.target.removeWatcher(watch.watcher);
|
|
568
|
+
}
|
|
569
|
+
#onTerminated(frame) {
|
|
570
|
+
const { ref, failure } = frame;
|
|
571
|
+
if (typeof ref !== 'string')
|
|
572
|
+
return;
|
|
573
|
+
const target = this.#targets.get(ref);
|
|
574
|
+
if (target === undefined) {
|
|
575
|
+
// An unwatch crossed it on the way, or this node's watch ended first.
|
|
576
|
+
this.#hooks.log.debug(`remoting: a terminated for ${ref}, which nothing here watches`);
|
|
577
|
+
return;
|
|
578
|
+
}
|
|
579
|
+
target.end(failure === undefined ? undefined : { error: rebuild(failure) });
|
|
580
|
+
}
|
|
581
|
+
/** Ends every watch on the node at `address` that isn't on incarnation `keep`. */
|
|
582
|
+
#endTargets(address, keep, why) {
|
|
583
|
+
for (const target of [...this.#targets.values()]) {
|
|
584
|
+
if (target.address.address === address && target.address.uid !== keep) {
|
|
585
|
+
target.end(nodeGone(address, why));
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
/**
|
|
590
|
+
* The peer's incarnation has gone: a new uid at its address, or downed. Its connections close,
|
|
591
|
+
* its watches on this node's actors are dropped, and this node's watches on it end.
|
|
592
|
+
*/
|
|
593
|
+
#incarnationGone(peer, why, keep) {
|
|
594
|
+
for (const link of [peer.link, peer.dialing]) {
|
|
595
|
+
if (link !== undefined && link !== keep)
|
|
596
|
+
this.#abandon(link);
|
|
597
|
+
}
|
|
598
|
+
for (const { target, watcher } of peer.watchedBy.values())
|
|
599
|
+
target.removeWatcher(watcher);
|
|
600
|
+
peer.watchedBy.clear();
|
|
601
|
+
this.#endTargets(peer.address, undefined, why);
|
|
602
|
+
}
|
|
603
|
+
#nodeDown(address, uid) {
|
|
604
|
+
const peer = this.#peer(address);
|
|
605
|
+
if (uid !== undefined && peer.uid !== undefined && peer.uid !== uid) {
|
|
606
|
+
// A newer incarnation is here already. The old one's watches end, and nothing else.
|
|
607
|
+
this.#endTargets(address, peer.uid, 'down');
|
|
608
|
+
return;
|
|
609
|
+
}
|
|
610
|
+
if (peer.down)
|
|
611
|
+
return;
|
|
612
|
+
peer.down = true;
|
|
613
|
+
peer.downUid = uid ?? peer.uid;
|
|
614
|
+
peer.cancelRetry?.();
|
|
615
|
+
peer.cancelRetry = undefined;
|
|
616
|
+
for (let next = peer.waiting.shift(); next !== undefined; next = peer.waiting.shift()) {
|
|
617
|
+
this.#drop(next.letter, 'unreachable');
|
|
618
|
+
}
|
|
619
|
+
this.#incarnationGone(peer, 'down');
|
|
620
|
+
}
|
|
621
|
+
#close() {
|
|
622
|
+
if (this.#closing !== undefined)
|
|
623
|
+
return this.#closing;
|
|
624
|
+
this.#closed = true;
|
|
625
|
+
for (const peer of this.#peers.values()) {
|
|
626
|
+
peer.cancelRetry?.();
|
|
627
|
+
peer.cancelRetry = undefined;
|
|
628
|
+
for (let next = peer.waiting.shift(); next !== undefined; next = peer.waiting.shift()) {
|
|
629
|
+
this.#drop(next.letter, 'terminated');
|
|
630
|
+
}
|
|
631
|
+
for (const { target, watcher } of peer.watchedBy.values())
|
|
632
|
+
target.removeWatcher(watcher);
|
|
633
|
+
peer.watchedBy.clear();
|
|
634
|
+
}
|
|
635
|
+
this.#closing = this.#transport.close();
|
|
636
|
+
return this.#closing;
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
/** A remote actor that local actors watch, standing in for its cell. */
|
|
640
|
+
class RemoteTarget {
|
|
641
|
+
ref;
|
|
642
|
+
address;
|
|
643
|
+
terminated = false;
|
|
644
|
+
failure;
|
|
645
|
+
#owner;
|
|
646
|
+
#watchers = new Set();
|
|
647
|
+
#registered = false;
|
|
648
|
+
constructor(ref, address, owner) {
|
|
649
|
+
this.ref = ref;
|
|
650
|
+
this.address = address;
|
|
651
|
+
this.#owner = owner;
|
|
652
|
+
}
|
|
653
|
+
addWatcher(watcher) {
|
|
654
|
+
if (this.terminated)
|
|
655
|
+
return;
|
|
656
|
+
this.#watchers.add(watcher);
|
|
657
|
+
if (this.#registered)
|
|
658
|
+
return;
|
|
659
|
+
this.#registered = true;
|
|
660
|
+
this.#owner.register(this);
|
|
661
|
+
}
|
|
662
|
+
removeWatcher(watcher) {
|
|
663
|
+
if (!this.#watchers.delete(watcher) || this.#watchers.size > 0 || !this.#registered)
|
|
664
|
+
return;
|
|
665
|
+
this.#registered = false;
|
|
666
|
+
this.#owner.unregister(this);
|
|
667
|
+
}
|
|
668
|
+
end(failure) {
|
|
669
|
+
if (this.terminated)
|
|
670
|
+
return;
|
|
671
|
+
this.terminated = true;
|
|
672
|
+
this.failure = failure;
|
|
673
|
+
this.#registered = false;
|
|
674
|
+
this.#owner.ended(this);
|
|
675
|
+
const watchers = [...this.#watchers];
|
|
676
|
+
this.#watchers.clear();
|
|
677
|
+
for (const watcher of watchers)
|
|
678
|
+
watcher.notifyTerminated(this);
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
/** Of two links to one peer, whether `existing` wins: the one the lower address dialed. */
|
|
682
|
+
function keepsOver(existing, incoming) {
|
|
683
|
+
// The same dialer dials again only once its old link has gone, whether or not this node knows.
|
|
684
|
+
if (existing.dialer === incoming.dialer)
|
|
685
|
+
return false;
|
|
686
|
+
return existing.dialer < incoming.dialer;
|
|
687
|
+
}
|
|
688
|
+
function terminatedFrame(uri, failure) {
|
|
689
|
+
if (failure === undefined)
|
|
690
|
+
return { t: 'terminated', ref: uri };
|
|
691
|
+
const { error } = failure;
|
|
692
|
+
const described = error instanceof Error
|
|
693
|
+
? { name: error.name, message: error.message }
|
|
694
|
+
: { name: 'Error', message: String(error) };
|
|
695
|
+
return { t: 'terminated', ref: uri, failure: described };
|
|
696
|
+
}
|
|
697
|
+
/** A failure from the wire as an `Error` with the original's name and message, without a stack. */
|
|
698
|
+
function rebuild(failure) {
|
|
699
|
+
const { name, message } = isRecord(failure) ? failure : {};
|
|
700
|
+
const error = new Error(typeof message === 'string' ? message : 'the remote actor failed');
|
|
701
|
+
if (typeof name === 'string')
|
|
702
|
+
error.name = name;
|
|
703
|
+
return error;
|
|
704
|
+
}
|
|
705
|
+
function nodeGone(address, why) {
|
|
706
|
+
return { error: new Error(`node ${address} is gone: ${why}`) };
|
|
707
|
+
}
|
|
708
|
+
/** Whether `value` has a transport's shape. */
|
|
709
|
+
export function isTransport(value) {
|
|
710
|
+
if (typeof value !== 'object' || value === null)
|
|
711
|
+
return false;
|
|
712
|
+
const t = value;
|
|
713
|
+
return (typeof t.address === 'string' &&
|
|
714
|
+
typeof t.listen === 'function' &&
|
|
715
|
+
typeof t.connect === 'function' &&
|
|
716
|
+
typeof t.close === 'function');
|
|
717
|
+
}
|
|
718
|
+
function isRecord(value) {
|
|
719
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
720
|
+
}
|
|
721
|
+
function describeValue(value) {
|
|
722
|
+
if (value === undefined)
|
|
723
|
+
return 'missing';
|
|
724
|
+
return typeof value === 'string' ? `"${value}"` : JSON.stringify(value);
|
|
725
|
+
}
|
|
726
|
+
function preview(text) {
|
|
727
|
+
return text.length > 80 ? `${JSON.stringify(text.slice(0, 80))}…` : JSON.stringify(text);
|
|
728
|
+
}
|
|
729
|
+
/** The UTF-8 length of `text`, without allocating. Lone surrogates count as the 3 bytes of U+FFFD. */
|
|
730
|
+
export function utf8Bytes(text) {
|
|
731
|
+
let bytes = text.length;
|
|
732
|
+
for (let i = 0; i < text.length; i++) {
|
|
733
|
+
const c = text.charCodeAt(i);
|
|
734
|
+
if (c < 0x80)
|
|
735
|
+
continue;
|
|
736
|
+
if (c < 0x800) {
|
|
737
|
+
bytes += 1;
|
|
738
|
+
}
|
|
739
|
+
else if (c >= 0xd800 && c <= 0xdbff && i + 1 < text.length) {
|
|
740
|
+
const next = text.charCodeAt(i + 1);
|
|
741
|
+
if (next >= 0xdc00 && next <= 0xdfff) {
|
|
742
|
+
// Two code units, four bytes.
|
|
743
|
+
bytes += 2;
|
|
744
|
+
i++;
|
|
745
|
+
}
|
|
746
|
+
else {
|
|
747
|
+
bytes += 2;
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
else {
|
|
751
|
+
bytes += 2;
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
return bytes;
|
|
755
|
+
}
|