@netifyjs/netify 1.0.0-alpha.3 → 1.0.0-alpha.5
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/README.md +6 -6
- package/package.json +1 -1
- package/src/bootstrap/client/index.ts +1 -1
- package/src/bootstrap/dispatcher/index.ts +7 -0
- package/src/bootstrap/reconnect/index.ts +14 -4
- package/src/bootstrap/server/index.ts +10 -4
- package/src/buffer/index.ts +229 -88
- package/src/buffer/quantizer/index.ts +16 -3
- package/src/channel/context/index.ts +18 -11
- package/src/channel/handler/index.ts +1 -1
- package/src/channel/index.ts +30 -7
- package/src/channel/outbound/index.ts +28 -12
- package/src/channel/pipeline/index.ts +45 -5
- package/src/channel/queue/index.ts +3 -2
- package/src/channel/resolved/index.ts +13 -3
- package/src/channel/server/index.ts +19 -10
- package/src/cluster/index.ts +73 -32
- package/src/codec/binary/index.ts +19 -0
- package/src/codec/decoder/index.ts +223 -83
- package/src/codec/delimiter/index.ts +95 -37
- package/src/codec/encoder/index.ts +30 -5
- package/src/codec/fixed/index.ts +5 -1
- package/src/codec/http/aggregator/index.ts +45 -16
- package/src/codec/http/decoder/object/index.ts +156 -40
- package/src/codec/http/decoder/request/index.ts +6 -2
- package/src/codec/http/encoder/object/index.ts +2 -7
- package/src/codec/http/encoder/request/index.ts +5 -0
- package/src/codec/http/headers/index.ts +45 -2
- package/src/codec/http/message/full/request/index.ts +1 -1
- package/src/codec/http/message/full/response/index.ts +1 -1
- package/src/codec/http/message/request/index.ts +14 -1
- package/src/codec/http/message/response/index.ts +1 -1
- package/src/codec/http/version/index.ts +3 -0
- package/src/codec/length/index.ts +66 -44
- package/src/codec/message/decoder/index.ts +18 -1
- package/src/codec/message/encoder/index.ts +85 -8
- package/src/codec/prepender/index.ts +4 -1
- package/src/codec/string/decoder/index.ts +6 -1
- package/src/codec/websocket/decoder/index.ts +78 -3
- package/src/codec/websocket/encoder/index.ts +90 -6
- package/src/codec/websocket/frame/base/index.ts +19 -10
- package/src/codec/websocket/frame/text/index.ts +2 -2
- package/src/codec/websocket/protocol/index.ts +12 -0
- package/src/datagram/bootstrap/index.ts +1 -1
- package/src/datagram/transport/index.ts +30 -1
- package/src/handler/flush/index.ts +25 -5
- package/src/handler/idle/index.ts +120 -26
- package/src/handler/logging/index.ts +13 -1
- package/src/handler/metrics/index.ts +49 -16
- package/src/handler/offload/index.ts +7 -0
- package/src/handler/timeout/read/index.ts +1 -1
- package/src/handler/timeout/write/index.ts +104 -23
- package/src/handler/traffic/index.ts +251 -38
- package/src/index.ts +1 -0
- package/src/reliable/cipher/index.ts +49 -5
- package/src/reliable/cookie/index.ts +64 -2
- package/src/reliable/handler/index.ts +11 -15
- package/src/reliable/index.ts +7 -1
- package/src/reliable/lane/index.ts +277 -58
- package/src/reliable/peer/index.ts +125 -38
- package/src/serve/bootstrap/index.ts +82 -135
- package/src/serve/channel/index.ts +84 -2
- package/src/serve/gate/index.ts +215 -0
- package/src/serve/index.ts +6 -2
- package/src/serve/outbox/index.ts +63 -2
- package/src/serve/transport/index.ts +33 -3
- package/src/serve/writer/index.ts +4 -3
- package/src/shard/gateway/index.ts +102 -29
- package/src/shard/link/index.ts +161 -175
- package/src/worker/group/index.ts +231 -13
- package/src/worker/pool/index.ts +100 -19
- package/src/worker/service/index.ts +9 -1
|
@@ -85,7 +85,7 @@ export type OutboundHandler<O = unknown> = {
|
|
|
85
85
|
write?(context: HandlerContext, message: O): Promise<void>
|
|
86
86
|
|
|
87
87
|
/**
|
|
88
|
-
* Takes
|
|
88
|
+
* Takes sync writes directly, false to route them through write
|
|
89
89
|
* @param context where the write is going
|
|
90
90
|
* @param message what's being sent
|
|
91
91
|
*/
|
package/src/channel/index.ts
CHANGED
|
@@ -5,7 +5,7 @@ import { Pipeline } from './pipeline/index.ts'
|
|
|
5
5
|
import { HeadHandler } from './pipeline/head/index.ts'
|
|
6
6
|
import type { ChannelSetup } from './initializer/index.ts'
|
|
7
7
|
import { EventQueue } from './queue/index.ts'
|
|
8
|
-
import { pending, resolved } from './resolved/index.ts'
|
|
8
|
+
import { pending, protect, resolved } from './resolved/index.ts'
|
|
9
9
|
import type { Transport } from './transport/index.ts'
|
|
10
10
|
import { ConsoleLogger, type Logger } from '../logger/index.ts'
|
|
11
11
|
import type { PacketAddress } from '../datagram/packet/index.ts'
|
|
@@ -272,7 +272,7 @@ export class Channel {
|
|
|
272
272
|
send: (
|
|
273
273
|
bytes: Uint8Array,
|
|
274
274
|
recipient: PacketAddress | null,
|
|
275
|
-
): Promise<void> => this.send(bytes, recipient),
|
|
275
|
+
): Promise<void> => this.send(bytes, recipient, this.pipeline.syncing),
|
|
276
276
|
/**
|
|
277
277
|
* Tells the tracker first so late writes still get flushed
|
|
278
278
|
*/
|
|
@@ -380,7 +380,7 @@ export class Channel {
|
|
|
380
380
|
}
|
|
381
381
|
|
|
382
382
|
/**
|
|
383
|
-
*
|
|
383
|
+
* Writes and flushes without a promise
|
|
384
384
|
* @param message anything your encoders understand
|
|
385
385
|
*/
|
|
386
386
|
public writeAndFlushSync(message: unknown): void {
|
|
@@ -410,6 +410,7 @@ export class Channel {
|
|
|
410
410
|
return this.send(
|
|
411
411
|
bytes instanceof ByteBuf ? bytes.toUint8Array() : bytes,
|
|
412
412
|
null,
|
|
413
|
+
false,
|
|
413
414
|
)
|
|
414
415
|
}
|
|
415
416
|
|
|
@@ -426,7 +427,7 @@ export class Channel {
|
|
|
426
427
|
}
|
|
427
428
|
|
|
428
429
|
/**
|
|
429
|
-
*
|
|
430
|
+
* Sends encoded bytes without a promise
|
|
430
431
|
* @param bytes already encoded, nothing in the pipeline touches them
|
|
431
432
|
*/
|
|
432
433
|
public writeAndFlushEncodedSync(bytes: Uint8Array | ByteBuf): void {
|
|
@@ -438,6 +439,13 @@ export class Channel {
|
|
|
438
439
|
void this.outbound.flush()
|
|
439
440
|
}
|
|
440
441
|
|
|
442
|
+
/**
|
|
443
|
+
* Sends the queued encoded writes
|
|
444
|
+
*/
|
|
445
|
+
public flushEncoded(): void {
|
|
446
|
+
void this.outbound.flush()
|
|
447
|
+
}
|
|
448
|
+
|
|
441
449
|
/**
|
|
442
450
|
* writeEncoded plus a flush, what ChannelGroup.broadcast uses
|
|
443
451
|
* @param bytes ready for the wire
|
|
@@ -520,6 +528,16 @@ export class Channel {
|
|
|
520
528
|
)
|
|
521
529
|
}
|
|
522
530
|
|
|
531
|
+
/**
|
|
532
|
+
* Reports a failed sync write
|
|
533
|
+
* @param error why the write failed
|
|
534
|
+
*/
|
|
535
|
+
public handleWriteFailure(error: unknown): void {
|
|
536
|
+
if (this.finished) return
|
|
537
|
+
|
|
538
|
+
void this.handleError(error)
|
|
539
|
+
}
|
|
540
|
+
|
|
523
541
|
/**
|
|
524
542
|
* Turns a throw or rejection into exceptionCaught instead of a lost error
|
|
525
543
|
* @param task what execute was given
|
|
@@ -578,8 +596,10 @@ export class Channel {
|
|
|
578
596
|
* Finishes a staged write, flushing if a flush is waiting on it
|
|
579
597
|
*/
|
|
580
598
|
public commitStaged(): void {
|
|
599
|
+
this.outbound.commit()
|
|
581
600
|
if (this.pipeline.tracker.flushCarried) {
|
|
582
|
-
|
|
601
|
+
const flushing = this.outbound.flush()
|
|
602
|
+
if (flushing !== resolved) void protect(flushing)
|
|
583
603
|
}
|
|
584
604
|
}
|
|
585
605
|
|
|
@@ -587,15 +607,18 @@ export class Channel {
|
|
|
587
607
|
* Queues bytes for the socket and flushes if a flush is waiting on them
|
|
588
608
|
* @param bytes encoded bytes
|
|
589
609
|
* @param recipient UDP target, or null
|
|
610
|
+
* @param discard nobody's waiting on it
|
|
590
611
|
*/
|
|
591
612
|
private send(
|
|
592
613
|
bytes: Uint8Array,
|
|
593
614
|
recipient: PacketAddress | null,
|
|
615
|
+
discard: boolean,
|
|
594
616
|
): Promise<void> {
|
|
595
|
-
const promise = this.outbound.add(bytes, recipient,
|
|
617
|
+
const promise = this.outbound.add(bytes, recipient, discard)
|
|
596
618
|
|
|
597
619
|
if (this.pipeline.tracker.flushCarried) {
|
|
598
|
-
|
|
620
|
+
const flushing = this.outbound.flush()
|
|
621
|
+
if (flushing !== resolved) void protect(flushing)
|
|
599
622
|
}
|
|
600
623
|
|
|
601
624
|
return promise
|
|
@@ -206,10 +206,20 @@ export class OutboundBuffer {
|
|
|
206
206
|
|
|
207
207
|
const offset = this.stagedTo
|
|
208
208
|
this.stagedTo += size
|
|
209
|
-
this.
|
|
209
|
+
this.size += size
|
|
210
210
|
return offset
|
|
211
211
|
}
|
|
212
212
|
|
|
213
|
+
/**
|
|
214
|
+
* Rechecks writability for a filled reservation
|
|
215
|
+
*/
|
|
216
|
+
public commit(): void {
|
|
217
|
+
if (this.writable && this.size > this.options.highWaterMark) {
|
|
218
|
+
this.writable = false
|
|
219
|
+
this.options.onWritabilityChanged(false)
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
213
223
|
/**
|
|
214
224
|
* Turns the open staged run into one entry, so later writes queue behind it
|
|
215
225
|
*/
|
|
@@ -400,9 +410,9 @@ export class OutboundBuffer {
|
|
|
400
410
|
}
|
|
401
411
|
|
|
402
412
|
while (this.flushed.length > 0 && !this.closed) {
|
|
403
|
-
const gathered = this.transport.datagram ?
|
|
413
|
+
const gathered = this.transport.datagram ? 0 : this.gather()
|
|
404
414
|
const progressed =
|
|
405
|
-
gathered ===
|
|
415
|
+
gathered === 0 ? this.writeOne() : this.writeMany(gathered)
|
|
406
416
|
if (!progressed) return
|
|
407
417
|
}
|
|
408
418
|
}
|
|
@@ -467,12 +477,18 @@ export class OutboundBuffer {
|
|
|
467
477
|
|
|
468
478
|
/**
|
|
469
479
|
* One syscall for lots of small writes, true if the socket took all of it
|
|
470
|
-
* @param bytes
|
|
480
|
+
* @param size bytes gathered
|
|
471
481
|
*/
|
|
472
|
-
private writeMany(
|
|
482
|
+
private writeMany(size: number): boolean {
|
|
483
|
+
const transport = this.transport
|
|
484
|
+
const scratch = OutboundBuffer.scratch
|
|
485
|
+
|
|
473
486
|
let written: number
|
|
474
487
|
try {
|
|
475
|
-
written =
|
|
488
|
+
written =
|
|
489
|
+
transport.writeRange === undefined
|
|
490
|
+
? transport.write(scratch.subarray(0, size), null)
|
|
491
|
+
: transport.writeRange(scratch, 0, size)
|
|
476
492
|
} catch (cause) {
|
|
477
493
|
this.fail(new Error('Write was refused', { cause }))
|
|
478
494
|
return false
|
|
@@ -484,18 +500,18 @@ export class OutboundBuffer {
|
|
|
484
500
|
}
|
|
485
501
|
|
|
486
502
|
this.consume(written)
|
|
487
|
-
return written >=
|
|
503
|
+
return written >= size
|
|
488
504
|
}
|
|
489
505
|
|
|
490
506
|
/**
|
|
491
|
-
*
|
|
507
|
+
* Packs entries into the scratch buffer, or 0
|
|
492
508
|
*/
|
|
493
|
-
private gather():
|
|
509
|
+
private gather(): number {
|
|
494
510
|
const first = this.flushed[0]
|
|
495
511
|
const second = this.flushed[1]
|
|
496
|
-
if (first === undefined || second === undefined) return
|
|
512
|
+
if (first === undefined || second === undefined) return 0
|
|
497
513
|
if (first.bytes.length + second.bytes.length > OutboundBuffer.gatherLimit) {
|
|
498
|
-
return
|
|
514
|
+
return 0
|
|
499
515
|
}
|
|
500
516
|
|
|
501
517
|
let size = 0
|
|
@@ -505,7 +521,7 @@ export class OutboundBuffer {
|
|
|
505
521
|
size += entry.bytes.length
|
|
506
522
|
}
|
|
507
523
|
|
|
508
|
-
return
|
|
524
|
+
return size
|
|
509
525
|
}
|
|
510
526
|
|
|
511
527
|
/**
|
|
@@ -60,6 +60,16 @@ export class Pipeline {
|
|
|
60
60
|
*/
|
|
61
61
|
private readonly lifecycles: Set<Promise<void>>
|
|
62
62
|
|
|
63
|
+
/**
|
|
64
|
+
* Rejection callback for watch
|
|
65
|
+
*/
|
|
66
|
+
private readonly refused: (error: unknown) => void
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Last promise watch was given, or null
|
|
70
|
+
*/
|
|
71
|
+
private watched: Promise<void> | null
|
|
72
|
+
|
|
63
73
|
/**
|
|
64
74
|
* Starts with just a head and tail linked together
|
|
65
75
|
* @param channel the owner
|
|
@@ -76,6 +86,10 @@ export class Pipeline {
|
|
|
76
86
|
this.tail = new HandlerContext(this, 'tail', new TailHandler(logger))
|
|
77
87
|
this.contexts = new Map<string, HandlerContext>()
|
|
78
88
|
this.lifecycles = new Set<Promise<void>>()
|
|
89
|
+
this.watched = null
|
|
90
|
+
this.refused = (error: unknown): void => {
|
|
91
|
+
channel.handleWriteFailure(error)
|
|
92
|
+
}
|
|
79
93
|
|
|
80
94
|
this.head.next = this.tail
|
|
81
95
|
this.tail.previous = this.head
|
|
@@ -140,7 +154,7 @@ export class Pipeline {
|
|
|
140
154
|
}
|
|
141
155
|
|
|
142
156
|
/**
|
|
143
|
-
* Swaps
|
|
157
|
+
* Swaps a handler in place
|
|
144
158
|
* @param base name of the handler to swap out
|
|
145
159
|
* @param name for the new one, may reuse base
|
|
146
160
|
* @param handler the replacement
|
|
@@ -154,9 +168,24 @@ export class Pipeline {
|
|
|
154
168
|
const reason = name === base ? null : this.refusal(name)
|
|
155
169
|
if (reason !== null) throw new Error(reason)
|
|
156
170
|
|
|
157
|
-
const
|
|
158
|
-
|
|
159
|
-
|
|
171
|
+
const replacement = new HandlerContext(this, name, handler)
|
|
172
|
+
const { previous, next } = context
|
|
173
|
+
replacement.previous = previous
|
|
174
|
+
replacement.next = next
|
|
175
|
+
if (previous !== null) previous.next = replacement
|
|
176
|
+
if (next !== null) next.previous = replacement
|
|
177
|
+
context.previous = replacement
|
|
178
|
+
context.next = replacement
|
|
179
|
+
this.contexts.delete(base)
|
|
180
|
+
this.contexts.set(name, replacement)
|
|
181
|
+
this.version += 1
|
|
182
|
+
|
|
183
|
+
this.track(replacement, (): void | Promise<void> =>
|
|
184
|
+
handler.handlerAdded?.(replacement),
|
|
185
|
+
)
|
|
186
|
+
this.track(context, (): void | Promise<void> =>
|
|
187
|
+
context.handler.handlerRemoved?.(context),
|
|
188
|
+
)
|
|
160
189
|
return context.handler
|
|
161
190
|
}
|
|
162
191
|
|
|
@@ -290,7 +319,7 @@ export class Pipeline {
|
|
|
290
319
|
}
|
|
291
320
|
|
|
292
321
|
/**
|
|
293
|
-
*
|
|
322
|
+
* Writes and flushes without waiting
|
|
294
323
|
* @param message what to send
|
|
295
324
|
*/
|
|
296
325
|
public writeAndFlushSync(message: unknown): void {
|
|
@@ -320,6 +349,17 @@ export class Pipeline {
|
|
|
320
349
|
return this.tail.close()
|
|
321
350
|
}
|
|
322
351
|
|
|
352
|
+
/**
|
|
353
|
+
* Reports a sync write's failure
|
|
354
|
+
* @param promise what the outbound handler returned
|
|
355
|
+
*/
|
|
356
|
+
public watch(promise: Promise<void>): void {
|
|
357
|
+
if (promise === this.watched) return
|
|
358
|
+
|
|
359
|
+
this.watched = promise
|
|
360
|
+
void promise.then(undefined, this.refused)
|
|
361
|
+
}
|
|
362
|
+
|
|
323
363
|
/**
|
|
324
364
|
* Error text if the name is empty or taken, or null
|
|
325
365
|
* @param name proposed handler name
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { pending, resolved } from '../resolved/index.ts'
|
|
1
|
+
import { adopt, pending, resolved } from '../resolved/index.ts'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Lets the owner react when work starts piling up and when it clears
|
|
@@ -119,7 +119,8 @@ export class EventQueue {
|
|
|
119
119
|
return resolved
|
|
120
120
|
}
|
|
121
121
|
|
|
122
|
-
|
|
122
|
+
const waiting = result instanceof Promise ? result : adopt(result)
|
|
123
|
+
return waiting.finally((): void => {
|
|
123
124
|
this.drain()
|
|
124
125
|
})
|
|
125
126
|
}
|
|
@@ -3,12 +3,22 @@
|
|
|
3
3
|
*/
|
|
4
4
|
export const resolved: Promise<void> = Promise.resolve()
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* Turns a handler's result into a promise
|
|
8
|
+
* @param result a plain value or a thenable
|
|
9
|
+
*/
|
|
10
|
+
export const adopt = (result: unknown): Promise<void> =>
|
|
11
|
+
Promise.resolve<unknown>(result).then<undefined>((): undefined => undefined)
|
|
12
|
+
|
|
6
13
|
/**
|
|
7
14
|
* Null if there's nothing left to await, otherwise the promise itself
|
|
8
15
|
* @param result what a handler returned
|
|
9
16
|
*/
|
|
10
|
-
export const pending = (result: void | Promise<void>): Promise<void> | null =>
|
|
11
|
-
result === undefined || result === resolved
|
|
17
|
+
export const pending = (result: void | Promise<void>): Promise<void> | null => {
|
|
18
|
+
if (result === undefined || result === resolved) return null
|
|
19
|
+
|
|
20
|
+
return result instanceof Promise ? result : adopt(result)
|
|
21
|
+
}
|
|
12
22
|
|
|
13
23
|
/**
|
|
14
24
|
* Promises we've already attached a catch to
|
|
@@ -21,7 +31,7 @@ const guarded = new WeakSet<Promise<void>>()
|
|
|
21
31
|
const recent: { promise: Promise<void> | null } = { promise: resolved }
|
|
22
32
|
|
|
23
33
|
/**
|
|
24
|
-
*
|
|
34
|
+
* Swallows rejections nobody's listening for
|
|
25
35
|
* @param promise one the caller might never await
|
|
26
36
|
*/
|
|
27
37
|
export const protect = (promise: Promise<void>): Promise<void> => {
|
|
@@ -45,7 +45,7 @@ export class ServerChannel {
|
|
|
45
45
|
private readonly bound: ServerAddress
|
|
46
46
|
|
|
47
47
|
/**
|
|
48
|
-
*
|
|
48
|
+
* Every child the server has
|
|
49
49
|
*/
|
|
50
50
|
private readonly children: Set<Channel>
|
|
51
51
|
|
|
@@ -111,7 +111,7 @@ export class ServerChannel {
|
|
|
111
111
|
}
|
|
112
112
|
|
|
113
113
|
/**
|
|
114
|
-
*
|
|
114
|
+
* Copy of every current connection
|
|
115
115
|
*/
|
|
116
116
|
public get channels(): readonly Channel[] {
|
|
117
117
|
return [...this.children]
|
|
@@ -132,17 +132,19 @@ export class ServerChannel {
|
|
|
132
132
|
void channel.pipeline.fireUserEventTriggered(event)
|
|
133
133
|
}
|
|
134
134
|
|
|
135
|
+
const expiry = Promise.withResolvers<undefined>()
|
|
136
|
+
const timer = setTimeout((): void => {
|
|
137
|
+
expiry.resolve(undefined)
|
|
138
|
+
}, timeout)
|
|
135
139
|
await Promise.race([
|
|
136
140
|
Promise.allSettled(
|
|
137
141
|
channels.map((channel: Channel): Promise<void> => channel.closed),
|
|
138
142
|
),
|
|
139
|
-
|
|
143
|
+
expiry.promise,
|
|
140
144
|
])
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
),
|
|
145
|
-
)
|
|
145
|
+
clearTimeout(timer)
|
|
146
|
+
|
|
147
|
+
await this.closeChildren()
|
|
146
148
|
this.closing.resolve(undefined)
|
|
147
149
|
}
|
|
148
150
|
|
|
@@ -154,11 +156,18 @@ export class ServerChannel {
|
|
|
154
156
|
this.stopped = true
|
|
155
157
|
|
|
156
158
|
this.listener.stop(false)
|
|
159
|
+
await this.closeChildren()
|
|
160
|
+
this.closing.resolve(undefined)
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Closes every child and waits
|
|
165
|
+
*/
|
|
166
|
+
private async closeChildren(): Promise<void> {
|
|
157
167
|
await Promise.allSettled(
|
|
158
168
|
[...this.children].map((channel: Channel): Promise<void> =>
|
|
159
|
-
channel.close(),
|
|
169
|
+
channel.close().then((): Promise<void> => channel.closed),
|
|
160
170
|
),
|
|
161
171
|
)
|
|
162
|
-
this.closing.resolve(undefined)
|
|
163
172
|
}
|
|
164
173
|
}
|
package/src/cluster/index.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { ConsoleLogger } from '../logger/index.ts'
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* What to run and how many copies
|
|
3
5
|
*/
|
|
@@ -74,7 +76,7 @@ export class Cluster {
|
|
|
74
76
|
private static readonly readyMessage: string = 'netify:ready'
|
|
75
77
|
|
|
76
78
|
/**
|
|
77
|
-
* A worker that
|
|
79
|
+
* A worker that lives this long counts as started
|
|
78
80
|
*/
|
|
79
81
|
private static readonly stableAfter: number = 10_000
|
|
80
82
|
|
|
@@ -109,12 +111,12 @@ export class Cluster {
|
|
|
109
111
|
private readonly processes: (Worker | null)[]
|
|
110
112
|
|
|
111
113
|
/**
|
|
112
|
-
*
|
|
114
|
+
* Readiness of every live worker
|
|
113
115
|
*/
|
|
114
116
|
private readonly readiness: Map<Worker, PromiseWithResolvers<boolean>>
|
|
115
117
|
|
|
116
118
|
/**
|
|
117
|
-
* When each worker started
|
|
119
|
+
* When each worker started
|
|
118
120
|
*/
|
|
119
121
|
private readonly births: Map<Worker, number>
|
|
120
122
|
|
|
@@ -133,6 +135,11 @@ export class Cluster {
|
|
|
133
135
|
*/
|
|
134
136
|
private running: boolean
|
|
135
137
|
|
|
138
|
+
/**
|
|
139
|
+
* Reload in progress
|
|
140
|
+
*/
|
|
141
|
+
private reloading: boolean
|
|
142
|
+
|
|
136
143
|
/**
|
|
137
144
|
* Doesn't spawn anything yet, call start for that
|
|
138
145
|
* @param options script, worker count and restart policy
|
|
@@ -150,6 +157,7 @@ export class Cluster {
|
|
|
150
157
|
this.crashes = Array.from({ length: this.count }, (): number => 0)
|
|
151
158
|
this.pending = Array.from({ length: this.count }, (): null => null)
|
|
152
159
|
this.running = false
|
|
160
|
+
this.reloading = false
|
|
153
161
|
}
|
|
154
162
|
|
|
155
163
|
/**
|
|
@@ -214,9 +222,15 @@ export class Cluster {
|
|
|
214
222
|
process.once('SIGTERM', (): void => {
|
|
215
223
|
void Promise.resolve()
|
|
216
224
|
.then(callback)
|
|
217
|
-
.
|
|
218
|
-
|
|
219
|
-
|
|
225
|
+
.then(
|
|
226
|
+
(): void => {
|
|
227
|
+
process.exit(0)
|
|
228
|
+
},
|
|
229
|
+
(err: unknown): void => {
|
|
230
|
+
new ConsoleLogger().warn({ err }, 'Stop callback failed')
|
|
231
|
+
process.exit(1)
|
|
232
|
+
},
|
|
233
|
+
)
|
|
220
234
|
})
|
|
221
235
|
}
|
|
222
236
|
|
|
@@ -255,7 +269,7 @@ export class Cluster {
|
|
|
255
269
|
await Promise.all(
|
|
256
270
|
this.processes
|
|
257
271
|
.filter((child: Worker | null): child is Worker => child !== null)
|
|
258
|
-
.map((child: Worker): Promise<
|
|
272
|
+
.map((child: Worker): Promise<boolean> => this.waitReady(child)),
|
|
259
273
|
)
|
|
260
274
|
}
|
|
261
275
|
|
|
@@ -271,9 +285,7 @@ export class Cluster {
|
|
|
271
285
|
|
|
272
286
|
this.pending.fill(null)
|
|
273
287
|
|
|
274
|
-
const children = this.
|
|
275
|
-
(child: Worker | null): child is Worker => child !== null,
|
|
276
|
-
)
|
|
288
|
+
const children = [...this.readiness.keys()]
|
|
277
289
|
this.processes.fill(null)
|
|
278
290
|
await Promise.all(
|
|
279
291
|
children.map((child: Worker): Promise<void> => this.retire(child)),
|
|
@@ -281,28 +293,58 @@ export class Cluster {
|
|
|
281
293
|
}
|
|
282
294
|
|
|
283
295
|
/**
|
|
284
|
-
* Replaces
|
|
296
|
+
* Replaces workers one at a time
|
|
285
297
|
* @param order start-first for shared ports, stop-first when each worker has its own
|
|
286
298
|
*/
|
|
287
299
|
public async reload(order: ReloadOrder = 'start-first'): Promise<void> {
|
|
288
300
|
if (!this.running) throw new Error('Cluster is not running')
|
|
301
|
+
if (this.reloading) throw new Error('Cluster is reloading')
|
|
302
|
+
this.reloading = true
|
|
303
|
+
|
|
304
|
+
try {
|
|
305
|
+
for (let index = 0; index < this.count; index++) {
|
|
306
|
+
const ready =
|
|
307
|
+
order === 'stop-first'
|
|
308
|
+
? await this.replaceAfter(index)
|
|
309
|
+
: await this.replaceBefore(index)
|
|
310
|
+
if (!this.isRunning) throw new Error('Cluster is not running')
|
|
311
|
+
if (!ready) throw new Error('Worker is not ready')
|
|
312
|
+
}
|
|
313
|
+
} finally {
|
|
314
|
+
this.reloading = false
|
|
315
|
+
}
|
|
316
|
+
}
|
|
289
317
|
|
|
290
|
-
|
|
291
|
-
|
|
318
|
+
/**
|
|
319
|
+
* Swaps one slot's worker, false if the new one isn't ready
|
|
320
|
+
* @param index the slot
|
|
321
|
+
*/
|
|
322
|
+
private async replaceAfter(index: number): Promise<boolean> {
|
|
323
|
+
const old = this.processes[index] ?? null
|
|
324
|
+
this.processes[index] = null
|
|
325
|
+
if (old !== null) await this.retire(old)
|
|
326
|
+
if (!this.running) return false
|
|
292
327
|
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
328
|
+
const fresh = this.spawn(index)
|
|
329
|
+
this.processes[index] = fresh
|
|
330
|
+
return this.waitReady(fresh)
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Brings the new worker up first, false if it fails
|
|
335
|
+
* @param index the slot
|
|
336
|
+
*/
|
|
337
|
+
private async replaceBefore(index: number): Promise<boolean> {
|
|
338
|
+
const fresh = this.spawn(index)
|
|
339
|
+
if (!(await this.waitReady(fresh)) || !this.running) {
|
|
340
|
+
await this.retire(fresh)
|
|
341
|
+
return false
|
|
305
342
|
}
|
|
343
|
+
|
|
344
|
+
const old = this.processes[index] ?? null
|
|
345
|
+
this.processes[index] = fresh
|
|
346
|
+
if (old !== null) await this.retire(old)
|
|
347
|
+
return true
|
|
306
348
|
}
|
|
307
349
|
|
|
308
350
|
/**
|
|
@@ -343,8 +385,6 @@ export class Cluster {
|
|
|
343
385
|
ipc: (message: unknown): void => {
|
|
344
386
|
if (message === Cluster.readyMessage) {
|
|
345
387
|
this.readiness.get(child)?.resolve(true)
|
|
346
|
-
this.births.delete(child)
|
|
347
|
-
this.crashes[index] = 0
|
|
348
388
|
}
|
|
349
389
|
},
|
|
350
390
|
/**
|
|
@@ -372,7 +412,7 @@ export class Cluster {
|
|
|
372
412
|
}
|
|
373
413
|
|
|
374
414
|
/**
|
|
375
|
-
*
|
|
415
|
+
* Restarts a crashed worker
|
|
376
416
|
* @param index the slot
|
|
377
417
|
* @param lived ms the worker ran for
|
|
378
418
|
*/
|
|
@@ -402,19 +442,20 @@ export class Cluster {
|
|
|
402
442
|
}
|
|
403
443
|
|
|
404
444
|
/**
|
|
405
|
-
* Waits for
|
|
445
|
+
* Waits for ready, false if it never comes
|
|
406
446
|
* @param child the worker
|
|
407
447
|
*/
|
|
408
|
-
private async waitReady(child: Worker): Promise<
|
|
448
|
+
private async waitReady(child: Worker): Promise<boolean> {
|
|
409
449
|
const readiness = this.readiness.get(child)
|
|
410
|
-
if (readiness === undefined) return
|
|
450
|
+
if (readiness === undefined) return false
|
|
411
451
|
|
|
412
452
|
const timeout = this.options.readyTimeout ?? 10000
|
|
413
453
|
const timer = setTimeout((): void => {
|
|
414
454
|
readiness.resolve(false)
|
|
415
455
|
}, timeout)
|
|
416
|
-
await readiness.promise
|
|
456
|
+
const ready = await readiness.promise
|
|
417
457
|
clearTimeout(timer)
|
|
458
|
+
return ready
|
|
418
459
|
}
|
|
419
460
|
|
|
420
461
|
/**
|
|
@@ -99,4 +99,23 @@ export class BinaryCodec<T> extends DuplexHandlerAdapter {
|
|
|
99
99
|
|
|
100
100
|
return context.write(bytes)
|
|
101
101
|
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Encodes and writes without a promise, false for other messages
|
|
105
|
+
* @param context where bytes go
|
|
106
|
+
* @param message whatever is going out
|
|
107
|
+
*/
|
|
108
|
+
public writeSync(context: HandlerContext, message: unknown): boolean {
|
|
109
|
+
if (!this.takes(message)) return false
|
|
110
|
+
|
|
111
|
+
let bytes: Uint8Array
|
|
112
|
+
try {
|
|
113
|
+
bytes = this.encoder(message)
|
|
114
|
+
} catch (cause) {
|
|
115
|
+
throw new EncoderError('Encoding failed', { cause })
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
context.writeSync(bytes)
|
|
119
|
+
return true
|
|
120
|
+
}
|
|
102
121
|
}
|