@@ -94,14 +94,56 @@ export const runRequest = async (body: BodyResponse|ScrolledResponse, scroll: st
94
94
} ;
95
95
96
96
export const runBulkRequest = async ( body : string ) : Promise < BulkRequestResult > => {
97
- const response = await axios ( `http://elasticsearch:9200/_msearch` , {
98
- method : 'post' ,
99
- data : body ,
100
- headers : {
101
- 'Content-Type' : 'application/x-ndjson' ,
102
- 'Cache-Control' : 'no-cache'
97
+ // Exponential backoff parameters
98
+ const maxRetries = 3 ;
99
+ const initialBackoff = 300 ;
100
+ let retries = 0 ;
101
+ let backoff = initialBackoff ;
102
+
103
+ let response ;
104
+ while ( retries <= maxRetries ) {
105
+ try {
106
+ response = await axios ( `http://elasticsearch:9200/_msearch` , {
107
+ method : 'post' ,
108
+ data : body ,
109
+ headers : {
110
+ 'Content-Type' : 'application/x-ndjson' ,
111
+ 'Cache-Control' : 'no-cache'
112
+ } ,
113
+ timeout : 10000 // timemout of 10s
114
+ } ) ;
115
+ break ; // if ok, exit the loop
116
+ } catch ( error ) {
117
+ if ( ( error . response ?. status >= 500 || error . code === 'ECONNABORTED' ) && retries < maxRetries ) {
118
+ // if it's a timeout or a 5xx error, retry
119
+ log ( {
120
+ elasticsearchError : error . toString ( ) ,
121
+ msg : `Elasticsearch overloaded, retrying in ${ backoff } ms... (${ maxRetries - retries } attempts left)`
122
+ } ) ;
123
+ await new Promise ( resolve => setTimeout ( resolve , backoff ) ) ;
124
+ retries ++ ;
125
+ backoff *= 2 ; // exponential backoff
126
+ continue ;
127
+ }
128
+ return {
129
+ data : {
130
+ responses : [ {
131
+ took : 0 ,
132
+ hits : {
133
+ total : {
134
+ value : 1
135
+ } ,
136
+ max_score : 0 ,
137
+ hits : [ ] ,
138
+ } ,
139
+ status : response . status ,
140
+ statusText : response . statusText ,
141
+ error : true ,
142
+ } ]
143
+ }
144
+ } ;
103
145
}
104
- } ) ;
146
+ }
105
147
if ( response . status >= 400 ) {
106
148
return {
107
149
data : {
0 commit comments