1- import got , { type OptionsOfTextResponseBody , type Method } from 'got '
1+ import { fetchWithRetry } from '@/frame/lib/fetch-utils '
22import { Failbot , HTTPBackend } from '@github/failbot'
33import { getLoggerContext } from '@/observability/logger/lib/logger-context'
44
55const HAYSTACK_APP = 'docs'
66
7- async function retryingGot ( input : RequestInfo | URL , init ?: RequestInit ) : Promise < Response > {
7+ async function retryingFetch ( input : RequestInfo | URL , init ?: RequestInit ) : Promise < Response > {
88 const url = typeof input === 'string' ? input : input . toString ( )
99
10- // Extract body from fetch init for got options
11- const gotOptions : OptionsOfTextResponseBody = {
12- method : ( init ?. method as Method ) || 'GET' ,
13- body : typeof init ?. body === 'string' ? init . body : undefined ,
14- headers : init ?. headers as Record < string , string > | undefined ,
15- // With the timeout at 3000 (milliseconds) and the retry.limit
16- // at 4 (times), the total worst-case is:
17- // 3000 * 4 + 1000 + 2000 + 3000 + 4000 + 8000 = 30 seconds
18- timeout : {
19- response : 3000 ,
10+ // Use fetchWithRetry with retry configuration matching got's behavior
11+ // With the timeout at 3000 (milliseconds) and the retry.limit
12+ // at 4 (times), the total worst-case is:
13+ // 3000 * 4 + 1000 + 2000 + 3000 + 4000 + 8000 = 30 seconds
14+ const response = await fetchWithRetry (
15+ url ,
16+ {
17+ method : init ?. method || 'GET' ,
18+ body : init ?. body ,
19+ headers : init ?. headers ,
2020 } ,
21- retry : {
22- // This means it will wait...
23- // 1. 1000ms
24- // 2. 2000ms
25- // 3. 4000ms
26- // 4. 8000ms
27- // 5. give up!
28- //
29- // From the documentation:
30- //
31- // Delays between retries counts with function
32- // 1000 * Math.pow(2, retry - 1) + Math.random() * 100,
33- // where retry is attempt number (starts from 1).
34- //
35- limit : 4 ,
21+ {
22+ timeout : 3000 ,
23+ retries : 4 ,
24+ throwHttpErrors : false , // Let failbot handle HTTP errors
3625 } ,
37- }
38-
39- const gotResponse = await got ( url , gotOptions )
26+ )
4027
41- // Convert got response to fetch-compatible Response
42- return new Response ( gotResponse . body , {
43- status : gotResponse . statusCode ,
44- statusText : gotResponse . statusMessage ,
45- headers : gotResponse . headers as HeadersInit ,
46- } )
28+ return response
4729}
4830
4931export function report ( error : Error , metadata ?: Record < string , unknown > ) {
@@ -55,7 +37,7 @@ export function report(error: Error, metadata?: Record<string, unknown>) {
5537 const backends = [
5638 new HTTPBackend ( {
5739 haystackURL : process . env . HAYSTACK_URL ,
58- fetchFn : retryingGot ,
40+ fetchFn : retryingFetch ,
5941 } ) ,
6042 ]
6143 const failbot = new Failbot ( {
0 commit comments