@netifyjs/netify 1.0.0-alpha.2 → 1.0.0-alpha.3

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@netifyjs/netify",
3
3
  "description": "A Netty-inspired networking framework for Bun, for TCP, UDP, reliable UDP and WebSocket game servers",
4
- "version": "1.0.0-alpha.2",
4
+ "version": "1.0.0-alpha.3",
5
5
  "type": "module",
6
6
  "exports": {
7
7
  ".": "./src/index.ts"
@@ -413,6 +413,18 @@ export class Channel {
413
413
  )
414
414
  }
415
415
 
416
+ /**
417
+ * Queues encoded bytes without a flush or a promise
418
+ * @param bytes already encoded, nothing in the pipeline touches them
419
+ */
420
+ public writeEncodedSync(bytes: Uint8Array | ByteBuf): void {
421
+ void this.outbound.add(
422
+ bytes instanceof ByteBuf ? bytes.toUint8Array() : bytes,
423
+ null,
424
+ true,
425
+ )
426
+ }
427
+
416
428
  /**
417
429
  * Fire-and-forget version of writeAndFlushEncoded, no promise is made
418
430
  * @param bytes already encoded, nothing in the pipeline touches them
@@ -28,6 +28,16 @@ export class Gateway {
28
28
  */
29
29
  private readonly groups: Map<string, ChannelGroup>
30
30
 
31
+ /**
32
+ * Channels written to since the last flush
33
+ */
34
+ private readonly touched: Set<Channel>
35
+
36
+ /**
37
+ * Flushes every touched channel once, bound for queueMicrotask
38
+ */
39
+ private readonly flusher: () => void
40
+
31
41
  /**
32
42
  * Starts listening for publishes from other shards
33
43
  * @param link the shard's link
@@ -35,6 +45,11 @@ export class Gateway {
35
45
  public constructor(link: ShardLink) {
36
46
  this.link = link
37
47
  this.groups = new Map<string, ChannelGroup>()
48
+ this.touched = new Set<Channel>()
49
+ this.flusher = (): void => {
50
+ for (const channel of this.touched) void channel.flush()
51
+ this.touched.clear()
52
+ }
38
53
 
39
54
  link.on(Gateway.topic, (data: unknown): void => {
40
55
  const [topic, bytes] = data as Publication
@@ -114,6 +129,13 @@ export class Gateway {
114
129
  return 0
115
130
  }
116
131
 
117
- return group.broadcastSync(bytes)
132
+ if (this.touched.size === 0) queueMicrotask(this.flusher)
133
+
134
+ for (const channel of group) {
135
+ channel.writeEncodedSync(bytes)
136
+ this.touched.add(channel)
137
+ }
138
+
139
+ return group.size
118
140
  }
119
141
  }
@@ -108,7 +108,7 @@ type Arrival = {
108
108
  readonly state: unknown
109
109
 
110
110
  /**
111
- * The old shard, told once the player is claimed
111
+ * The old shard, which has the last word on the claim
112
112
  */
113
113
  readonly from: number
114
114
 
