@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.
Files changed (72) hide show
  1. package/README.md +6 -6
  2. package/package.json +1 -1
  3. package/src/bootstrap/client/index.ts +1 -1
  4. package/src/bootstrap/dispatcher/index.ts +7 -0
  5. package/src/bootstrap/reconnect/index.ts +14 -4
  6. package/src/bootstrap/server/index.ts +10 -4
  7. package/src/buffer/index.ts +230 -89
  8. package/src/buffer/quantizer/index.ts +16 -3
  9. package/src/channel/context/index.ts +18 -11
  10. package/src/channel/handler/index.ts +1 -1
  11. package/src/channel/index.ts +30 -7
  12. package/src/channel/outbound/index.ts +28 -12
  13. package/src/channel/pipeline/index.ts +45 -5
  14. package/src/channel/queue/index.ts +3 -2
  15. package/src/channel/resolved/index.ts +13 -3
  16. package/src/channel/server/index.ts +19 -10
  17. package/src/cluster/index.ts +73 -32
  18. package/src/codec/binary/index.ts +19 -0
  19. package/src/codec/decoder/index.ts +223 -83
  20. package/src/codec/delimiter/index.ts +95 -37
  21. package/src/codec/encoder/index.ts +30 -5
  22. package/src/codec/fixed/index.ts +5 -1
  23. package/src/codec/http/aggregator/index.ts +45 -16
  24. package/src/codec/http/decoder/object/index.ts +156 -40
  25. package/src/codec/http/decoder/request/index.ts +6 -2
  26. package/src/codec/http/encoder/object/index.ts +2 -7
  27. package/src/codec/http/encoder/request/index.ts +5 -0
  28. package/src/codec/http/headers/index.ts +45 -2
  29. package/src/codec/http/message/full/request/index.ts +1 -1
  30. package/src/codec/http/message/full/response/index.ts +1 -1
  31. package/src/codec/http/message/request/index.ts +14 -1
  32. package/src/codec/http/message/response/index.ts +1 -1
  33. package/src/codec/http/version/index.ts +3 -0
  34. package/src/codec/length/index.ts +66 -44
  35. package/src/codec/message/decoder/index.ts +18 -1
  36. package/src/codec/message/encoder/index.ts +85 -8
  37. package/src/codec/prepender/index.ts +4 -1
  38. package/src/codec/string/decoder/index.ts +6 -1
  39. package/src/codec/websocket/decoder/index.ts +78 -3
  40. package/src/codec/websocket/encoder/index.ts +90 -6
  41. package/src/codec/websocket/frame/base/index.ts +19 -10
  42. package/src/codec/websocket/frame/text/index.ts +2 -2
  43. package/src/codec/websocket/protocol/index.ts +12 -0
  44. package/src/datagram/bootstrap/index.ts +1 -1
  45. package/src/datagram/transport/index.ts +30 -1
  46. package/src/handler/flush/index.ts +25 -5
  47. package/src/handler/idle/index.ts +120 -26
  48. package/src/handler/logging/index.ts +13 -1
  49. package/src/handler/metrics/index.ts +49 -16
  50. package/src/handler/offload/index.ts +7 -0
  51. package/src/handler/timeout/read/index.ts +1 -1
  52. package/src/handler/timeout/write/index.ts +104 -23
  53. package/src/handler/traffic/index.ts +251 -38
  54. package/src/index.ts +1 -0
  55. package/src/reliable/cipher/index.ts +49 -5
  56. package/src/reliable/cookie/index.ts +64 -2
  57. package/src/reliable/handler/index.ts +11 -15
  58. package/src/reliable/index.ts +7 -1
  59. package/src/reliable/lane/index.ts +277 -58
  60. package/src/reliable/peer/index.ts +125 -38
  61. package/src/serve/bootstrap/index.ts +82 -135
  62. package/src/serve/channel/index.ts +84 -2
  63. package/src/serve/gate/index.ts +215 -0
  64. package/src/serve/index.ts +6 -2
  65. package/src/serve/outbox/index.ts +63 -2
  66. package/src/serve/transport/index.ts +33 -3
  67. package/src/serve/writer/index.ts +4 -3
  68. package/src/shard/gateway/index.ts +102 -29
  69. package/src/shard/link/index.ts +15 -0
  70. package/src/worker/group/index.ts +231 -13
  71. package/src/worker/pool/index.ts +100 -19
  72. package/src/worker/service/index.ts +9 -1
