File tree Expand file tree Collapse file tree 2 files changed +22
-2
lines changed Expand file tree Collapse file tree 2 files changed +22
-2
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ export { newCvToScan, LangLevel } from './cv.ts'
33
44import { envT , readEnv , serverT } from './env.ts'
55import { CVToScan } from './cv.ts'
6+ import { fetchWithRetry } from './utils.ts'
67
78import { 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 } `
Original file line number Diff line number Diff line change 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 ) )
You can’t perform that action at this time.
0 commit comments