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
10 changes: 9 additions & 1 deletion server/src/active-pool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ test('an invalid attempt cannot clear a previously connected pool', () => {
const result = detectActivePool(
POOLS,
[log('Trying upstream 2 of 2: attacker.example.com:4444')],
{ activeIndex: 1, pendingIndex: null, connectedIndex: null }
{
activeIndex: 1,
activeNegotiatedAt: '2026-07-17T09:59:59.000Z',
pendingIndex: null,
connectedIndex: null,
}
);

assert.equal(result.activeIndex, 1);
Expand Down Expand Up @@ -139,10 +144,12 @@ test('tracker retains the connected fallback across incremental polls and log fa
assert.deepEqual(await tracker.getActivePool('translator', POOLS), {
name: 'Fallback',
index: 1,
negotiatedAt: '2026-07-17T10:00:00.000Z',
});
assert.deepEqual(await tracker.getActivePool('translator', POOLS), {
name: 'Fallback',
index: 1,
negotiatedAt: '2026-07-17T10:00:00.000Z',
});
assert.equal(calls[0], undefined);
assert.equal(typeof calls[1]?.since, 'number');
Expand All @@ -166,5 +173,6 @@ test('tracker carries a pending connection across incremental polls', async () =
assert.deepEqual(await tracker.getActivePool('translator', POOLS), {
name: 'Fallback',
index: 1,
negotiatedAt: '2026-07-17T10:00:00.000Z',
});
});
21 changes: 18 additions & 3 deletions server/src/active-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { ContainerLogLine, LogContainerRole } from './logs/types.js';
export type ActivePool = {
name: string;
index: number;
negotiatedAt: string | null;
};

export type ActivePoolLogOptions = {
Expand All @@ -17,6 +18,7 @@ export type ActivePoolLogProvider = (

type DetectionState = {
activeIndex: number | null;
activeNegotiatedAt: string | null;
pendingIndex: number | null;
connectedIndex: number | null;
};
Expand Down Expand Up @@ -54,11 +56,13 @@ export function detectActivePool(
lines: ContainerLogLine[],
initialState: DetectionState = {
activeIndex: null,
activeNegotiatedAt: null,
pendingIndex: null,
connectedIndex: null,
}
): DetectionState {
let activeIndex = initialState.activeIndex;
let activeNegotiatedAt = initialState.activeNegotiatedAt;
let pendingIndex = initialState.pendingIndex;
let connectedIndex = initialState.connectedIndex;

Expand All @@ -80,6 +84,7 @@ export function detectActivePool(
connectedIndex = null;
// A new attempt means the previous upstream is no longer current.
activeIndex = null;
activeNegotiatedAt = null;
continue;
}

Expand All @@ -99,13 +104,14 @@ export function detectActivePool(
if (SETUP_CONNECTION_SUCCESS_PATTERN.test(line.message)) {
if (connectedIndex !== null && pools[connectedIndex]) {
activeIndex = connectedIndex;
activeNegotiatedAt = line.timestamp;
}
pendingIndex = null;
connectedIndex = null;
}
}

return { activeIndex, pendingIndex, connectedIndex };
return { activeIndex, activeNegotiatedAt, pendingIndex, connectedIndex };
}

function getConfigKey(container: LogContainerRole, pools: PoolConfig[]): string {
Expand Down Expand Up @@ -154,6 +160,7 @@ export class ActivePoolTracker {
this.state = {
configKey,
activeIndex,
activeNegotiatedAt: detected.activeNegotiatedAt,
pendingIndex: detected.pendingIndex,
connectedIndex: detected.connectedIndex,
// Docker's `since` value is inclusive and has one-second precision.
Expand All @@ -163,7 +170,11 @@ export class ActivePoolTracker {

return activeIndex === null
? null
: { name: pools[activeIndex].name, index: activeIndex };
: {
name: pools[activeIndex].name,
index: activeIndex,
negotiatedAt: detected.activeNegotiatedAt,
};
} catch {
const activeIndex = previous?.activeIndex !== null &&
previous?.activeIndex !== undefined &&
Expand All @@ -172,7 +183,11 @@ export class ActivePoolTracker {
: null;
return activeIndex === null
? null
: { name: pools[activeIndex].name, index: activeIndex };
: {
name: pools[activeIndex].name,
index: activeIndex,
negotiatedAt: previous?.activeNegotiatedAt ?? null,
};
}
}
}
Loading
Loading