@@ -69,6 +69,26 @@ export type Outgoing = {
69
69
  */
70
70
  export type Deliver = (content: Uint8Array) => void
71
71
 
72
+ /**
73
+ * Caps on what a lane holds
74
+ */
75
+ export type LaneLimits = {
76
+ /**
77
+ * Ids that can be in flight or held at once
78
+ */
79
+ readonly window: number
80
+
81
+ /**
82
+ * Biggest message after joining fragments
83
+ */
84
+ readonly maxMessageSize: number
85
+
86
+ /**
87
+ * Most bytes of unfinished messages
88
+ */
89
+ readonly maxHeldBytes: number
90
+ }
91
+
72
92
  /**
73
93
  * A fragment held until the rest of its message turns up
74
94
  */
@@ -89,6 +109,31 @@ type Piece = {
89
109
  readonly payload: Uint8Array
90
110
  }
91
111
 
112
+ /**
113
+ * A partly arrived unordered message
114
+ */
115
+ type Group = {
116
+ /**
117
+ * Fragments in the whole message
118
+ */
119
+ readonly count: number
120
+
121
+ /**
122
+ * Payloads by fragment position
123
+ */
124
+ readonly pieces: (Uint8Array | undefined)[]
125
+
126
+ /**
127
+ * How many have arrived
128
+ */
129
+ received: number
130
+
131
+ /**
132
+ * Bytes arrived so far
133
+ */
134
+ bytes: number
135
+ }
136
+
92
137
  /**
93
138
  * One lane's send queue and receive state for one peer
94
139
  * @class
@@ -110,9 +155,14 @@ export class Lane {
110
155
  public readonly queue: Outgoing[]
111
156
 
112
157
  /**
113
- * Sent but not acked yet, reliable lanes only
158
+ * Unacked messages in a ring by id
114
159
  */
115
- public readonly inFlight: Map<number, Outgoing>
160
+ private readonly inFlight: (Outgoing | undefined)[]
161
+
162
+ /**
163
+ * Masks an id down to its inFlight slot
164
+ */
165
+ private readonly mask: number
116
166
 
117
167
  /**
118
168
  * Bytes sitting in queue and inFlight
@@ -130,19 +180,34 @@ export class Lane {
130
180
  private nextDeliver: number
131
181
 
132
182
  /**
133
- * Ordered lanes, arrivals held until the gap before them fills
183
+ * Ordered lanes, early arrivals in a ring by id
134
184
  */
135
- private readonly held: Map<number, Piece>
185
+ private readonly held: (Piece | undefined)[]
136
186
 
137
187
  /**
138
- * Unordered lanes, which ids have already been delivered
188
+ * Ordered lanes, held run from nextDeliver
189
+ */
190
+ private ready: number
191
+
192
+ /**
193
+ * Unordered lanes, arrivals from floor on
139
194
  */
140
195
  private readonly seen: Uint8Array
141
196
 
142
197
  /**
143
- * Unordered lanes, fragments by the id of their first piece
198
+ * Unordered lanes, oldest id that hasn't arrived
199
+ */
200
+ private floor: number
201
+
202
+ /**
203
+ * Unordered lanes, unfinished messages by first id
144
204
  */
145
- private readonly groups: Map<number, Piece[]>
205
+ private readonly groups: Map<number, Group>
206
+
207
+ /**
208
+ * Unordered lanes, bytes sitting in groups
209
+ */
210
+ private heldBytes: number
146
211
 
147
212
  /**
148
213
  * Sequenced lanes, newest id delivered
@@ -159,11 +224,26 @@ export class Lane {
159
224
  */
160
225
  private readonly window: number
161
226
 
227
+ /**
228
+ * Biggest message after joining fragments
229
+ */
230
+ private readonly maxMessageSize: number
231
+
232
+ /**
233
+ * Most bytes groups may hold
234
+ */
235
+ private readonly maxHeldBytes: number
236
+
162
237
  /**
163
238
  * Oldest id still waiting for an ack
164
239
  */
165
240
  private oldest: number
166
241
 
242
+ /**
243
+ * First id of the message oldest belongs to
244
+ */
245
+ private origin: number
246
+
167
247
  /**
168
248
  * Next id to go in flight
169
249
  */
@@ -172,37 +252,84 @@ export class Lane {
172
252
  /**
173
253
  * Fresh state for one lane
174
254
  * @param options delivery mode
175
- * @param window ids that can be in flight or held at once
255
+ * @param limits window and memory caps
176
256
  */
177
- public constructor(options: LaneOptions, window: number) {
257
+ public constructor(options: LaneOptions, limits: LaneLimits) {
178
258
  this.delivery = options.delivery
179
259
  this.reliable =
180
260
  options.delivery === 'reliable-ordered' ||
181
261
  options.delivery === 'reliable-unordered'
182
262
  this.queue = []
183
- this.inFlight = new Map<number, Outgoing>()
263
+ this.mask = Lane.ringSize(this.reliable ? limits.window : 1) - 1
264
+ this.inFlight = new Array<Outgoing | undefined>(this.mask + 1).fill(
265
+ undefined,
266
+ )
184
267
  this.queuedBytes = 0
185
268
  this.nextId = 0
186
269
  this.nextDeliver = 0
187
- this.held = new Map<number, Piece>()
270
+ this.held = new Array<Piece | undefined>(
271
+ options.delivery === 'reliable-ordered' ? this.mask + 1 : 0,
272
+ ).fill(undefined)
273
+ this.ready = 0
188
274
  this.seen = new Uint8Array(
189
275
  options.delivery === 'reliable-unordered' ? 65536 : 0,
190
276
  )
191
- this.groups = new Map<number, Piece[]>()
277
+ this.floor = 0
278
+ this.groups = new Map<number, Group>()
279
+ this.heldBytes = 0
192
280
  this.latest = 0
193
281
  this.started = false
194
- this.window = window
282
+ this.window = limits.window
283
+ this.maxMessageSize = limits.maxMessageSize
284
+ this.maxHeldBytes = limits.maxHeldBytes
195
285
  this.oldest = 0
286
+ this.origin = 0
196
287
  this.launched = 0
197
288
  }
198
289
 
290
+ /**
291
+ * Smallest power of two that holds a window
292
+ * @param window ids that can be in flight at once
293
+ */
294
+ private static ringSize(window: number): number {
295
+ let size = 1
296
+ while (size < window) size *= 2
297
+
298
+ return size
299
+ }
300
+
301
+ /**
302
+ * Id of the oldest message still waiting for an ack
303
+ */
304
+ public get flightStart(): number {
305
+ return this.oldest
306
+ }
307
+
308
+ /**
309
+ * Id one past the newest message in flight
310
+ */
311
+ public get flightEnd(): number {
312
+ return this.launched
313
+ }
314
+
315
+ /**
316
+ * Message in flight under an id, or undefined
317
+ * @param id lane id
318
+ */
319
+ public flying(id: number): Outgoing | undefined {
320
+ return this.inFlight[id & this.mask]
321
+ }
322
+
199
323
  /**
200
324
  * Whether another message fits in the send window
201
325
  * @param limit congestion window, can be smaller than ours
202
326
  */
203
327
  public hasRoom(limit: number): boolean {
328
+ const launched = this.launched
329
+
204
330
  return (
205
- ((this.launched - this.oldest) & 0xffff) < Math.min(this.window, limit)
331
+ ((launched - this.oldest) & 0xffff) < limit &&
332
+ ((launched - this.origin) & 0xffff) < this.window
206
333
  )
207
334
  }
208
335
 
@@ -211,7 +338,12 @@ export class Lane {
211
338
  * @param message the next one in the queue
212
339
  */
213
340
  public launch(message: Outgoing): void {
214
- this.inFlight.set(message.id, message)
341
+ if (this.oldest === this.launched) {
342
+ this.oldest = message.id
343
+ this.origin = (message.id - message.index) & 0xffff
344
+ }
345
+
346
+ this.inFlight[message.id & this.mask] = message
215
347
  this.launched = (message.id + 1) & 0xffff
216
348
  }
217
349
 
@@ -248,15 +380,21 @@ export class Lane {
248
380
  if (message.acked) return
249
381
  message.acked = true
250
382
 
251
- this.inFlight.delete(message.id)
383
+ this.inFlight[message.id & this.mask] = undefined
252
384
  this.queuedBytes -= message.payload.length
253
- while (this.oldest !== this.launched && !this.inFlight.has(this.oldest)) {
385
+ if (message.id !== this.oldest) return
386
+
387
+ let head: Outgoing | undefined
388
+ while (this.oldest !== this.launched && head === undefined) {
254
389
  this.oldest = (this.oldest + 1) & 0xffff
390
+ head = this.inFlight[this.oldest & this.mask]
255
391
  }
392
+
393
+ this.origin = (this.oldest - (head?.index ?? 0)) & 0xffff
256
394
  }
257
395
 
258
396
  /**
259
- * Takes in an arrival and hands back whatever can be delivered now
397
+ * Takes an arrival, false if refused
260
398
  * @param id its id on this lane
261
399
  * @param index fragment position
262
400
  * @param count fragments in the whole message
@@ -269,19 +407,18 @@ export class Lane {
269
407
  count: number,
270
408
  payload: Uint8Array,
271
409
  deliver: Deliver,
272
- ): void {
410
+ ): boolean {
273
411
  switch (this.delivery) {
274
412
  case 'unreliable':
275
413
  deliver(payload)
276
- return
414
+ return true
277
415
  case 'unreliable-sequenced':
278
416
  this.sequenced(id, payload, deliver)
279
- return
417
+ return true
280
418
  case 'reliable-unordered':
281
- this.unordered(id, index, count, payload, deliver)
282
- return
419
+ return this.unordered(id, index, count, payload, deliver)
283
420
  case 'reliable-ordered':
284
- this.ordered(id, index, count, payload, deliver)
421
+ return this.ordered(id, index, count, payload, deliver)
285
422
  }
286
423
  }
287
424
 
@@ -313,40 +450,84 @@ export class Lane {
313
450
  count: number,
314
451
  payload: Uint8Array,
315
452
  deliver: Deliver,
316
- ): void {
317
- if (this.seen[id] === 1) return
453
+ ): boolean {
454
+ const ahead = (id - this.floor) & 0xffff
455
+ if (ahead >= 0x8000 || this.seen[id] === 1) return true
456
+ if (ahead >= this.window) return false
457
+
318
458
  if (count === 1) {
319
459
  this.mark(id)
320
460
  deliver(payload)
321
- return
461
+ return true
322
462
  }
323
463
 
324
464
  const base = (id - index) & 0xffff
325
- const group = this.groups.get(base)
326
- if (group === undefined && this.groups.size >= this.window) return
327
- this.mark(id)
465
+ const group = this.groups.get(base) ?? Lane.group(count)
466
+ if (!this.fits(group, index, count, payload.length)) return false
328
467
 
329
- const pieces = group ?? []
330
- pieces.push({ index, count, payload: payload.slice() })
331
- if (pieces.length < count) {
332
- this.groups.set(base, pieces)
333
- return
468
+ this.mark(id)
469
+ group.pieces[index] = payload.slice()
470
+ group.received += 1
471
+ group.bytes += payload.length
472
+ this.heldBytes += payload.length
473
+ if (group.received < count) {
474
+ this.groups.set(base, group)
475
+ return true
334
476
  }
335
477
 
336
478
  this.groups.delete(base)
337
- pieces.sort(
338
- (first: Piece, second: Piece): number => first.index - second.index,
339
- )
340
- deliver(Lane.join(pieces))
479
+ this.heldBytes -= group.bytes
480
+ deliver(Lane.concat(group))
481
+ return true
482
+ }
483
+
484
+ /**
485
+ * An empty group
486
+ * @param count fragments in the whole message
487
+ */
488
+ private static group(count: number): Group {
489
+ return {
490
+ count,
491
+ pieces: new Array<Uint8Array | undefined>(count),
492
+ received: 0,
493
+ bytes: 0,
494
+ }
341
495
  }
342
496
 
343
497
  /**
344
- * Notes an id as delivered and frees the one half a lap away
498
+ * Whether a fragment fits its group
499
+ * @param group the message it claims to be part of
500
+ * @param index fragment position
501
+ * @param count fragments it says the message has
502
+ * @param size bytes it carries
503
+ */
504
+ private fits(
505
+ group: Group,
506
+ index: number,
507
+ count: number,
508
+ size: number,
509
+ ): boolean {
510
+ if (group.count !== count || group.pieces[index] !== undefined) return false
511
+ if (group.bytes + size > this.maxMessageSize) return false
512
+
513
+ return this.heldBytes + size <= this.maxHeldBytes
514
+ }
515
+
516
+ /**
517
+ * Marks an id arrived and moves floor up
345
518
  * @param id its id
346
519
  */
347
520
  private mark(id: number): void {
348
- this.seen[id] = 1
349
- this.seen[(id + 32768) & 0xffff] = 0
521
+ const seen = this.seen
522
+ seen[id] = 1
523
+
524
+ let floor = this.floor
525
+ while (seen[floor] === 1) {
526
+ seen[floor] = 0
527
+ floor = (floor + 1) & 0xffff
528
+ }
529
+
530
+ this.floor = floor
350
531
  }
351
532
 
352
533
  /**
@@ -363,41 +544,79 @@ export class Lane {
363
544
  count: number,
364
545
  payload: Uint8Array,
365
546
  deliver: Deliver,
366
- ): void {
547
+ ): boolean {
367
548
  const ahead = (id - this.nextDeliver) & 0xffff
368
- if (ahead >= this.window || this.held.has(id)) return
549
+ if (ahead >= 0x8000) return true
550
+ if (ahead >= this.window) return false
551
+ if (this.held[id & this.mask] !== undefined) return true
369
552
 
370
553
  if (ahead === 0 && count === 1) {
371
554
  deliver(payload)
372
555
  this.nextDeliver = (this.nextDeliver + 1) & 0xffff
373
556
  } else {
374
- this.held.set(id, { index, count, payload: payload.slice() })
557
+ this.held[id & this.mask] = { index, count, payload: payload.slice() }
558
+ if (ahead !== this.ready) return true
559
+ }
560
+
561
+ this.release(deliver)
562
+ return true
563
+ }
564
+
565
+ /**
566
+ * Delivers whatever's ready, in order
567
+ * @param deliver gets each message in order
568
+ */
569
+ private release(deliver: Deliver): void {
570
+ const held = this.held
571
+ const mask = this.mask
572
+ let ready = this.ready
573
+ while (
574
+ ready <= mask &&
575
+ held[(this.nextDeliver + ready) & mask] !== undefined
576
+ ) {
577
+ ready += 1
375
578
  }
376
579
 
377
- for (;;) {
378
- const head = this.held.get(this.nextDeliver)
379
- if (head === undefined) break
580
+ while (ready > 0) {
581
+ const first = this.nextDeliver
582
+ const head = held[first & mask]
583
+ if (head === undefined || head.count > ready) break
584
+
585
+ this.nextDeliver = (first + head.count) & 0xffff
586
+ ready -= head.count
380
587
  if (head.count === 1) {
381
- this.held.delete(this.nextDeliver)
588
+ held[first & mask] = undefined
382
589
  deliver(head.payload)
383
- this.nextDeliver = (this.nextDeliver + 1) & 0xffff
384
590
  continue
385
591
  }
386
592
 
387
593
  const pieces: Piece[] = []
388
594
  for (let offset = 0; offset < head.count; offset++) {
389
- const piece = this.held.get((this.nextDeliver + offset) & 0xffff)
390
- if (piece === undefined) return
391
- pieces.push(piece)
392
- }
393
-
394
- for (let offset = 0; offset < head.count; offset++) {
395
- this.held.delete((this.nextDeliver + offset) & 0xffff)
595
+ const piece = held[(first + offset) & mask]
596
+ held[(first + offset) & mask] = undefined
597
+ if (piece !== undefined) pieces.push(piece)
396
598
  }
397
599
 
398
600
  deliver(Lane.join(pieces))
399
- this.nextDeliver = (this.nextDeliver + head.count) & 0xffff
400
601
  }
602
+
603
+ this.ready = ready
604
+ }
605
+
606
+ /**
607
+ * Glues a finished group together
608
+ * @param group every fragment of one message
609
+ */
610
+ private static concat(group: Group): Uint8Array {
611
+ const whole = new Uint8Array(group.bytes)
612
+ let offset = 0
613
+ for (const piece of group.pieces) {
614
+ if (piece === undefined) continue
615
+ whole.set(piece, offset)
616
+ offset += piece.length
617
+ }
618
+
619
+ return whole
401
620
  }
402
621
 
403
622
  /**