@actuallab/rpc 16.0.52 → 16.1.4

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/dist/index.d.cts CHANGED
@@ -484,9 +484,9 @@ declare class RpcOutboundCall {
484
484
  /** Per-call connect/run timeouts enforced by the peer's maintenance loop
485
485
  * (R12). `undefined` means unbounded (the query default). */
486
486
  timeouts: RpcCallTimeouts | undefined;
487
- /** `Date.now()` when this call was created (used for the connect timeout). */
487
+ /** `monotonicClock.nowMs()` when this call was created (used for the connect timeout). */
488
488
  readonly startedAt: number;
489
- /** `Date.now()` when this call was last put on the wire, or 0 if it hasn't
489
+ /** `monotonicClock.nowMs()` when this call was last put on the wire, or 0 if it hasn't
490
490
  * been sent yet (used for the run timeout). */
491
491
  sentAt: number;
492
492
  /**
@@ -999,10 +999,10 @@ declare abstract class RpcPeer {
999
999
  protected _isConnected: boolean;
1000
1000
  protected _connectionState: RpcConnectionState;
1001
1001
  private _keepAliveTimer;
1002
- private _keepAliveWatchdog;
1002
+ private _cancelKeepAliveWatchdog;
1003
1003
  private _maintainTimer;
1004
- /** Wall-clock time of the last inbound `$sys.KeepAlive`, or 0 if none yet
1005
- * for the current connection. Useful for diagnostics. */
1004
+ /** `monotonicClock` time of the last inbound `$sys.KeepAlive` or the last connect,
1005
+ * whichever is later; 0 if the peer has never been connected. */
1006
1006
  private _lastKeepAliveAt;
1007
1007
  /** When the current outage began; 0 until the peer has been connected at least once. */
1008
1008
  private _disconnectedAt;
package/dist/index.d.ts CHANGED
@@ -484,9 +484,9 @@ declare class RpcOutboundCall {
484
484
  /** Per-call connect/run timeouts enforced by the peer's maintenance loop
485
485
  * (R12). `undefined` means unbounded (the query default). */
486
486
  timeouts: RpcCallTimeouts | undefined;
487
- /** `Date.now()` when this call was created (used for the connect timeout). */
487
+ /** `monotonicClock.nowMs()` when this call was created (used for the connect timeout). */
488
488
  readonly startedAt: number;
489
- /** `Date.now()` when this call was last put on the wire, or 0 if it hasn't
489
+ /** `monotonicClock.nowMs()` when this call was last put on the wire, or 0 if it hasn't
490
490
  * been sent yet (used for the run timeout). */
491
491
  sentAt: number;
492
492
  /**
@@ -999,10 +999,10 @@ declare abstract class RpcPeer {
999
999
  protected _isConnected: boolean;
1000
1000
  protected _connectionState: RpcConnectionState;
1001
1001
  private _keepAliveTimer;
1002
- private _keepAliveWatchdog;
1002
+ private _cancelKeepAliveWatchdog;
1003
1003
  private _maintainTimer;
1004
- /** Wall-clock time of the last inbound `$sys.KeepAlive`, or 0 if none yet
1005
- * for the current connection. Useful for diagnostics. */
1004
+ /** `monotonicClock` time of the last inbound `$sys.KeepAlive` or the last connect,
1005
+ * whichever is later; 0 if the peer has never been connected. */
1006
1006
  private _lastKeepAliveAt;
1007
1007
  /** When the current outage began; 0 until the peer has been connected at least once. */
1008
1008
  private _disconnectedAt;
package/dist/index.js CHANGED
@@ -654,7 +654,7 @@ function defineRpcService(name, methods) {
654
654
  }
655
655
 
656
656
  // src/rpc-call-tracker.ts
657
- import { PromiseSource as PromiseSource2 } from "@actuallab/core";
657
+ import { PromiseSource as PromiseSource2, monotonicClock } from "@actuallab/core";
658
658
 
659
659
  // src/rpc-call-stage.ts
660
660
  var RpcCallStage = {
@@ -684,9 +684,9 @@ var RpcOutboundCall = class {
684
684
  /** Per-call connect/run timeouts enforced by the peer's maintenance loop
685
685
  * (R12). `undefined` means unbounded (the query default). */
686
686
  timeouts;
687
- /** `Date.now()` when this call was created (used for the connect timeout). */
688
- startedAt = Date.now();
689
- /** `Date.now()` when this call was last put on the wire, or 0 if it hasn't
687
+ /** `monotonicClock.nowMs()` when this call was created (used for the connect timeout). */
688
+ startedAt = monotonicClock.nowMs();
689
+ /** `monotonicClock.nowMs()` when this call was last put on the wire, or 0 if it hasn't
690
690
  * been sent yet (used for the run timeout). */
691
691
  sentAt = 0;
692
692
  /**
@@ -1424,7 +1424,7 @@ import Denque from "denque";
1424
1424
  import { PromiseSource as PromiseSource4 } from "@actuallab/core";
1425
1425
 
1426
1426
  // src/rpc-stream-sender.ts
1427
- import { PromiseSource as PromiseSource3, RingBuffer } from "@actuallab/core";
1427
+ import { PromiseSource as PromiseSource3, RingBuffer, awakeClock } from "@actuallab/core";
1428
1428
  var { warnLog: warnLog5, errorLog: errorLog2 } = getLogs("RpcSharedStream");
1429
1429
  function _ignore() {
1430
1430
  }
@@ -1507,7 +1507,7 @@ var RpcStreamSender = class _RpcStreamSender {
1507
1507
  // Windowed-MIN ack round trip (ms, -1 until sampled): converges on propagation
1508
1508
  // RTT — unlike a mean/EMA it is not inflated by self-induced queuing.
1509
1509
  get minRttMs() {
1510
- const cutoffMs = Date.now() - RTT_WINDOW_MS;
1510
+ const cutoffMs = awakeClock.nowMs() - RTT_WINDOW_MS;
1511
1511
  while (this._rttSamples.length > 0 && this._rttSamples[0].atMs < cutoffMs)
1512
1512
  this._rttSamples.shift();
1513
1513
  if (this._rttSamples.length === 0)
@@ -1628,7 +1628,7 @@ var RpcStreamSender = class _RpcStreamSender {
1628
1628
  this._nextIndex,
1629
1629
  item
1630
1630
  );
1631
- this._pendingSendTimes.push({ index: this._nextIndex, sentAtMs: Date.now() });
1631
+ this._pendingSendTimes.push({ index: this._nextIndex, sentAtMs: awakeClock.nowMs() });
1632
1632
  if (this._pendingSendTimes.length > MAX_PENDING_SEND_TIMES)
1633
1633
  this._pendingSendTimes.splice(0, MAX_PENDING_SEND_TIMES >> 1);
1634
1634
  }
@@ -1907,7 +1907,7 @@ var RpcStreamSender = class _RpcStreamSender {
1907
1907
  }
1908
1908
  if (sentAtMs < 0)
1909
1909
  return;
1910
- const nowMs = Date.now();
1910
+ const nowMs = awakeClock.nowMs();
1911
1911
  const cutoffMs = nowMs - RTT_WINDOW_MS;
1912
1912
  while (this._rttSamples.length > 0 && this._rttSamples[0].atMs < cutoffMs)
1913
1913
  this._rttSamples.shift();
@@ -2465,7 +2465,15 @@ var RpcSharedObjectTracker = class {
2465
2465
  };
2466
2466
 
2467
2467
  // src/rpc-peer.ts
2468
- import { EventHandlerSet as EventHandlerSet3, PromiseSource as PromiseSource5, TimeoutError, cancellationError, withTimeout } from "@actuallab/core";
2468
+ import {
2469
+ EventHandlerSet as EventHandlerSet3,
2470
+ PromiseSource as PromiseSource5,
2471
+ TimeoutError,
2472
+ cancellationError,
2473
+ monotonicClock as monotonicClock2,
2474
+ startTimer,
2475
+ withTimeout
2476
+ } from "@actuallab/core";
2469
2477
 
2470
2478
  // src/rpc-wire-format.ts
2471
2479
  import { Decoder as Decoder3 } from "@msgpack/msgpack";
@@ -2729,10 +2737,10 @@ var RpcPeer = class {
2729
2737
  _isConnected = false;
2730
2738
  _connectionState = 0 /* Disconnected */;
2731
2739
  _keepAliveTimer;
2732
- _keepAliveWatchdog;
2740
+ _cancelKeepAliveWatchdog;
2733
2741
  _maintainTimer;
2734
- /** Wall-clock time of the last inbound `$sys.KeepAlive`, or 0 if none yet
2735
- * for the current connection. Useful for diagnostics. */
2742
+ /** `monotonicClock` time of the last inbound `$sys.KeepAlive` or the last connect,
2743
+ * whichever is later; 0 if the peer has never been connected. */
2736
2744
  _lastKeepAliveAt = 0;
2737
2745
  /** When the current outage began; 0 until the peer has been connected at least once. */
2738
2746
  _disconnectedAt = 0;
@@ -2761,7 +2769,7 @@ var RpcPeer = class {
2761
2769
  * per outage) plus `Maintain` (RunTimeout, per connection). Calls without timeouts
2762
2770
  * (queries) never time out here. */
2763
2771
  _maintainOutboundCalls() {
2764
- const now = Date.now();
2772
+ const now = monotonicClock2.nowMs();
2765
2773
  const canRunTimeout = this._isConnected && (this._lastKeepAliveAt === 0 || now - this._lastKeepAliveAt <= this.keepAliveTimeoutMs);
2766
2774
  for (const call of [...this.outboundCalls.values()]) {
2767
2775
  const timeouts = call.timeouts;
@@ -2828,11 +2836,12 @@ var RpcPeer = class {
2828
2836
  if (value === 3 /* Connected */) {
2829
2837
  this._isConnected = true;
2830
2838
  this._disconnectedAt = 0;
2839
+ this._lastKeepAliveAt = monotonicClock2.nowMs();
2831
2840
  this._armKeepAliveWatchdog();
2832
2841
  } else {
2833
2842
  if (previousState === 3 /* Connected */) {
2834
2843
  this._isConnected = false;
2835
- this._disconnectedAt = Date.now();
2844
+ this._disconnectedAt = monotonicClock2.nowMs();
2836
2845
  }
2837
2846
  this._disarmKeepAliveWatchdog();
2838
2847
  }
@@ -2859,7 +2868,7 @@ var RpcPeer = class {
2859
2868
  if (this._connection !== conn) return;
2860
2869
  this._connection = void 0;
2861
2870
  if (this._isConnected)
2862
- this._disconnectedAt = Date.now();
2871
+ this._disconnectedAt = monotonicClock2.nowMs();
2863
2872
  this._isConnected = false;
2864
2873
  if (ev.reason)
2865
2874
  infoLog?.log(`'${this.ref}': Disconnected: ${ev.reason} (code=${ev.code})`);
@@ -2900,14 +2909,14 @@ var RpcPeer = class {
2900
2909
  outboundCall.timeouts = options?.timeouts;
2901
2910
  this.outboundCalls.register(outboundCall);
2902
2911
  if (this._isConnected) {
2903
- outboundCall.sentAt = Date.now();
2912
+ outboundCall.sentAt = monotonicClock2.nowMs();
2904
2913
  this._sendWireData(outboundCall.serializedWireData);
2905
2914
  } else {
2906
2915
  const stateHandler = (state) => {
2907
2916
  if (state !== 3 /* Connected */) return;
2908
2917
  this.connectionStateChanged.remove(stateHandler);
2909
2918
  if (outboundCall.result.isCompleted) return;
2910
- outboundCall.sentAt = Date.now();
2919
+ outboundCall.sentAt = monotonicClock2.nowMs();
2911
2920
  this._sendWireData(outboundCall.serializedWireData);
2912
2921
  };
2913
2922
  this.connectionStateChanged.add(stateHandler);
@@ -3133,10 +3142,9 @@ var RpcPeer = class {
3133
3142
  }
3134
3143
  }
3135
3144
  }, this.keepAlivePeriodMs);
3136
- if (this._keepAliveWatchdog !== void 0)
3137
- clearTimeout(this._keepAliveWatchdog);
3138
- this._keepAliveWatchdog = setTimeout(() => {
3139
- this._keepAliveWatchdog = void 0;
3145
+ this._cancelKeepAliveWatchdog?.();
3146
+ this._cancelKeepAliveWatchdog = startTimer(() => {
3147
+ this._cancelKeepAliveWatchdog = void 0;
3140
3148
  if (this._connection === void 0) return;
3141
3149
  warnLog8?.log(
3142
3150
  `'${this.ref}': no keep-alive for ${this.keepAliveTimeoutMs}ms \u2014 force-closing connection`
@@ -3151,16 +3159,14 @@ var RpcPeer = class {
3151
3159
  clearInterval(this._keepAliveTimer);
3152
3160
  this._keepAliveTimer = void 0;
3153
3161
  }
3154
- if (this._keepAliveWatchdog !== void 0) {
3155
- clearTimeout(this._keepAliveWatchdog);
3156
- this._keepAliveWatchdog = void 0;
3157
- }
3162
+ this._cancelKeepAliveWatchdog?.();
3163
+ this._cancelKeepAliveWatchdog = void 0;
3158
3164
  }
3159
3165
  /** Called by `RpcSystemCallHandler` when a `$sys.KeepAlive` message
3160
3166
  * arrives — records the timestamp and re-arms the silence watchdog. */
3161
3167
  notifyKeepAliveReceived() {
3162
- this._lastKeepAliveAt = Date.now();
3163
- if (this._keepAliveWatchdog !== void 0)
3168
+ this._lastKeepAliveAt = monotonicClock2.nowMs();
3169
+ if (this._cancelKeepAliveWatchdog !== void 0)
3164
3170
  this._armKeepAliveWatchdog();
3165
3171
  }
3166
3172
  };
@@ -3432,7 +3438,7 @@ var RpcClientPeer = class extends RpcPeer {
3432
3438
  );
3433
3439
  const sentCalls = this.outboundCalls.getSentCalls();
3434
3440
  this._setConnectionState(3 /* Connected */);
3435
- connectedAt = Date.now();
3441
+ connectedAt = monotonicClock2.nowMs();
3436
3442
  if (peerChangeKind !== 1 /* ChangedToVeryFirst */)
3437
3443
  await this._reconnect(
3438
3444
  sentCalls,
@@ -3451,7 +3457,7 @@ var RpcClientPeer = class extends RpcPeer {
3451
3457
  this._disposed = true;
3452
3458
  break;
3453
3459
  }
3454
- if (connectedAt !== 0 && Date.now() - connectedAt >= this.prematureDisconnectTimeoutMs)
3460
+ if (connectedAt !== 0 && monotonicClock2.nowMs() - connectedAt >= this.prematureDisconnectTimeoutMs)
3455
3461
  this._tryIndex = 0;
3456
3462
  this._tryIndex++;
3457
3463
  }
@@ -3511,7 +3517,7 @@ var RpcClientPeer = class extends RpcPeer {
3511
3517
  continue;
3512
3518
  }
3513
3519
  eligible.push(call);
3514
- call.sentAt = Date.now();
3520
+ call.sentAt = monotonicClock2.nowMs();
3515
3521
  }
3516
3522
  let unknownIds = null;
3517
3523
  let isReconciled = false;
@@ -3541,7 +3547,7 @@ var RpcClientPeer = class extends RpcPeer {
3541
3547
  this.outboundCalls.remove(call.callId);
3542
3548
  continue;
3543
3549
  }
3544
- call.sentAt = Date.now();
3550
+ call.sentAt = monotonicClock2.nowMs();
3545
3551
  this._sendWireData(call.serializedWireData);
3546
3552
  resentSinceYield++;
3547
3553
  if (resentSinceYield >= YIELD_BATCH) {
@@ -4747,7 +4753,7 @@ function getStateDescription(state, useLastError = false) {
4747
4753
  }
4748
4754
 
4749
4755
  // src/rpc-peer-state-monitor.ts
4750
- import { EventHandlerSet as EventHandlerSet4 } from "@actuallab/core";
4756
+ import { EventHandlerSet as EventHandlerSet4, monotonicClock as monotonicClock3 } from "@actuallab/core";
4751
4757
  var { infoLog: infoLog2 } = getLogs("RpcPeerStateMonitor");
4752
4758
  var JustConnectedPeriodMs = 1500;
4753
4759
  var JustDisconnectedPeriodMs = 3e3;
@@ -4765,14 +4771,14 @@ var RpcPeerStateMonitor = class {
4765
4771
  _onConnectionStateChanged = (state) => {
4766
4772
  if (state === 3 /* Connected */) {
4767
4773
  this._rawConnected = true;
4768
- this._connectedAt = Date.now();
4774
+ this._connectedAt = monotonicClock3.nowMs();
4769
4775
  this._lastError = void 0;
4770
4776
  } else {
4771
4777
  const wasConnected = this._rawConnected;
4772
4778
  this._rawConnected = false;
4773
4779
  this._lastError = new Error("Connection closed");
4774
4780
  if (wasConnected) {
4775
- this._disconnectedAt = Date.now();
4781
+ this._disconnectedAt = monotonicClock3.nowMs();
4776
4782
  }
4777
4783
  }
4778
4784
  this._recompute();
@@ -4787,13 +4793,13 @@ var RpcPeerStateMonitor = class {
4787
4793
  this.peer = peer;
4788
4794
  this._rawConnected = peer.isConnected;
4789
4795
  if (this._rawConnected) {
4790
- this._connectedAt = Date.now();
4796
+ this._connectedAt = monotonicClock3.nowMs();
4791
4797
  this._state = {
4792
4798
  kind: 3 /* JustConnected */,
4793
4799
  reconnectsIn: 0
4794
4800
  };
4795
4801
  } else {
4796
- this._disconnectedAt = Date.now();
4802
+ this._disconnectedAt = monotonicClock3.nowMs();
4797
4803
  this._state = {
4798
4804
  kind: 1 /* JustDisconnected */,
4799
4805
  reconnectsIn: 0
@@ -4829,7 +4835,7 @@ var RpcPeerStateMonitor = class {
4829
4835
  clearTimeout(this._timer);
4830
4836
  this._timer = void 0;
4831
4837
  }
4832
- const now = Date.now();
4838
+ const now = monotonicClock3.nowMs();
4833
4839
  let newState;
4834
4840
  if (this._rawConnected) {
4835
4841
  const connectedFor = now - this._connectedAt;