@netifyjs/netify 1.0.0-alpha.4 → 1.0.0-alpha.6
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 +230 -89
- 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 +15 -0
- package/src/worker/group/index.ts +231 -13
- package/src/worker/pool/index.ts +100 -19
- package/src/worker/service/index.ts +9 -1
package/src/worker/pool/index.ts
CHANGED
|
@@ -81,9 +81,34 @@ export class WorkerPool<I, O> {
|
|
|
81
81
|
public static readonly defaultMaxQueued: number = 1024
|
|
82
82
|
|
|
83
83
|
/**
|
|
84
|
-
*
|
|
84
|
+
* Wait before a thread's second quick restart
|
|
85
85
|
*/
|
|
86
|
-
private readonly
|
|
86
|
+
private static readonly firstBackoff: number = 250
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Longest wait between restarts
|
|
90
|
+
*/
|
|
91
|
+
private static readonly maxBackoff: number = 10_000
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* What each thread runs
|
|
95
|
+
*/
|
|
96
|
+
private readonly script: string | URL
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Each thread, or null
|
|
100
|
+
*/
|
|
101
|
+
private readonly workers: (Worker | null)[]
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Deaths in a row, per thread
|
|
105
|
+
*/
|
|
106
|
+
private readonly failures: number[]
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Pending restart timers, per thread
|
|
110
|
+
*/
|
|
111
|
+
private readonly pending: (Timer | null)[]
|
|
87
112
|
|
|
88
113
|
/**
|
|
89
114
|
* Jobs in flight per worker
|
|
@@ -124,10 +149,13 @@ export class WorkerPool<I, O> {
|
|
|
124
149
|
this.jobs = new Map<number, Job<O>>()
|
|
125
150
|
this.nextId = 1
|
|
126
151
|
this.closed = false
|
|
152
|
+
this.script = options.script
|
|
127
153
|
this.load = Array.from({ length: count }, (): number => 0)
|
|
154
|
+
this.failures = Array.from({ length: count }, (): number => 0)
|
|
155
|
+
this.pending = Array.from({ length: count }, (): null => null)
|
|
128
156
|
this.workers = Array.from(
|
|
129
157
|
{ length: count },
|
|
130
|
-
(_: unknown, index: number): Worker => this.spawn(
|
|
158
|
+
(_: unknown, index: number): Worker => this.spawn(index),
|
|
131
159
|
)
|
|
132
160
|
}
|
|
133
161
|
|
|
@@ -150,12 +178,19 @@ export class WorkerPool<I, O> {
|
|
|
150
178
|
}
|
|
151
179
|
|
|
152
180
|
const worker = this.leastBusy()
|
|
153
|
-
const
|
|
181
|
+
const thread = this.workers[worker] ?? null
|
|
182
|
+
if (thread === null) return Promise.reject(new Error('Pool has no workers'))
|
|
183
|
+
|
|
184
|
+
const request: WorkerRequest<I> = { id: this.nextId++, input }
|
|
185
|
+
try {
|
|
186
|
+
thread.postMessage(request, [...transfer])
|
|
187
|
+
} catch (cause) {
|
|
188
|
+
return Promise.reject(new Error('Input is invalid', { cause }))
|
|
189
|
+
}
|
|
190
|
+
|
|
154
191
|
const job: Job<O> = { result: Promise.withResolvers<O>(), worker }
|
|
155
|
-
this.jobs.set(id, job)
|
|
192
|
+
this.jobs.set(request.id, job)
|
|
156
193
|
this.load[worker] = (this.load[worker] ?? 0) + 1
|
|
157
|
-
const request: WorkerRequest<I> = { id, input }
|
|
158
|
-
this.workers[worker]?.postMessage(request, [...transfer])
|
|
159
194
|
return job.result.promise
|
|
160
195
|
}
|
|
161
196
|
|
|
@@ -165,7 +200,12 @@ export class WorkerPool<I, O> {
|
|
|
165
200
|
public close(): Promise<void> {
|
|
166
201
|
this.closed = true
|
|
167
202
|
|
|
168
|
-
for (const
|
|
203
|
+
for (const timer of this.pending) {
|
|
204
|
+
if (timer !== null) clearTimeout(timer)
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
this.pending.fill(null)
|
|
208
|
+
for (const worker of this.workers) worker?.terminate()
|
|
169
209
|
for (const job of this.jobs.values()) {
|
|
170
210
|
job.result.reject(new Error('Pool is closed'))
|
|
171
211
|
}
|
|
@@ -176,21 +216,58 @@ export class WorkerPool<I, O> {
|
|
|
176
216
|
|
|
177
217
|
/**
|
|
178
218
|
* Starts one thread and wires up its replies
|
|
179
|
-
* @param script what the thread runs
|
|
180
219
|
* @param index its slot in the pool
|
|
181
220
|
*/
|
|
182
|
-
private spawn(
|
|
183
|
-
const worker = new Worker(script)
|
|
221
|
+
private spawn(index: number): Worker {
|
|
222
|
+
const worker = new Worker(this.script)
|
|
184
223
|
|
|
185
224
|
worker.onmessage = (event: MessageEvent<WorkerReply<O>>): void => {
|
|
186
225
|
this.settle(event.data)
|
|
187
226
|
}
|
|
188
227
|
worker.onerror = (event: ErrorEvent): void => {
|
|
189
|
-
|
|
228
|
+
const cause: unknown = event.error ?? new Error(event.message)
|
|
229
|
+
this.replace(index, worker, new Error('Worker failed', { cause }))
|
|
190
230
|
}
|
|
231
|
+
worker.addEventListener('close', (): void => {
|
|
232
|
+
this.replace(index, worker, new Error('Worker failed'))
|
|
233
|
+
})
|
|
191
234
|
return worker
|
|
192
235
|
}
|
|
193
236
|
|
|
237
|
+
/**
|
|
238
|
+
* Swaps a dead thread for a new one
|
|
239
|
+
* @param index the thread's slot
|
|
240
|
+
* @param worker the thread that died
|
|
241
|
+
* @param error why it failed
|
|
242
|
+
*/
|
|
243
|
+
private replace(index: number, worker: Worker, error: Error): void {
|
|
244
|
+
if (this.closed || this.workers[index] !== worker) return
|
|
245
|
+
this.workers[index] = null
|
|
246
|
+
worker.terminate()
|
|
247
|
+
this.fail(index, error)
|
|
248
|
+
|
|
249
|
+
const failures = (this.failures[index] ?? 0) + 1
|
|
250
|
+
this.failures[index] = failures
|
|
251
|
+
const delay =
|
|
252
|
+
failures <= 1
|
|
253
|
+
? 0
|
|
254
|
+
: Math.min(
|
|
255
|
+
WorkerPool.maxBackoff,
|
|
256
|
+
WorkerPool.firstBackoff * 2 ** (failures - 2),
|
|
257
|
+
)
|
|
258
|
+
if (delay === 0) {
|
|
259
|
+
this.workers[index] = this.spawn(index)
|
|
260
|
+
return
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
const timer = setTimeout((): void => {
|
|
264
|
+
this.pending[index] = null
|
|
265
|
+
this.workers[index] = this.spawn(index)
|
|
266
|
+
}, delay)
|
|
267
|
+
timer.unref()
|
|
268
|
+
this.pending[index] = timer
|
|
269
|
+
}
|
|
270
|
+
|
|
194
271
|
/**
|
|
195
272
|
* Resolves or rejects the job a reply belongs to
|
|
196
273
|
* @param reply what the worker sent
|
|
@@ -200,17 +277,18 @@ export class WorkerPool<I, O> {
|
|
|
200
277
|
if (job === undefined) return
|
|
201
278
|
this.jobs.delete(reply.id)
|
|
202
279
|
this.load[job.worker] = (this.load[job.worker] ?? 1) - 1
|
|
280
|
+
this.failures[job.worker] = 0
|
|
203
281
|
|
|
204
282
|
if (reply.error !== undefined) job.result.reject(new Error(reply.error))
|
|
205
283
|
else job.result.resolve(reply.output as O)
|
|
206
284
|
}
|
|
207
285
|
|
|
208
286
|
/**
|
|
209
|
-
* Fails every job a
|
|
210
|
-
* @param index the
|
|
287
|
+
* Fails every job a dead thread had
|
|
288
|
+
* @param index the thread's slot
|
|
211
289
|
* @param error why it failed
|
|
212
290
|
*/
|
|
213
|
-
private
|
|
291
|
+
private fail(index: number, error: Error): void {
|
|
214
292
|
for (const [id, job] of this.jobs) {
|
|
215
293
|
if (job.worker !== index) continue
|
|
216
294
|
this.jobs.delete(id)
|
|
@@ -221,13 +299,16 @@ export class WorkerPool<I, O> {
|
|
|
221
299
|
}
|
|
222
300
|
|
|
223
301
|
/**
|
|
224
|
-
* Slot of the
|
|
302
|
+
* Slot of the live thread with the fewest jobs, or -1
|
|
225
303
|
*/
|
|
226
304
|
private leastBusy(): number {
|
|
227
|
-
let best =
|
|
305
|
+
let best = -1
|
|
228
306
|
|
|
229
|
-
for (let index =
|
|
230
|
-
if (
|
|
307
|
+
for (let index = 0; index < this.load.length; index++) {
|
|
308
|
+
if (this.workers[index] === null) continue
|
|
309
|
+
if (best < 0 || (this.load[index] ?? 0) < (this.load[best] ?? 0)) {
|
|
310
|
+
best = index
|
|
311
|
+
}
|
|
231
312
|
}
|
|
232
313
|
|
|
233
314
|
return best
|