@steve31415/baselib 3.62.2 → 3.62.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/dist/hub-daemon.d.ts +8 -0
- package/dist/hub-daemon.js +38 -2
- package/package.json +1 -1
package/dist/hub-daemon.d.ts
CHANGED
|
@@ -8,6 +8,10 @@ export declare const HUB_DEGRADED_SLEEP_MS = 2000;
|
|
|
8
8
|
export declare const HUB_MINT_FLOOR_MS: number;
|
|
9
9
|
/** A token is replaced once it is this old (the hub's TTL is one hour). */
|
|
10
10
|
export declare const HUB_TOKEN_REFRESH_MS: number;
|
|
11
|
+
/** The longest a park waits for a refresh in flight before parking on the
|
|
12
|
+
* old token anyway; above the callers' 10 s bound on one mint, so in
|
|
13
|
+
* practice the mint always settles first. */
|
|
14
|
+
export declare const HUB_REFRESH_WAIT_MS = 15000;
|
|
11
15
|
/** Added to the caller's lookback (review S2), and the hub's cap on it. */
|
|
12
16
|
export declare const HUB_LOOKBACK_SLACK_MS = 1000;
|
|
13
17
|
export declare const HUB_LOOKBACK_MAX_MS = 60000;
|
|
@@ -36,6 +40,7 @@ export interface HubDaemonClientOptions {
|
|
|
36
40
|
degradedSleepMs?: number;
|
|
37
41
|
mintFloorMs?: number;
|
|
38
42
|
refreshAfterMs?: number;
|
|
43
|
+
refreshWaitMs?: number;
|
|
39
44
|
/** Injectable for tests. */
|
|
40
45
|
fetch?: typeof fetch;
|
|
41
46
|
/** Monotonic milliseconds (tests). */
|
|
@@ -53,6 +58,7 @@ export declare class HubDaemonClient {
|
|
|
53
58
|
private readonly degradedSleepMs;
|
|
54
59
|
private readonly mintFloorMs;
|
|
55
60
|
private readonly refreshAfterMs;
|
|
61
|
+
private readonly refreshWaitMs;
|
|
56
62
|
private readonly fetchFn;
|
|
57
63
|
private readonly now;
|
|
58
64
|
private readonly sleepFn;
|
|
@@ -76,6 +82,8 @@ export declare class HubDaemonClient {
|
|
|
76
82
|
* caller's monotonic clock. Never throws. */
|
|
77
83
|
park(lookbackMs: number, signal?: AbortSignal): Promise<HubParkOutcome>;
|
|
78
84
|
private usableToken;
|
|
85
|
+
/** Wait for the mint in flight, at most refreshWaitMs or until `signal`. */
|
|
86
|
+
private awaitMint;
|
|
79
87
|
private maybeMint;
|
|
80
88
|
private enterDegraded;
|
|
81
89
|
private leaveDegraded;
|
package/dist/hub-daemon.js
CHANGED
|
@@ -15,9 +15,13 @@
|
|
|
15
15
|
// - It never sends Authorization: the app's device bearer stays on the
|
|
16
16
|
// daemon ↔ app path (review S4). The hub token goes in X-Hub-Token.
|
|
17
17
|
// - One attempt per park, bounded at the park plus 10 s; no retry budget.
|
|
18
|
-
// - The token is minted off the loop's critical path, at most once
|
|
19
|
-
// HUB_MINT_FLOOR_MS whatever the outcome (review S3); until a token
|
|
18
|
+
// - The first token is minted off the loop's critical path, at most once
|
|
19
|
+
// per HUB_MINT_FLOOR_MS whatever the outcome (review S3); until a token
|
|
20
20
|
// exists the loop runs degraded (direct requests every 2 s).
|
|
21
|
+
// - The park that starts a refresh waits for it (bounded) before sending
|
|
22
|
+
// its wait: the hub kills the old token the moment the new one
|
|
23
|
+
// registers, so a wait sent on the old token alongside the refresh
|
|
24
|
+
// could reach the hub's token lookup after that and be refused (3.62.3).
|
|
21
25
|
// - After a 401 it never presents that token again until a new mint
|
|
22
26
|
// (§14.4: the hub's per-address limit on bad tokens is shared by every
|
|
23
27
|
// daemon on the host).
|
|
@@ -36,6 +40,10 @@ export const HUB_DEGRADED_SLEEP_MS = 2_000;
|
|
|
36
40
|
export const HUB_MINT_FLOOR_MS = 5 * 60_000;
|
|
37
41
|
/** A token is replaced once it is this old (the hub's TTL is one hour). */
|
|
38
42
|
export const HUB_TOKEN_REFRESH_MS = 50 * 60_000;
|
|
43
|
+
/** The longest a park waits for a refresh in flight before parking on the
|
|
44
|
+
* old token anyway; above the callers' 10 s bound on one mint, so in
|
|
45
|
+
* practice the mint always settles first. */
|
|
46
|
+
export const HUB_REFRESH_WAIT_MS = 15_000;
|
|
39
47
|
/** Added to the caller's lookback (review S2), and the hub's cap on it. */
|
|
40
48
|
export const HUB_LOOKBACK_SLACK_MS = 1_000;
|
|
41
49
|
export const HUB_LOOKBACK_MAX_MS = 60_000;
|
|
@@ -50,6 +58,7 @@ export class HubDaemonClient {
|
|
|
50
58
|
degradedSleepMs;
|
|
51
59
|
mintFloorMs;
|
|
52
60
|
refreshAfterMs;
|
|
61
|
+
refreshWaitMs;
|
|
53
62
|
fetchFn;
|
|
54
63
|
now;
|
|
55
64
|
sleepFn;
|
|
@@ -68,6 +77,7 @@ export class HubDaemonClient {
|
|
|
68
77
|
this.degradedSleepMs = opts.degradedSleepMs ?? HUB_DEGRADED_SLEEP_MS;
|
|
69
78
|
this.mintFloorMs = opts.mintFloorMs ?? HUB_MINT_FLOOR_MS;
|
|
70
79
|
this.refreshAfterMs = opts.refreshAfterMs ?? HUB_TOKEN_REFRESH_MS;
|
|
80
|
+
this.refreshWaitMs = opts.refreshWaitMs ?? HUB_REFRESH_WAIT_MS;
|
|
71
81
|
this.fetchFn = opts.fetch ?? fetch;
|
|
72
82
|
this.now = opts.now ?? (() => performance.now());
|
|
73
83
|
this.sleepFn = opts.sleep ?? sleep;
|
|
@@ -91,6 +101,19 @@ export class HubDaemonClient {
|
|
|
91
101
|
if (signal?.aborted)
|
|
92
102
|
return 'stopped';
|
|
93
103
|
this.maybeMint();
|
|
104
|
+
if (this.minting && this.usableToken()) {
|
|
105
|
+
// A refresh is in flight (we hold a token, so it is not the first
|
|
106
|
+
// mint). Its register replaces our row and kills the token we hold the
|
|
107
|
+
// instant it commits, while a wait sent now on that token could reach
|
|
108
|
+
// the hub's lookup after that commit and be refused with 401 — the
|
|
109
|
+
// daemon racing itself (task 105aa5f6: four WARNs a day on lurch). So
|
|
110
|
+
// park on the new token: one mint's latency every 50 minutes, in idle
|
|
111
|
+
// time. A refresh that fails or overruns leaves the old token, still
|
|
112
|
+
// valid, in place.
|
|
113
|
+
await this.awaitMint(signal);
|
|
114
|
+
if (signal?.aborted)
|
|
115
|
+
return 'stopped';
|
|
116
|
+
}
|
|
94
117
|
const token = this.usableToken();
|
|
95
118
|
if (!token) {
|
|
96
119
|
// Quiet while the very first mint is still in flight; degraded after.
|
|
@@ -153,6 +176,19 @@ export class HubDaemonClient {
|
|
|
153
176
|
this.token = null;
|
|
154
177
|
return this.token;
|
|
155
178
|
}
|
|
179
|
+
/** Wait for the mint in flight, at most refreshWaitMs or until `signal`. */
|
|
180
|
+
async awaitMint(signal) {
|
|
181
|
+
const minting = this.minting;
|
|
182
|
+
if (!minting)
|
|
183
|
+
return;
|
|
184
|
+
const settled = new AbortController();
|
|
185
|
+
try {
|
|
186
|
+
await Promise.race([minting, sleep(this.refreshWaitMs, signal ? AbortSignal.any([signal, settled.signal]) : settled.signal)]);
|
|
187
|
+
}
|
|
188
|
+
finally {
|
|
189
|
+
settled.abort();
|
|
190
|
+
}
|
|
191
|
+
}
|
|
156
192
|
maybeMint() {
|
|
157
193
|
const t = this.now();
|
|
158
194
|
const needed = this.token === null || t - this.token.mintedAt >= this.refreshAfterMs;
|
package/package.json
CHANGED