Skip to content

Commit

Permalink
feat(request): retry server errors for requests without timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
leomp12 committed Jul 28, 2020
1 parent 9c915c2 commit d991312
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const request = (config, api, delay = 170, scheduleTime) => {
}

export default axiosConfig => {
const { url, method, baseURL } = axiosConfig
const { url, method, baseURL, timeout } = axiosConfig
if (!url.indexOf('.json') === -1) {
// all APIs endpoints have JSON extension
axiosConfig.url = url.replace(/^([^?]+)(\?.*)?$/, '$1.json$2')
Expand Down Expand Up @@ -118,6 +118,7 @@ export default axiosConfig => {

// returns promise resolved with request after timeout
return new Promise((resolve, reject) => {
let retries = 0
const schedule = () => {
// calculate final delay with API queue and concurrent requests multipliers
const requestDelay = delay * queue + concurrentRequests * 2.5
Expand All @@ -131,7 +132,16 @@ export default axiosConfig => {
if (waitingApis.indexOf(api) <= -1) {
// send request and reset scheduled requests count
scheduledRequests[api]--
request(axiosConfig, api, delay, scheduleTime).then(resolve).catch(reject)
request(axiosConfig, api, delay, scheduleTime)
.then(resolve)
.catch(err => {
// retry server errors for requests without timeout
if (!timeout && retries < 2 && err.response && err.response.status >= 500) {
setTimeout(schedule, Math.max(delay, 600))
return retries++
}
reject(err)
})
} else {
// API on idle due to 503 response
// schedule request again
Expand Down

0 comments on commit d991312

Please sign in to comment.