@kumiai/rpc 0.13.1 → 0.13.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/lib/hub-mux.d.ts +2 -0
- package/lib/hub-mux.js +4 -2
- package/lib/log-delivery.d.ts +2 -0
- package/lib/log-delivery.js +4 -2
- package/lib/peer.js +35 -29
- package/lib/recovery-confirmation.d.ts +3 -1
- package/lib/recovery-confirmation.js +8 -7
- package/package.json +14 -14
package/lib/hub-mux.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { BroadcastBus } from '@kumiai/broadcast';
|
|
2
2
|
import { type StoredMessage } from '@kumiai/hub-protocol';
|
|
3
3
|
import type { HubFetchTopicResult, HubSubscribeOptions, LogHub, MailboxHub } from '@kumiai/hub-tunnel';
|
|
4
|
+
import { type Now } from '@sozai/runtime';
|
|
4
5
|
/**
|
|
5
6
|
* A subscribe the hub refused for good — either a permanent refusal (it has answered, and the
|
|
6
7
|
* answer will not change) or a transient one the retry schedule ran out on. Either way this peer
|
|
@@ -30,6 +31,7 @@ export type ReceiveLaneEnded = {
|
|
|
30
31
|
error?: unknown;
|
|
31
32
|
};
|
|
32
33
|
export type HubMuxParams = {
|
|
34
|
+
monotonicNow?: Now;
|
|
33
35
|
/** The real hub. It must serve a log: the commit lane reads one. */
|
|
34
36
|
hub: LogHub;
|
|
35
37
|
/** Authenticated DID used to drain `hub.receive` and stamp bus publishes. */
|
package/lib/hub-mux.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { AuthorizationDeniedError, RetentionExceededError } from '@kumiai/hub-protocol';
|
|
2
2
|
import { getReporter } from '@sozai/log';
|
|
3
|
+
import { createRuntime } from '@sozai/runtime';
|
|
3
4
|
import { asDeliveryPosition } from './cursor.js';
|
|
4
5
|
import { PeerDisposedError } from './errors.js';
|
|
5
6
|
import { assertFrameFits } from './frame-size.js';
|
|
@@ -74,6 +75,7 @@ const warnReceiveEnded = (ended)=>{
|
|
|
74
75
|
* TTL. Expiry drops it unacked so it returns on redelivery rather than reporting false success.
|
|
75
76
|
*/ export function createHubMux(params) {
|
|
76
77
|
const { hub, localDID } = params;
|
|
78
|
+
const monotonicNow = params.monotonicNow ?? createRuntime().monotonicNow;
|
|
77
79
|
const onSubscribeFailed = params.onSubscribeFailed ?? warnSubscribeFailed;
|
|
78
80
|
const onReceiveEnded = params.onReceiveEnded ?? warnReceiveEnded;
|
|
79
81
|
const retryDelaysMs = params.subscribeRetryDelaysMs ?? DEFAULT_SUBSCRIBE_RETRY_DELAYS_MS;
|
|
@@ -265,7 +267,7 @@ const warnReceiveEnded = (ended)=>{
|
|
|
265
267
|
}
|
|
266
268
|
set.add(listener);
|
|
267
269
|
retain(topicID, options);
|
|
268
|
-
sweepPending(
|
|
270
|
+
sweepPending(monotonicNow());
|
|
269
271
|
const handoffs = [];
|
|
270
272
|
for (const [sequenceID, entry] of pending){
|
|
271
273
|
const message = entry.unmatched;
|
|
@@ -382,7 +384,7 @@ const warnReceiveEnded = (ended)=>{
|
|
|
382
384
|
return;
|
|
383
385
|
}
|
|
384
386
|
const message = result.value;
|
|
385
|
-
const now =
|
|
387
|
+
const now = monotonicNow();
|
|
386
388
|
sweepPending(now);
|
|
387
389
|
// Snapshotted BEFORE fan-out: a listener that unsubscribes mid-delivery must not be counted
|
|
388
390
|
// as a pending holder of a frame it is still receiving.
|
package/lib/log-delivery.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { HubFetchTopicResult } from '@kumiai/hub-tunnel';
|
|
2
|
+
import { type Now } from '@sozai/runtime';
|
|
2
3
|
import type { AppOutboxAcceptance, AppOutboxEntry } from './app-outbox.js';
|
|
3
4
|
import type { LogPosition } from './cursor.js';
|
|
4
5
|
export type EpochFloor = {
|
|
@@ -21,6 +22,7 @@ type SealedLogFrame = LogFrame & {
|
|
|
21
22
|
floor: EpochFloor;
|
|
22
23
|
};
|
|
23
24
|
export type LogDeliveryParams = {
|
|
25
|
+
monotonicNow?: Now;
|
|
24
26
|
queue: AppOutboxAcceptance;
|
|
25
27
|
ready: () => Promise<void>;
|
|
26
28
|
floor: () => EpochFloor;
|
package/lib/log-delivery.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { createRuntime } from '@sozai/runtime';
|
|
1
2
|
export function checkedFetchResult(result) {
|
|
2
3
|
if (typeof result.gap !== 'boolean') {
|
|
3
4
|
throw new Error('Commit fetch requires a boolean gap');
|
|
@@ -5,6 +6,7 @@ export function checkedFetchResult(result) {
|
|
|
5
6
|
return result;
|
|
6
7
|
}
|
|
7
8
|
/** Publication evidence stays in memory. A prepared durable attempt proves nothing. */ export function createLogDelivery(params) {
|
|
9
|
+
const monotonicNow = params.monotonicNow ?? createRuntime().monotonicNow;
|
|
8
10
|
const publications = new Map();
|
|
9
11
|
const proven = new Set();
|
|
10
12
|
const delivered = [];
|
|
@@ -20,7 +22,7 @@ export function checkedFetchResult(result) {
|
|
|
20
22
|
let backoff = 1000;
|
|
21
23
|
const headPastFloor = (head, floor)=>head != null && (floor.position == null || head > floor.position);
|
|
22
24
|
const pruneDelivered = ()=>{
|
|
23
|
-
const oldest =
|
|
25
|
+
const oldest = monotonicNow() - params.deliveredWindowMs;
|
|
24
26
|
while(delivered.length > 0 && ((delivered[0]?.at ?? 0) < oldest || delivered.length > params.deliveredLimit))delivered.shift();
|
|
25
27
|
};
|
|
26
28
|
const keepDelivered = (seq)=>{
|
|
@@ -33,7 +35,7 @@ export function checkedFetchResult(result) {
|
|
|
33
35
|
prc,
|
|
34
36
|
data,
|
|
35
37
|
epoch,
|
|
36
|
-
at:
|
|
38
|
+
at: monotonicNow()
|
|
37
39
|
});
|
|
38
40
|
pruneDelivered();
|
|
39
41
|
};
|
package/lib/peer.js
CHANGED
|
@@ -113,7 +113,8 @@ export function createGroupPeer(input) {
|
|
|
113
113
|
const onAppWindowPruned = params.onAppWindowPruned;
|
|
114
114
|
// Destructured rather than held as `runtime`: that name is taken in this scope by
|
|
115
115
|
// {@link ProtocolRuntime}, which is a different thing entirely.
|
|
116
|
-
const
|
|
116
|
+
const platformRuntime = createRuntime(params.runtime);
|
|
117
|
+
const { getRandomID, monotonicNow } = platformRuntime;
|
|
117
118
|
const newPublishID = getRandomID;
|
|
118
119
|
const recoveryTimeoutMs = params.recovery?.timeoutMs ?? DEFAULT_RECOVERY_TIMEOUT_MS;
|
|
119
120
|
const recoveryDeadlineMs = params.recovery?.deadlineMs ?? DEFAULT_RECOVERY_DEADLINE_MS;
|
|
@@ -122,6 +123,7 @@ export function createGroupPeer(input) {
|
|
|
122
123
|
const appLogRetentionSeconds = params.appLogRetentionSeconds ?? DEFAULT_APP_LOG_RETENTION_SECONDS;
|
|
123
124
|
const commitDeadlineMs = params.commitDeadlineMs ?? DEFAULT_COMMIT_DEADLINE_MS;
|
|
124
125
|
const mux = createHubMux({
|
|
126
|
+
monotonicNow,
|
|
125
127
|
hub,
|
|
126
128
|
localDID,
|
|
127
129
|
...params.onSubscribeFailed != null ? {
|
|
@@ -337,7 +339,7 @@ export function createGroupPeer(input) {
|
|
|
337
339
|
if (disposed) return;
|
|
338
340
|
await runSerial(async ()=>{
|
|
339
341
|
await replayJournal();
|
|
340
|
-
if (!await ensureLedger(
|
|
342
|
+
if (!await ensureLedger(monotonicNow() + recoveryTimeoutMs)) {
|
|
341
343
|
throw new Error('app pull is waiting for the ledger');
|
|
342
344
|
}
|
|
343
345
|
await reconcileCommits();
|
|
@@ -382,7 +384,7 @@ export function createGroupPeer(input) {
|
|
|
382
384
|
return runSerial(async ()=>{
|
|
383
385
|
if (disposed) return;
|
|
384
386
|
const replayed = await replayJournal();
|
|
385
|
-
if (mls != null && await ensureLedger(
|
|
387
|
+
if (mls != null && await ensureLedger(monotonicNow() + recoveryTimeoutMs)) {
|
|
386
388
|
ledgerReady = true;
|
|
387
389
|
await finalizeBootstrap(mls);
|
|
388
390
|
}
|
|
@@ -417,7 +419,7 @@ export function createGroupPeer(input) {
|
|
|
417
419
|
protocols,
|
|
418
420
|
handlers,
|
|
419
421
|
suppress,
|
|
420
|
-
runtime:
|
|
422
|
+
runtime: platformRuntime,
|
|
421
423
|
appLogRetentionSeconds,
|
|
422
424
|
anchor: ()=>anchor,
|
|
423
425
|
sealBarrier: ()=>sealBarrier,
|
|
@@ -446,6 +448,7 @@ export function createGroupPeer(input) {
|
|
|
446
448
|
};
|
|
447
449
|
let locallyRemoved = false;
|
|
448
450
|
const logDelivery = appOutboxAcceptance == null || mls == null ? undefined : createLogDelivery({
|
|
451
|
+
monotonicNow,
|
|
449
452
|
queue: appOutboxAcceptance,
|
|
450
453
|
ready: async ()=>{
|
|
451
454
|
await ready;
|
|
@@ -472,7 +475,7 @@ export function createGroupPeer(input) {
|
|
|
472
475
|
pull: async ()=>{
|
|
473
476
|
await runSerial(async ()=>{
|
|
474
477
|
await replayJournal();
|
|
475
|
-
await ensureLedger(
|
|
478
|
+
await ensureLedger(monotonicNow() + recoveryTimeoutMs);
|
|
476
479
|
await reconcileCommits();
|
|
477
480
|
});
|
|
478
481
|
await healIfRequested();
|
|
@@ -768,15 +771,15 @@ export function createGroupPeer(input) {
|
|
|
768
771
|
const confirmationWaiters = new Map();
|
|
769
772
|
const commitOutcomes = createRecoveryCache((outcome)=>{
|
|
770
773
|
if (outcome.kind === 'applied') outcome.key.fill(0);
|
|
771
|
-
});
|
|
774
|
+
}, monotonicNow);
|
|
772
775
|
const recordCommitOutcome = (position, outcome)=>{
|
|
773
776
|
if (commitOutcomes.get(position) != null) {
|
|
774
777
|
if (outcome.kind === 'applied') outcome.key.fill(0);
|
|
775
778
|
return;
|
|
776
779
|
}
|
|
777
|
-
commitOutcomes.set(position, outcome,
|
|
780
|
+
commitOutcomes.set(position, outcome, monotonicNow() + recoveryDeadlineMs);
|
|
778
781
|
};
|
|
779
|
-
const verdictCache = createRecoveryCache();
|
|
782
|
+
const verdictCache = createRecoveryCache(undefined, monotonicNow);
|
|
780
783
|
const verdictTimers = new Set();
|
|
781
784
|
/**
|
|
782
785
|
* Ledger-gather waiters, keyed by requestID. Called for EVERY reply, not just the first: a
|
|
@@ -837,10 +840,10 @@ export function createGroupPeer(input) {
|
|
|
837
840
|
}
|
|
838
841
|
const sealed = await cached.value;
|
|
839
842
|
const expiresAt = cached.expiresAt;
|
|
840
|
-
if (disposed || blocked() || expiresAt <=
|
|
843
|
+
if (disposed || blocked() || expiresAt <= monotonicNow()) return;
|
|
841
844
|
const timer = setTimeout(()=>{
|
|
842
845
|
verdictTimers.delete(timer);
|
|
843
|
-
if (disposed || blocked() || expiresAt <=
|
|
846
|
+
if (disposed || blocked() || expiresAt <= monotonicNow()) return;
|
|
844
847
|
void mux.publish({
|
|
845
848
|
topicID,
|
|
846
849
|
payload: encodeHandshakeFrame({
|
|
@@ -851,7 +854,7 @@ export function createGroupPeer(input) {
|
|
|
851
854
|
})
|
|
852
855
|
})
|
|
853
856
|
}).catch(()=>{});
|
|
854
|
-
}, Math.max(0, Math.min(getReplyDelayMs(), recoveryTimeoutMs, expiresAt -
|
|
857
|
+
}, Math.max(0, Math.min(getReplyDelayMs(), recoveryTimeoutMs, expiresAt - monotonicNow())));
|
|
855
858
|
verdictTimers.add(timer);
|
|
856
859
|
})().catch(()=>{});
|
|
857
860
|
};
|
|
@@ -1523,7 +1526,7 @@ export function createGroupPeer(input) {
|
|
|
1523
1526
|
// A wakeup is a lane operation: step 0, the ledger invariant, then the pull. No return
|
|
1524
1527
|
// value, so anything found is stashed for the next call that has one.
|
|
1525
1528
|
const replayed = await replayJournal();
|
|
1526
|
-
if (mls != null && await ensureLedger(
|
|
1529
|
+
if (mls != null && await ensureLedger(monotonicNow() + recoveryTimeoutMs)) {
|
|
1527
1530
|
await finalizeBootstrap(mls);
|
|
1528
1531
|
}
|
|
1529
1532
|
if (disposed) return;
|
|
@@ -1616,7 +1619,7 @@ export function createGroupPeer(input) {
|
|
|
1616
1619
|
// A peer restored with an incomplete ledger was killed between rejoining and bootstrapping.
|
|
1617
1620
|
// The invariant finds it here and at every later lane operation, with no memory of how it
|
|
1618
1621
|
// got there.
|
|
1619
|
-
const ledgerReady = await ensureLedger(
|
|
1622
|
+
const ledgerReady = await ensureLedger(monotonicNow() + recoveryTimeoutMs);
|
|
1620
1623
|
if (disposed) return;
|
|
1621
1624
|
if (ledgerReady) {
|
|
1622
1625
|
await finalizeBootstrap(mls);
|
|
@@ -1896,7 +1899,7 @@ export function createGroupPeer(input) {
|
|
|
1896
1899
|
});
|
|
1897
1900
|
};
|
|
1898
1901
|
const finishOnDispose = ()=>finish(false);
|
|
1899
|
-
const timer = setTimeout(()=>finish(false), Math.max(0, deadline -
|
|
1902
|
+
const timer = setTimeout(()=>finish(false), Math.max(0, deadline - monotonicNow()));
|
|
1900
1903
|
ledgerGatherFinishes.add(finishOnDispose);
|
|
1901
1904
|
ledgerWaiters.set(requestID, (sealed)=>{
|
|
1902
1905
|
void (async ()=>{
|
|
@@ -1978,13 +1981,13 @@ export function createGroupPeer(input) {
|
|
|
1978
1981
|
// `commit()`'s does not. NO heal is scheduled: this peer holds its leaf, so a rejoin
|
|
1979
1982
|
// would rotate the tree for nothing — the gather that just failed IS the repair,
|
|
1980
1983
|
// re-running at the head of the next lane operation.
|
|
1981
|
-
if (!await ensureLedger(
|
|
1984
|
+
if (!await ensureLedger(monotonicNow() + recoveryTimeoutMs)) {
|
|
1982
1985
|
assertLive();
|
|
1983
1986
|
throw new RecoveryRequiredError('commit: the ledger is incomplete, so this handle rejoined the group and its bootstrap has not completed — its roster has reset, and a commit built now would be judged against a group whose admins it cannot see. It must finish bootstrapping its ledger before it can commit again.');
|
|
1984
1987
|
}
|
|
1985
1988
|
await finalizeBootstrap(mls);
|
|
1986
1989
|
assertLive();
|
|
1987
|
-
const deadline =
|
|
1990
|
+
const deadline = monotonicNow() + commitDeadlineMs;
|
|
1988
1991
|
for(let attempt = 0; attempt < COMMIT_ATTEMPT_CEILING; attempt++){
|
|
1989
1992
|
// 1. Pull the log to the end: every frame processed, and the tip to race at learned from
|
|
1990
1993
|
// the store's own reply.
|
|
@@ -2057,7 +2060,7 @@ export function createGroupPeer(input) {
|
|
|
2057
2060
|
// the pending commit untouched, clear the slot, and go back to step 1.
|
|
2058
2061
|
await slot.clear(publishID);
|
|
2059
2062
|
assertLive();
|
|
2060
|
-
if (
|
|
2063
|
+
if (monotonicNow() >= deadline) {
|
|
2061
2064
|
// The last lost compare-and-set, carried: it is the proximate reason this attempt
|
|
2062
2065
|
// restarted, and the deadline only names how many such losses ran out the clock.
|
|
2063
2066
|
throw new CommitDeadlineError(`commit: still rebasing after ${commitDeadlineMs}ms and ${attempt + 1} attempts`, {
|
|
@@ -2121,7 +2124,7 @@ export function createGroupPeer(input) {
|
|
|
2121
2124
|
assertLive();
|
|
2122
2125
|
return runSerial(async ()=>{
|
|
2123
2126
|
if (await replayJournal()) await rebuildEpoch();
|
|
2124
|
-
if (mls != null && await ensureLedger(
|
|
2127
|
+
if (mls != null && await ensureLedger(monotonicNow() + recoveryTimeoutMs)) {
|
|
2125
2128
|
await finalizeBootstrap(mls);
|
|
2126
2129
|
}
|
|
2127
2130
|
assertLive();
|
|
@@ -2132,7 +2135,7 @@ export function createGroupPeer(input) {
|
|
|
2132
2135
|
* Ask the group for its state and wait for one member to answer, bounded by the deadline.
|
|
2133
2136
|
* The outcome distinguishes a silent responder, disposal and a failed request publish.
|
|
2134
2137
|
*/ const requestGroupInfo = async (request, requestID, topicID, deadline)=>{
|
|
2135
|
-
const remaining = deadline -
|
|
2138
|
+
const remaining = deadline - monotonicNow();
|
|
2136
2139
|
const wait = Math.max(0, Math.min(recoveryTimeoutMs, remaining));
|
|
2137
2140
|
const atDeadline = remaining <= recoveryTimeoutMs;
|
|
2138
2141
|
return await new Promise((resolve)=>{
|
|
@@ -2221,16 +2224,16 @@ export function createGroupPeer(input) {
|
|
|
2221
2224
|
}));
|
|
2222
2225
|
if (await replayJournal()) await rebuildEpoch();
|
|
2223
2226
|
assertLive();
|
|
2224
|
-
if (await ensureLedger(
|
|
2227
|
+
if (await ensureLedger(monotonicNow() + recoveryTimeoutMs)) await finalizeBootstrap(port);
|
|
2225
2228
|
assertLive();
|
|
2226
2229
|
return true;
|
|
2227
2230
|
});
|
|
2228
2231
|
if (!started) return {
|
|
2229
2232
|
advanced: true
|
|
2230
2233
|
};
|
|
2231
|
-
const deadline =
|
|
2234
|
+
const deadline = monotonicNow() + recoveryDeadlineMs;
|
|
2232
2235
|
let prepared = false;
|
|
2233
|
-
while(
|
|
2236
|
+
while(monotonicNow() < deadline){
|
|
2234
2237
|
const publication = await runSerial(async ()=>{
|
|
2235
2238
|
if (await replayJournal()) await rebuildEpoch();
|
|
2236
2239
|
// 1. Pull to the end. It may resolve the strand outright, and a heal it no longer needs
|
|
@@ -2255,7 +2258,7 @@ export function createGroupPeer(input) {
|
|
|
2255
2258
|
const requestID = newPublishID();
|
|
2256
2259
|
const request = await port.createRecoveryRequest({
|
|
2257
2260
|
requestID,
|
|
2258
|
-
deadlineMs: Math.max(0, deadline -
|
|
2261
|
+
deadlineMs: Math.max(0, deadline - monotonicNow())
|
|
2259
2262
|
});
|
|
2260
2263
|
assertLive();
|
|
2261
2264
|
const outcome = await requestGroupInfo(request, requestID, rendezvous, deadline);
|
|
@@ -2354,6 +2357,7 @@ export function createGroupPeer(input) {
|
|
|
2354
2357
|
if ('advanced' in publication) return publication;
|
|
2355
2358
|
const { pending, sequenceID, commitDigest, key, requestID, request, groupID, epochBeforeRejoin, headBeforeRejoin, inFlight } = publication;
|
|
2356
2359
|
const confirmation = await waitForRecoveryConfirmation({
|
|
2360
|
+
monotonicNow,
|
|
2357
2361
|
port,
|
|
2358
2362
|
pending,
|
|
2359
2363
|
groupID,
|
|
@@ -2528,6 +2532,8 @@ export function createGroupPeer(input) {
|
|
|
2528
2532
|
});
|
|
2529
2533
|
throw error instanceof PeerDisposedError ? error : new PeerDisposedError('Peer is disposed');
|
|
2530
2534
|
}
|
|
2535
|
+
// The cursor may already be past the frame that asked for this heal: nothing re-asks.
|
|
2536
|
+
retryRecovery();
|
|
2531
2537
|
emitRecovery({
|
|
2532
2538
|
...base,
|
|
2533
2539
|
phase: 'failed',
|
|
@@ -2586,16 +2592,16 @@ export function createGroupPeer(input) {
|
|
|
2586
2592
|
/**
|
|
2587
2593
|
* Run a heal the lane asked for, never from inside the lane. `recover()` takes the commit mutex,
|
|
2588
2594
|
* so the trigger records and the caller runs this once it releases the mutex. A heal already in
|
|
2589
|
-
* flight absorbs any trigger raised while it runs
|
|
2590
|
-
*
|
|
2595
|
+
* flight absorbs any trigger raised while it runs. A pull may have stepped past the frame that
|
|
2596
|
+
* raised it, so nothing re-asks: each failure path schedules its own follow-up.
|
|
2591
2597
|
*/ const healIfRequested = async ()=>{
|
|
2592
2598
|
if (disposed || !healRequested || activeRecovery != null || renewalHeld() || refusalHeld) return;
|
|
2593
2599
|
healRequested = false;
|
|
2594
2600
|
try {
|
|
2595
2601
|
await runRecovery('automatic');
|
|
2596
2602
|
} catch {
|
|
2597
|
-
//
|
|
2598
|
-
//
|
|
2603
|
+
// Reported through `onRecovery`. The peer stays degraded until the follow-up the failed
|
|
2604
|
+
// attempt scheduled.
|
|
2599
2605
|
}
|
|
2600
2606
|
};
|
|
2601
2607
|
let pendingRestoreTimer;
|
|
@@ -2668,7 +2674,7 @@ export function createGroupPeer(input) {
|
|
|
2668
2674
|
if (disposed) return;
|
|
2669
2675
|
await replayJournal();
|
|
2670
2676
|
if (disposed) return;
|
|
2671
|
-
await ensureLedger(
|
|
2677
|
+
await ensureLedger(monotonicNow() + recoveryTimeoutMs);
|
|
2672
2678
|
if (disposed) return;
|
|
2673
2679
|
await reconcileCommits();
|
|
2674
2680
|
});
|
|
@@ -2778,7 +2784,7 @@ export function createGroupPeer(input) {
|
|
|
2778
2784
|
await appLane.dropFrame(topicID, position);
|
|
2779
2785
|
assertLive();
|
|
2780
2786
|
await replayJournal();
|
|
2781
|
-
await ensureLedger(
|
|
2787
|
+
await ensureLedger(monotonicNow() + recoveryTimeoutMs);
|
|
2782
2788
|
await reconcileCommits();
|
|
2783
2789
|
});
|
|
2784
2790
|
await healIfRequested();
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type Now } from '@sozai/runtime';
|
|
1
2
|
import type { GroupMLS, OpenedRecoveryVerdict, PendingRecovery, RecoveryRefusalReason } from './crypto.js';
|
|
2
3
|
export type RecoveryCommitOutcome = {
|
|
3
4
|
commitDigest: string;
|
|
@@ -17,7 +18,7 @@ export type RecoveryCacheEntry<TValue> = {
|
|
|
17
18
|
expiresAt: number;
|
|
18
19
|
};
|
|
19
20
|
/** Expiry releases exporter keys even when the peer receives no further traffic. */
|
|
20
|
-
export declare function createRecoveryCache<TValue>(release?: (value: TValue) => void): {
|
|
21
|
+
export declare function createRecoveryCache<TValue>(release?: (value: TValue) => void, monotonicNow?: Now): {
|
|
21
22
|
delete: (key: string) => void;
|
|
22
23
|
get(key: string): RecoveryCacheEntry<TValue> | undefined;
|
|
23
24
|
set(key: string, value: TValue, expiresAt: number): void;
|
|
@@ -48,6 +49,7 @@ export type ConfirmationWaiter = {
|
|
|
48
49
|
dispose: () => void;
|
|
49
50
|
};
|
|
50
51
|
export type WaitForRecoveryConfirmationParams = {
|
|
52
|
+
monotonicNow?: Now;
|
|
51
53
|
port: GroupMLS;
|
|
52
54
|
pending: PendingRecovery;
|
|
53
55
|
groupID: string;
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { encodeMultibase } from '@kokuin/token';
|
|
2
2
|
import { hmac } from '@noble/hashes/hmac.js';
|
|
3
3
|
import { sha256 } from '@noble/hashes/sha2.js';
|
|
4
|
+
import { createRuntime } from '@sozai/runtime';
|
|
4
5
|
export function confirmationTag(key, requestID) {
|
|
5
6
|
return encodeMultibase(hmac(sha256, key, new TextEncoder().encode(requestID)));
|
|
6
7
|
}
|
|
7
|
-
/** Expiry releases exporter keys even when the peer receives no further traffic. */ export function createRecoveryCache(release = ()=>{}) {
|
|
8
|
+
/** Expiry releases exporter keys even when the peer receives no further traffic. */ export function createRecoveryCache(release = ()=>{}, monotonicNow = createRuntime().monotonicNow) {
|
|
8
9
|
const entries = new Map();
|
|
9
10
|
const remove = (key)=>{
|
|
10
11
|
const held = entries.get(key);
|
|
@@ -17,7 +18,7 @@ export function confirmationTag(key, requestID) {
|
|
|
17
18
|
delete: remove,
|
|
18
19
|
get (key) {
|
|
19
20
|
const held = entries.get(key);
|
|
20
|
-
if (held != null && held.expiresAt <=
|
|
21
|
+
if (held != null && held.expiresAt <= monotonicNow()) {
|
|
21
22
|
remove(key);
|
|
22
23
|
return;
|
|
23
24
|
}
|
|
@@ -25,14 +26,14 @@ export function confirmationTag(key, requestID) {
|
|
|
25
26
|
},
|
|
26
27
|
set (key, value, expiresAt) {
|
|
27
28
|
remove(key);
|
|
28
|
-
if (expiresAt <=
|
|
29
|
+
if (expiresAt <= monotonicNow()) {
|
|
29
30
|
release(value);
|
|
30
31
|
return;
|
|
31
32
|
}
|
|
32
33
|
entries.set(key, {
|
|
33
34
|
value,
|
|
34
35
|
expiresAt,
|
|
35
|
-
timer: setTimeout(()=>remove(key), expiresAt -
|
|
36
|
+
timer: setTimeout(()=>remove(key), expiresAt - monotonicNow())
|
|
36
37
|
});
|
|
37
38
|
while(entries.size > 1024){
|
|
38
39
|
const oldest = entries.keys().next().value;
|
|
@@ -45,7 +46,7 @@ export function confirmationTag(key, requestID) {
|
|
|
45
46
|
};
|
|
46
47
|
}
|
|
47
48
|
export function waitForRecoveryConfirmation(params) {
|
|
48
|
-
const { port, pending, groupID, requestID, position, commitDigest, key, deadline, timeoutMs, send, waiters } = params;
|
|
49
|
+
const { port, monotonicNow = createRuntime().monotonicNow, pending, groupID, requestID, position, commitDigest, key, deadline, timeoutMs, send, waiters } = params;
|
|
49
50
|
return new Promise((resolve)=>{
|
|
50
51
|
let finished = false;
|
|
51
52
|
let settle;
|
|
@@ -77,7 +78,7 @@ export function waitForRecoveryConfirmation(params) {
|
|
|
77
78
|
]
|
|
78
79
|
});
|
|
79
80
|
};
|
|
80
|
-
const end = setTimeout(finishWithBest, Math.max(0, deadline -
|
|
81
|
+
const end = setTimeout(finishWithBest, Math.max(0, deadline - monotonicNow()));
|
|
81
82
|
const repeat = setInterval(ask, timeoutMs);
|
|
82
83
|
waiters.set(requestID, {
|
|
83
84
|
dispose: ()=>finish({
|
|
@@ -123,7 +124,7 @@ export function waitForRecoveryConfirmation(params) {
|
|
|
123
124
|
};
|
|
124
125
|
if (settle == null) {
|
|
125
126
|
clearInterval(repeat);
|
|
126
|
-
settle = setTimeout(finishWithBest, Math.min(timeoutMs, Math.max(0, deadline -
|
|
127
|
+
settle = setTimeout(finishWithBest, Math.min(timeoutMs, Math.max(0, deadline - monotonicNow())));
|
|
127
128
|
ask();
|
|
128
129
|
}
|
|
129
130
|
}).catch((error)=>finish({
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kumiai/rpc",
|
|
3
|
-
"version": "0.13.
|
|
3
|
+
"version": "0.13.3",
|
|
4
4
|
"description": "High-level MLS-aware group RPC for Enkaku: bus broadcast/anycast/gather plus directed RPC over the pub/sub hub",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"rpc",
|
|
@@ -26,26 +26,26 @@
|
|
|
26
26
|
"lib/*"
|
|
27
27
|
],
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@enkaku/client": "^0.23.
|
|
30
|
-
"@enkaku/protocol": "^0.23.
|
|
31
|
-
"@enkaku/server": "^0.23.
|
|
32
|
-
"@enkaku/transport": "^0.23.
|
|
33
|
-
"@kokuin/token": "^0.7.
|
|
29
|
+
"@enkaku/client": "^0.23.7",
|
|
30
|
+
"@enkaku/protocol": "^0.23.4",
|
|
31
|
+
"@enkaku/server": "^0.23.5",
|
|
32
|
+
"@enkaku/transport": "^0.23.3",
|
|
33
|
+
"@kokuin/token": "^0.7.1",
|
|
34
34
|
"@kumiai/broadcast": "^0.13.0",
|
|
35
|
-
"@kumiai/hub-protocol": "^0.13.
|
|
36
|
-
"@kumiai/hub-tunnel": "^0.13.
|
|
35
|
+
"@kumiai/hub-protocol": "^0.13.3",
|
|
36
|
+
"@kumiai/hub-tunnel": "^0.13.3",
|
|
37
37
|
"@noble/hashes": "^2.4.0",
|
|
38
|
-
"@sozai/codec": "^0.4.
|
|
39
|
-
"@sozai/event": "^0.1.
|
|
38
|
+
"@sozai/codec": "^0.4.5",
|
|
39
|
+
"@sozai/event": "^0.1.7",
|
|
40
40
|
"@sozai/log": "^0.4.0",
|
|
41
|
-
"@sozai/runtime": "^0.1.
|
|
41
|
+
"@sozai/runtime": "^0.1.1",
|
|
42
42
|
"@sozai/schema": "^0.1.6"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
|
-
"@kumiai/hub-conformance": "^0.13.
|
|
46
|
-
"@kumiai/rpc-conformance": "^0.13.
|
|
45
|
+
"@kumiai/hub-conformance": "^0.13.1",
|
|
46
|
+
"@kumiai/rpc-conformance": "^0.13.1",
|
|
47
47
|
"@noble/curves": "^2.4.0",
|
|
48
|
-
"vitest": "^5.1.
|
|
48
|
+
"vitest": "^5.1.2"
|
|
49
49
|
},
|
|
50
50
|
"publishConfig": {
|
|
51
51
|
"access": "public"
|