@@ -25,6 +25,7 @@ import { fromEntries, promiseAll } from '../utils/array.js'
2525import { weightedRequestConcurrencyController } from '@autonomys/asynchronous'
2626import { optimizeBatchFetch } from './batchOptimizer.js'
2727import { readableToStream } from '../utils/stream.js'
28+ import { withRetries } from '../utils/retries.js'
2829
2930const fetchNodesSchema = z . object ( {
3031 jsonrpc : z . string ( ) ,
@@ -75,41 +76,60 @@ const fetchObjects = async (objects: ObjectMapping[]) => {
7576 id : requestId ,
7677 }
7778
78- return concurrencyController ( async ( ) => {
79- logger . debug (
80- `Fetching nodes (requestId=${ requestId } ): ${ objects . map ( ( e ) => e [ 0 ] ) . join ( ', ' ) } ` ,
81- )
82- const fetchStart = performance . now ( )
83- const response = await axios . post ( gatewayUrl , body , {
84- timeout : 3600_000 ,
85- responseType : 'json' ,
86- } )
87- if ( response . status !== 200 ) {
88- console . error ( 'Failed to fetch nodes' , response . status , response . data )
89- throw new HttpError ( 500 , 'Internal server error: Failed to fetch nodes' )
90- }
79+ return concurrencyController (
80+ async ( ) =>
81+ withRetries (
82+ async ( ) => {
83+ logger . debug (
84+ `Fetching nodes (requestId=${ requestId } ): ${ objects . map ( ( e ) => e [ 0 ] ) . join ( ', ' ) } ` ,
85+ )
86+ const fetchStart = performance . now ( )
87+ const response = await axios . post ( gatewayUrl , body , {
88+ timeout : 3600_000 ,
89+ responseType : 'json' ,
90+ } )
91+ if ( response . status !== 200 ) {
92+ console . error (
93+ 'Failed to fetch nodes' ,
94+ response . status ,
95+ response . data ,
96+ )
97+ throw new HttpError (
98+ 500 ,
99+ 'Internal server error: Failed to fetch nodes' ,
100+ )
101+ }
91102
92- const validatedResponseData = fetchNodesSchema . safeParse ( response . data )
93- if ( ! validatedResponseData . success ) {
94- console . error (
95- 'Failed to parse fetch nodes response' ,
96- validatedResponseData . error ,
97- )
98- throw new HttpError (
99- 500 ,
100- 'Internal server error: Failed to parse fetch nodes response' ,
101- )
102- }
103+ const validatedResponseData = fetchNodesSchema . safeParse (
104+ response . data ,
105+ )
106+ if ( ! validatedResponseData . success ) {
107+ console . error (
108+ 'Failed to parse fetch nodes response' ,
109+ validatedResponseData . error ,
110+ )
111+ throw new HttpError (
112+ 500 ,
113+ 'Internal server error: Failed to parse fetch nodes response' ,
114+ )
115+ }
103116
104- const end = performance . now ( )
105- logger . debug (
106- `Fetched ${ objects . length } nodes in total=${ end - now } ms fetch=${ end - fetchStart } ms (requestId=${ requestId } )` ,
107- )
117+ const end = performance . now ( )
118+ logger . debug (
119+ `Fetched ${ objects . length } nodes in total=${ end - now } ms fetch=${ end - fetchStart } ms (requestId=${ requestId } )` ,
120+ )
108121
109- return validatedResponseData . data . result . map ( ( hex ) =>
110- decodeNode ( Buffer . from ( hex , 'hex' ) ) ,
111- )
112- } , objects . length )
122+ return validatedResponseData . data . result . map ( ( hex ) =>
123+ decodeNode ( Buffer . from ( hex , 'hex' ) ) ,
124+ )
125+ } ,
126+ {
127+ maxRetries : 3 ,
128+ delay : 500 ,
129+ } ,
130+ ) ,
131+ objects . length ,
132+ )
113133}
114134
115135/**
0 commit comments