Compare commits

...
Author SHA1 Message Date
Hona d72fadea15 fix(client): back off event stream reconnection attempts 2026-08-26 06:05:37 +00:00
2 changed files with 30 additions and 1 deletions
+2 -1
View File
@@ -26,6 +26,7 @@ export type ClientConnectionOptions = {
const connectTimeout = 2_000
const reconnectDelay = 1_000
const maxReconnectDelay = 30_000
const connectionHistoryLimit = 50
export function createClientConnection(initialApi: OpenCodeClient, options: ClientConnectionOptions) {
@@ -140,7 +141,7 @@ export function createClientConnection(initialApi: OpenCodeClient, options: Clie
if (attempt === 1) continue
}
}
await wait(reconnectDelay, controller.signal)
await wait(Math.min(reconnectDelay * 2 ** (attempt - 1), maxReconnectDelay), controller.signal)
}
}
@@ -0,0 +1,28 @@
import { expect, test } from "bun:test"
import { createRoot } from "solid-js"
import { isServer } from "solid-js/web"
import { OpenCode } from "../src/promise"
import { createClientConnection } from "../src/solid"
test.skipIf(isServer)("backs off repeated event stream failures", async () => {
const attempts: number[] = []
const api = OpenCode.make({
baseUrl: "http://offline.example",
fetch: async () => {
attempts.push(Date.now())
throw new TypeError("Server is offline")
},
})
const setup = createRoot((dispose) => ({
connection: createClientConnection(api, { onEvent() {} }),
dispose,
}))
try {
await Bun.sleep(2_500)
expect(attempts.length).toBe(2)
expect(setup.connection.attempt()).toBe(2)
} finally {
setup.dispose()
}
})