Skip to content

Commit a132661

Browse files
committed
fix(crabbox): bound heartbeat before idle expiry
1 parent 1179f5c commit a132661

2 files changed

Lines changed: 13 additions & 6 deletions

File tree

‎extensions/crabbox/src/crabbox-worker-profile.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,11 @@ function heartbeatIntervalMs(idleTimeout: string): number {
8686
if (idleNanoseconds === undefined) {
8787
throw new Error("Crabbox heartbeat requires a positive idle timeout");
8888
}
89-
const intervalNanoseconds = idleNanoseconds / 3n;
90-
return Math.max(5_000, Math.min(60_000, Number(intervalNanoseconds) / 1_000_000));
89+
const idleTimeoutMs = Number(idleNanoseconds) / 1_000_000;
90+
const referenceIntervalMs = Math.max(5_000, Math.min(60_000, idleTimeoutMs / 3));
91+
// Crabbox's floor can exceed short accepted timeouts. Keep renewal ahead of
92+
// coordinator idle expiry without changing the profile contract.
93+
return Math.min(referenceIntervalMs, Math.max(1, Math.floor(idleTimeoutMs / 2)));
9194
}
9295

9396
export function parseCrabboxProfile(profile: WorkerProfile): CrabboxProfile {

‎extensions/crabbox/src/crabbox-worker-provider.test.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1426,12 +1426,15 @@ describe("Crabbox worker provider", () => {
14261426
});
14271427

14281428
it.each([
1429-
{ idleTimeout: "12s", intervalMs: 5_000 },
1430-
{ idleTimeout: "30s", intervalMs: 10_000 },
1431-
{ idleTimeout: "6m", intervalMs: 60_000 },
1429+
{ idleTimeout: "1s", idleTimeoutMs: 1_000, intervalMs: 500 },
1430+
{ idleTimeout: "2s", idleTimeoutMs: 2_000, intervalMs: 1_000 },
1431+
{ idleTimeout: "5s", idleTimeoutMs: 5_000, intervalMs: 2_500 },
1432+
{ idleTimeout: "12s", idleTimeoutMs: 12_000, intervalMs: 5_000 },
1433+
{ idleTimeout: "30s", idleTimeoutMs: 30_000, intervalMs: 10_000 },
1434+
{ idleTimeout: "6m", idleTimeoutMs: 360_000, intervalMs: 60_000 },
14321435
])(
14331436
"heartbeats an active lease every $intervalMs ms for idleTimeout=$idleTimeout",
1434-
async ({ idleTimeout, intervalMs }) => {
1437+
async ({ idleTimeout, idleTimeoutMs, intervalMs }) => {
14351438
vi.useFakeTimers();
14361439
const calls: string[][] = [];
14371440
const profile = { ...PROFILE, idleTimeout };
@@ -1464,6 +1467,7 @@ describe("Crabbox worker provider", () => {
14641467

14651468
await vi.advanceTimersByTimeAsync(intervalMs - 1);
14661469
expect(heartbeatCalls()).toHaveLength(1);
1470+
expect(intervalMs).toBeLessThan(idleTimeoutMs);
14671471
await vi.advanceTimersByTimeAsync(1);
14681472
expect(heartbeatCalls()).toHaveLength(2);
14691473
} finally {

0 commit comments

Comments
 (0)