@@ -127,6 +127,11 @@ type Departure = {
127
127
  */
128
128
  readonly state: unknown
129
129
 
130
+ /**
131
+ * Set once the new shard was allowed to claim
132
+ */
133
+ granted: boolean
134
+
130
135
  /**
131
136
  * Where the player went
132
137
  */
@@ -195,14 +200,19 @@ export class ShardLink {
195
200
  public static readonly handoffTopic: string = 'netify:handoff'
196
201
 
197
202
  /**
198
- * Tells the old shard a player arrived
203
+ * Asks the old shard whether a player can still be claimed
199
204
  */
200
- public static readonly claimedTopic: string = 'netify:claimed'
205
+ public static readonly confirmTopic: string = 'netify:confirm'
201
206
 
202
207
  /**
203
- * Asks the new shard to drop a player who never arrived
208
+ * Tells the old shard it can drop its copy
204
209
  */
205
- public static readonly cancelTopic: string = 'netify:cancel'
210
+ public static readonly settledTopic: string = 'netify:settled'
211
+
212
+ /**
213
+ * Asks the new shard whether it has a player
214
+ */
215
+ public static readonly holdsTopic: string = 'netify:holds'
206
216
 
207
217
  /**
208
218
  * This shard
@@ -260,7 +270,12 @@ export class ShardLink {
260
270
  private readonly departures: Map<string, Departure>
261
271
 
262
272
  /**
263
- * Tokens claimed here lately, so a late recall can't bring the player back twice
273
+ * Tokens recalled here lately, so the new shard can't claim them after all
274
+ */
275
+ private readonly recalled: Map<string, Timer>
276
+
277
+ /**
278
+ * Tokens claimed here lately, for the old shard to ask about
264
279
  */
265
280
  private readonly claimed: Map<string, Timer>
266
281
 
@@ -310,6 +325,7 @@ export class ShardLink {
310
325
  this.waiting = new Map<number, Waiting>()
311
326
  this.arrivals = new Map<string, Arrival>()
312
327
  this.departures = new Map<string, Departure>()
328
+ this.recalled = new Map<string, Timer>()
313
329
  this.claimed = new Map<string, Timer>()
314
330
  this.connections = new Map<number, Peer>()
315
331
  this.dirty = new Set<Peer>()
@@ -323,12 +339,20 @@ export class ShardLink {
323
339
 
324
340
  this.responders.set(
325
341
  ShardLink.handoffTopic,
326
- (state: unknown, from: number): string => this.arrive(state, from),
342
+ (state: unknown, from: number): string => {
343
+ const token = crypto.randomUUID()
344
+
345
+ this.hold(token, state, from)
346
+ return token
347
+ },
348
+ )
349
+ this.responders.set(ShardLink.confirmTopic, (token: unknown): boolean =>
350
+ this.confirm(String(token)),
327
351
  )
328
- this.responders.set(ShardLink.cancelTopic, (token: unknown): boolean =>
329
- this.cancel(String(token)),
352
+ this.responders.set(ShardLink.holdsTopic, (token: unknown): boolean =>
353
+ this.claimed.has(String(token)),
330
354
  )
331
- this.listeners.set(ShardLink.claimedTopic, (token: unknown): void => {
355
+ this.listeners.set(ShardLink.settledTopic, (token: unknown): void => {
332
356
  this.forget(String(token))
333
357
  })
334
358
  }
@@ -478,31 +502,45 @@ export class ShardLink {
478
502
  this.departures.delete(token)
479
503
  }, this.handoffTimeout)
480
504
  timer.unref()
481
- this.departures.set(token, { state, shard, timer })
505
+ this.departures.set(token, { state, granted: false, shard, timer })
482
506
  return token
483
507
  }
484
508
 
485
509
  /**
486
- * Takes a handed off player's state, or null if the token is unknown or expired
510
+ * Takes a handed off player's state once the old shard agrees, or null if the token is unknown, expired or recalled
487
511
  * @param token from handoff
488
512
  */
489
- public claim<T>(token: string): T | null {
513
+ public async claim<T>(token: string): Promise<T | null> {
490
514
  const arrival = this.arrivals.get(token)
491
515
  if (arrival === undefined) return null
492
516
 
493
517
  clearTimeout(arrival.timer)
494
518
  this.arrivals.delete(token)
519
+ this.remember(this.claimed, token)
495
520
 
496
- const timer = setTimeout((): void => {
497
- this.claimed.delete(token)
498
- }, this.handoffTimeout)
499
- timer.unref()
500
- this.claimed.set(token, timer)
521
+ let allowed: boolean
522
+ try {
523
+ allowed = await this.request<boolean>(
524
+ arrival.from,
525
+ ShardLink.confirmTopic,
526
+ token,
527
+ )
528
+ } catch (error) {
529
+ if (!(error instanceof Error) || error.message !== 'Shard is down') {
530
+ this.release(this.claimed, token)
531
+ this.hold(token, arrival.state, arrival.from)
532
+ throw error
533
+ }
501
534
 
502
- if (arrival.from >= 0 && arrival.from < this.count) {
503
- this.send(arrival.from, ShardLink.claimedTopic, token)
535
+ allowed = true
504
536
  }
505
537
 
538
+ if (!allowed) {
539
+ this.release(this.claimed, token)
540
+ return null
541
+ }
542
+
543
+ if (!this.closed) this.send(arrival.from, ShardLink.settledTopic, token)
506
544
  return arrival.state as T
507
545
  }
508
546
 
@@ -514,25 +552,31 @@ export class ShardLink {
514
552
  const departure = this.departures.get(token)
515
553
  if (departure === undefined) return null
516
554
 
517
- let cancelled: boolean
518
- try {
519
- cancelled = await this.request<boolean>(
520
- departure.shard,
521
- ShardLink.cancelTopic,
522
- token,
523
- )
524
- } catch (error) {
525
- if (!(error instanceof Error) || error.message !== 'Shard is down') {
526
- throw error
555
+ if (departure.granted) {
556
+ let held: boolean
557
+ try {
558
+ held = await this.request<boolean>(
559
+ departure.shard,
560
+ ShardLink.holdsTopic,
561
+ token,
562
+ )
563
+ } catch (error) {
564
+ if (!(error instanceof Error) || error.message !== 'Shard is down') {
565
+ throw error
566
+ }
567
+
568
+ held = false
527
569
  }
528
570
 
529
- cancelled = true
571
+ if (held || this.departures.get(token) !== departure) {
572
+ this.forget(token)
573
+ return null
574
+ }
530
575
  }
531
576
 
532
- if (this.departures.get(token) !== departure) return null
533
-
534
577
  this.forget(token)
535
- return cancelled ? (departure.state as T) : null
578
+ this.remember(this.recalled, token)
579
+ return departure.state as T
536
580
  }
537
581
 
538
582
  /**
@@ -572,6 +616,8 @@ export class ShardLink {
572
616
  }
573
617
 
574
618
  this.departures.clear()
619
+ for (const timer of this.recalled.values()) clearTimeout(timer)
620
+ this.recalled.clear()
575
621
  for (const timer of this.claimed.values()) clearTimeout(timer)
576
622
  this.claimed.clear()
577
623
 
@@ -839,37 +885,58 @@ export class ShardLink {
839
885
  }
840
886
 
841
887
  /**
842
- * Stores a handed off player and returns their token
888
+ * Keeps a handed off player until they claim
889
+ * @param token their token
843
890
  * @param state what the old shard sent over
844
891
  * @param from the old shard
845
892
  */
846
- private arrive(state: unknown, from: number): string {
847
- const token = crypto.randomUUID()
893
+ private hold(token: string, state: unknown, from: number): void {
848
894
  const timer = setTimeout((): void => {
849
895
  this.arrivals.delete(token)
850
896
  }, this.handoffTimeout)
851
897
  timer.unref()
852
898
 
853
899
  this.arrivals.set(token, { state, from, timer })
854
- return token
855
900
  }
856
901
 
857
902
  /**
858
- * Drops a player who won't arrive, false if they already did
903
+ * Lets the new shard keep a player, false if they were recalled
859
904
  * @param token from handoff
860
905
  */
861
- private cancel(token: string): boolean {
862
- if (this.claimed.has(token)) return false
863
-
864
- const arrival = this.arrivals.get(token)
865
- if (arrival !== undefined) {
866
- clearTimeout(arrival.timer)
867
- this.arrivals.delete(token)
868
- }
906
+ private confirm(token: string): boolean {
907
+ if (this.recalled.has(token)) return false
869
908
 
909
+ const departure = this.departures.get(token)
910
+ if (departure !== undefined) departure.granted = true
870
911
  return true
871
912
  }
872
913
 
914
+ /**
915
+ * Takes a token back out of a set
916
+ * @param tokens where it's kept
917
+ * @param token from handoff
918
+ */
919
+ private release(tokens: Map<string, Timer>, token: string): void {
920
+ const timer = tokens.get(token)
921
+ if (timer === undefined) return
922
+
923
+ clearTimeout(timer)
924
+ tokens.delete(token)
925
+ }
926
+
927
+ /**
928
+ * Keeps a token in a set until claims and recalls on it can't happen any more
929
+ * @param tokens where to keep it
930
+ * @param token from handoff
931
+ */
932
+ private remember(tokens: Map<string, Timer>, token: string): void {
933
+ const timer = setTimeout((): void => {
934
+ tokens.delete(token)
935
+ }, this.handoffTimeout)
936
+ timer.unref()
937
+ tokens.set(token, timer)
938
+ }
939
+
873
940
  /**
874
941
  * Lets go of the copy of a player who's been claimed or recalled
875
942
  * @param token from handoff