Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions sdk/js/src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ export class MeshSession {
/** The addresses of the record last announced under this member's peer key. */
#announced: string[] = [];
#syncing: Promise<ControlPlaneSync> | undefined;
/** A triggered pull is due and its timer has not fired yet. */
#syncTriggered = false;
#keepingRelay: Promise<void> | undefined;
#keepRelayPending = false;
readonly #heldRouterIds: Set<string>;
Expand Down Expand Up @@ -1056,16 +1058,23 @@ export class MeshSession {
if (this.#closed) {
return;
}
this.#syncTriggered = true;
this.#scheduleSync(Math.floor(Math.random() * (this.#syncJitterMs + 1)));
}

#scheduleSync(delayMs: number): void {
clearTimeout(this.#syncTimer);
this.#syncTimer = setTimeout(() => {
this.sync()
this.#syncTriggered = false;
// A pull in flight may have read the control plane before this one was
// asked for, so it is not shared: this one starts when it ends.
(this.#syncing ?? Promise.resolve())
.catch(() => {})
.then(() => (this.#closed ? undefined : this.sync()))
.catch(() => {})
.finally(() => {
if (!this.#closed && this.#syncIntervalMs > 0) {
// A trigger that came in meanwhile keeps its own, earlier timer.
if (!this.#closed && this.#syncIntervalMs > 0 && !this.#syncTriggered) {
// Stretched by up to a tenth so a fleet started together does not pull together.
this.#scheduleSync(this.#syncIntervalMs + Math.floor(Math.random() * (this.#syncIntervalMs / 10 + 1)));
}
Expand Down
40 changes: 40 additions & 0 deletions sdk/js/src/sync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ class FakeControlPlane {
keys: SigningKey[];
banned: string[] = [];
refreshes = 0;
/** The /policies reads so far, one per pull; the hook may hold an answer. */
policyReads = 0;
onPolicyRead: ((reads: number) => Promise<void>) | undefined;
constructor(initial: SigningKey) {
this.keys = [initial];
}
Expand Down Expand Up @@ -127,6 +130,8 @@ class FakeControlPlane {
);
}
if (req.method === "GET" && path === "/policies") {
this.policyReads++;
await this.onPolicyRead?.(this.policyReads);
return proto(toBinary(PolicyConfigGetResponseSchema, create(PolicyConfigGetResponseSchema, { datalogRules: ['granted_publish_all_types(true) <- role("mesh:role:node")'] })));
}
if (req.method === "POST" && path === "/refresh") {
Expand Down Expand Up @@ -318,3 +323,38 @@ test("a banned peer is hung up on and refused at the gate and the handshake", as
await session.close();
}
});

test("an event that arrives while a pull is in flight is followed by another pull", async () => {
const mesh = await AgentMesh.enroll({ controlPlaneUrl: "http://127.0.0.1:1", bootstrapToken: "sbt", fetch: cp.fetch });
// No jitter, so a triggered pull is due at once; no periodic pull within the test.
const session = await mesh.join({ refreshLeadMs: 0, controlPlaneSyncIntervalMs: 3_600_000, controlPlaneSyncJitterMs: 0 });
const start = cp.policyReads;
let secondPull!: () => void;
const pulledAgain = new Promise<void>((resolve) => (secondPull = resolve));
// The first triggered pull holds its /policies answer (it has read the
// control plane) while a second event triggers. The answer is released
// from a timer queued after the second trigger's, so that trigger comes
// due while the first pull is still in flight.
cp.onPolicyRead = (reads) => {
if (reads === start + 2) {
secondPull();
}
if (reads !== start + 1) {
return Promise.resolve();
}
return new Promise<void>((release) => {
session.triggerSync();
setTimeout(release, 0);
});
};
let deadline: ReturnType<typeof setTimeout> | undefined;
try {
session.triggerSync();
await Promise.race([pulledAgain, new Promise<void>((resolve) => (deadline = setTimeout(resolve, 5_000)))]);
assert.equal(cp.policyReads, start + 2, "the event that came during the pull was not followed by a pull of its own");
} finally {
clearTimeout(deadline);
cp.onPolicyRead = undefined;
await session.close();
}
});
Loading