mirror of
https://github.com/cozystack/cozystack.git
synced 2026-11-22 12:43:32 +09:00
fix(dashboard): treat an open log tail as live before its first line
The tail counted as live, cleared its error and reset its backoff only when a line arrived. A quiet application therefore showed "Connecting" forever, a 429 warning stuck after the retry succeeded, and every idle close by the proxy (its tail idle timeout) raised the backoff towards 30s and reconnected without since, losing lines written in the gap. The tail now reports when the response opens. That marks it live, clears the error and resets the backoff; a clean close reconnects at the base delay; and with no line seen yet the reconnect resumes from the time the stream opened. Assisted-by: LLM Signed-off-by: ZverGuy <maximbel2003@gmail.com>
This commit is contained in:
1 parent
c9a15cb8ae
commit
fca5ca78de
4 files changed
+71
-5
No files matched your search
+18
@@ -194,6 +194,24 @@ describe("tailLogs", () => {
|
||||
|
||||
expect(err).toMatchObject({ kind: "forbidden" })
|
||||
})
|
||||
|
||||
it("says the stream opened before any line arrives", async () => {
|
||||
const { client } = clientReturning(new Response(""))
|
||||
const onOpen = vi.fn()
|
||||
|
||||
await tailLogs(client, logsRoot(ns), { query: "*" }, () => {}, undefined, onOpen)
|
||||
|
||||
expect(onOpen).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it("does not say the stream opened when the tail is refused", async () => {
|
||||
const { client } = clientReturning(new Response("", { status: 429 }))
|
||||
const onOpen = vi.fn()
|
||||
|
||||
await tailLogs(client, logsRoot(ns), { query: "*" }, () => {}, undefined, onOpen).catch(() => {})
|
||||
|
||||
expect(onOpen).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
describe("isScopeWide", () => {
|
||||
|
||||
@@ -202,8 +202,10 @@ export async function tailLogs(
|
||||
params: { query: string; since?: string },
|
||||
onEntry: (entry: LogEntry) => void,
|
||||
signal?: AbortSignal,
|
||||
onOpen?: () => void,
|
||||
): Promise<void> {
|
||||
const res = await proxyGet(client, `${root}select/logsql/tail`, params, signal)
|
||||
onOpen?.()
|
||||
if (!res.body) return
|
||||
const reader = res.body.getReader()
|
||||
const decoder = new TextDecoder()
|
||||
|
||||
+37
@@ -10,6 +10,10 @@ function ndjson(...lines: string[]): Response {
|
||||
return new Response(lines.map((l) => `${l}\n`).join(""))
|
||||
}
|
||||
|
||||
function openStream(): Response {
|
||||
return new Response(new ReadableStream<Uint8Array>({ start() {} }))
|
||||
}
|
||||
|
||||
function setup(...responses: Response[]) {
|
||||
const client = new K8sClient()
|
||||
const raw = vi.spyOn(client, "raw")
|
||||
@@ -75,6 +79,39 @@ describe("useLogTail", () => {
|
||||
await waitFor(() => expect(result.current.entries.map((e) => e._msg)).toEqual(["a"]))
|
||||
})
|
||||
|
||||
it("goes live and clears a load warning once the stream opens, before any line", async () => {
|
||||
const { wrapper } = setup(new Response("too many concurrent requests", { status: 429 }), openStream())
|
||||
|
||||
const { result } = renderHook(() => useLogTail("/root/", "*", 1, { retryDelayMs: 1 }), { wrapper })
|
||||
|
||||
await waitFor(() => expect(result.current.state).toBe("live"))
|
||||
expect(result.current.error).toBeNull()
|
||||
})
|
||||
|
||||
it("reconnects after an idle close at the base delay, from when the quiet stream opened", async () => {
|
||||
const delays: number[] = []
|
||||
const realSetTimeout = globalThis.setTimeout
|
||||
const spy = vi.spyOn(globalThis, "setTimeout").mockImplementation(((fn: () => void, ms?: number) => {
|
||||
if (ms !== undefined && ms % 7 === 0 && ms > 0) delays.push(ms)
|
||||
return realSetTimeout(fn, ms)
|
||||
}) as typeof setTimeout)
|
||||
const refused = () => new Response("", { status: 429 })
|
||||
const { raw, wrapper } = setup(refused(), refused(), ndjson())
|
||||
|
||||
try {
|
||||
renderHook(() => useLogTail("/root/", "*", 1, { retryDelayMs: 7 }), { wrapper })
|
||||
await waitFor(() => expect(raw).toHaveBeenCalledTimes(4))
|
||||
} finally {
|
||||
spy.mockRestore()
|
||||
}
|
||||
|
||||
expect(delays).toEqual([7, 14, 7])
|
||||
expect(new URL(raw.mock.calls[2][0], "http://x").searchParams.has("since")).toBe(false)
|
||||
const since = new URL(raw.mock.calls[3][0], "http://x").searchParams.get("since")
|
||||
expect(since).not.toBeNull()
|
||||
expect(new Date(since!).toISOString()).toBe(since)
|
||||
})
|
||||
|
||||
it("does not connect while disabled", () => {
|
||||
const { raw, wrapper } = setup()
|
||||
|
||||
|
||||
+14
-5
@@ -66,15 +66,22 @@ export function useLogTail(
|
||||
}
|
||||
|
||||
const run = async () => {
|
||||
for (let attempt = 0; !signal.aborted; attempt++) {
|
||||
let failures = 0
|
||||
let openedAt: string | undefined
|
||||
while (!signal.aborted) {
|
||||
resumedFrom = lastTime
|
||||
let delay = retryDelayMs
|
||||
try {
|
||||
await tailLogs(client, root, { query, since: lastTime }, (entry) => {
|
||||
attempt = 0
|
||||
await tailLogs(client, root, { query, since: lastTime ?? openedAt }, (entry) => {
|
||||
if (!lastTime || compareTime(entry._time, lastTime) > 0) lastTime = entry._time
|
||||
pending.push(entry)
|
||||
timer ??= setTimeout(flush, FLUSH_MS)
|
||||
}, signal)
|
||||
}, signal, () => {
|
||||
failures = 0
|
||||
// With no line yet, the next reconnect resumes from here so the gap is not lost.
|
||||
openedAt = new Date().toISOString()
|
||||
update({ state: "live", error: null })
|
||||
})
|
||||
flush()
|
||||
} catch (err) {
|
||||
flush()
|
||||
@@ -84,10 +91,12 @@ export function useLogTail(
|
||||
return
|
||||
}
|
||||
update({ error: err })
|
||||
delay = Math.min(retryDelayMs * 2 ** failures, MAX_RETRY_MS)
|
||||
failures++
|
||||
}
|
||||
if (signal.aborted) return
|
||||
update({ state: "reconnecting" })
|
||||
await sleep(Math.min(retryDelayMs * 2 ** attempt, MAX_RETRY_MS), signal)
|
||||
await sleep(delay, signal)
|
||||
}
|
||||
}
|
||||
void run()
|
||||
|
||||
Reference in new issue
Block a user