mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +01:00
fix: add connect timeout to event stream to recover from failed connections
This commit is contained in:
1 parent
8491391a15
commit
94c8c3788d
3 files changed
+38
-1
No files matched your search
@@ -44,6 +44,20 @@ describe('OpenCodeEventStream', () => {
|
||||
expect(healthStates.at(-1)).toMatchObject({ isConnected: true, isHealthy: true, isStalled: false })
|
||||
})
|
||||
|
||||
it('recovers when a connection never opens', async () => {
|
||||
const transport = new TestEventStreamTransport()
|
||||
const stream = new OpenCodeEventStream({ transport })
|
||||
|
||||
stream.subscribeGlobalMonitor({ directories: [], onEvent: vi.fn() })
|
||||
expect(transport.openedUrls).toHaveLength(1)
|
||||
|
||||
await vi.advanceTimersByTimeAsync(10_000)
|
||||
expect(transport.closeCount).toBeGreaterThan(0)
|
||||
|
||||
await vi.advanceTimersByTimeAsync(1_000)
|
||||
expect(transport.openedUrls.length).toBeGreaterThan(1)
|
||||
})
|
||||
|
||||
it('reconnects when the watchdog detects a stall', async () => {
|
||||
const transport = new TestEventStreamTransport()
|
||||
const stream = new OpenCodeEventStream({ transport })
|
||||
|
||||
@@ -21,7 +21,7 @@ interface OpenCodeEventStreamOptions {
|
||||
transport?: EventStreamTransport
|
||||
}
|
||||
|
||||
const { RECONNECT_DELAY_MS, MAX_RECONNECT_DELAY_MS, STALL_THRESHOLD_MS, WATCHDOG_TICK_MS } = DEFAULTS.SSE
|
||||
const { RECONNECT_DELAY_MS, MAX_RECONNECT_DELAY_MS, CONNECT_TIMEOUT_MS, STALL_THRESHOLD_MS, WATCHDOG_TICK_MS } = DEFAULTS.SSE
|
||||
|
||||
export class OpenCodeEventStream {
|
||||
private connection: { close(): void } | null = null
|
||||
@@ -30,6 +30,7 @@ export class OpenCodeEventStream {
|
||||
private directoryRefCounts = new Map<string, number>()
|
||||
private pendingDirectories = new Set<string>()
|
||||
private reconnectTimeout: ReturnType<typeof setTimeout> | null = null
|
||||
private connectTimeout: ReturnType<typeof setTimeout> | null = null
|
||||
private reconnectDelay: number = RECONNECT_DELAY_MS
|
||||
private connected = false
|
||||
private subscriberIdCounter = 0
|
||||
@@ -194,9 +195,26 @@ export class OpenCodeEventStream {
|
||||
onConnected: (data) => this.handleConnected(data),
|
||||
onHeartbeat: () => this.markActivity(),
|
||||
})
|
||||
this.startConnectTimeout()
|
||||
}
|
||||
|
||||
private startConnectTimeout(): void {
|
||||
this.clearConnectTimeout()
|
||||
this.connectTimeout = setTimeout(() => {
|
||||
this.connectTimeout = null
|
||||
this.handleError()
|
||||
}, CONNECT_TIMEOUT_MS)
|
||||
}
|
||||
|
||||
private clearConnectTimeout(): void {
|
||||
if (this.connectTimeout) {
|
||||
clearTimeout(this.connectTimeout)
|
||||
this.connectTimeout = null
|
||||
}
|
||||
}
|
||||
|
||||
private handleOpen(): void {
|
||||
this.clearConnectTimeout()
|
||||
this.connected = true
|
||||
this.reconnectDelay = RECONNECT_DELAY_MS
|
||||
this.startWatchdog()
|
||||
@@ -205,6 +223,7 @@ export class OpenCodeEventStream {
|
||||
}
|
||||
|
||||
private handleError(): void {
|
||||
this.clearConnectTimeout()
|
||||
this.connected = false
|
||||
this.clientId = null
|
||||
this.upstreamConnectedCount = null
|
||||
@@ -250,6 +269,7 @@ export class OpenCodeEventStream {
|
||||
this.clientId = null
|
||||
}
|
||||
|
||||
this.clearConnectTimeout()
|
||||
this.connected = true
|
||||
this.startWatchdog()
|
||||
this.markActivity()
|
||||
@@ -263,6 +283,7 @@ export class OpenCodeEventStream {
|
||||
this.reconnectTimeout = null
|
||||
}
|
||||
|
||||
this.clearConnectTimeout()
|
||||
this.stopWatchdog()
|
||||
this.connection?.close()
|
||||
this.connection = null
|
||||
@@ -289,6 +310,7 @@ export class OpenCodeEventStream {
|
||||
this.reconnectTimeout = null
|
||||
}
|
||||
|
||||
this.clearConnectTimeout()
|
||||
this.stopWatchdog()
|
||||
this.connection?.close()
|
||||
this.connection = null
|
||||
|
||||
@@ -50,6 +50,7 @@ export const DEFAULTS = {
|
||||
SSE: {
|
||||
RECONNECT_DELAY_MS: 1000,
|
||||
MAX_RECONNECT_DELAY_MS: 30000,
|
||||
CONNECT_TIMEOUT_MS: 10000,
|
||||
IDLE_GRACE_PERIOD_MS: 5000,
|
||||
HEARTBEAT_INTERVAL_MS: 30000,
|
||||
STALL_THRESHOLD_MS: 90000,
|
||||
|
||||
Reference in new issue
Block a user