Skip to content

Commit 3764b2c

Browse files
committed
retry requests to rtcv if failing
1 parent be25144 commit 3764b2c

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export { newCvToScan, LangLevel } from './cv.ts'
33

44
import { envT, readEnv, serverT } from './env.ts'
55
import { CVToScan } from './cv.ts'
6+
import { fetchWithRetry } from './utils.ts'
67

78
import { Sha512 } from "https://deno.land/std@0.143.0/hash/sha512.ts"
89

@@ -110,8 +111,7 @@ class ServerConn {
110111
body: body ? JSON.stringify(body) : undefined,
111112
}
112113

113-
const req = await fetch(url, options)
114-
// TODO retry if server is not available
114+
const req = await fetchWithRetry(url, options)
115115
if (req.status >= 400) {
116116
const text = await req.text()
117117
throw `server responsed with [${req.status}] ${req.statusText}: ${text}`

utils.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export async function fetchWithRetry(input: string | Request, init?: RequestInit) {
2+
let attempt = 0
3+
let lastError: unknown
4+
while (attempt <= 4) {
5+
if (attempt > 0) {
6+
const seconds = attempt * 4
7+
console.warn(`fetch failed to RTCV, retrying after ${seconds} seconds`)
8+
await sleep(seconds * 1000)
9+
}
10+
try {
11+
return await fetch(input, init)
12+
} catch (e) {
13+
attempt++
14+
lastError = e
15+
}
16+
}
17+
throw `fetch failed to RTCV, retried 4 times. ${lastError}`
18+
}
19+
20+
export const sleep = (timeout: number) => new Promise((res) => setTimeout(res, timeout))

0 commit comments

Comments
 (0)