Skip to content

Commit 1f3c842

Browse files
committed
using axios-retry to retry HTTP calls in QencodeApiClient in case of errors or timeouts
1 parent 14fe7fd commit 1f3c842

2 files changed

Lines changed: 18 additions & 6 deletions

File tree

QencodeApiClient.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
11
const axios = require('axios');
22
const querystring = require('querystring');
3+
const axiosRetry = require('axios-retry');
34

45
const TranscodingTask = require('./Classes/TranscodingTask');
56

7+
// Set the retry logic
8+
axiosRetry(axios, {
9+
retries: 20,
10+
retryDelay: (retryCount) => {
11+
return 3000; // time interval between retries
12+
},
13+
// retry on Network Error and 5xx responses
14+
retryCondition: (error) => {
15+
return axiosRetry.isNetworkOrIdempotentRequestError(error) || error.code === 'ECONNABORTED';
16+
},
17+
});
18+
619
class QencodeApiClient {
720
constructor(options) {
821

@@ -103,24 +116,22 @@ class QencodeApiClient {
103116
// convert parameters to string like 'api_key=5adb0584aa29f'
104117
parameters = querystring.stringify(parameters);
105118
}
106-
119+
107120
try {
108-
109121
this.lastResponseRaw = await axios.post(
110122
requestUrl,
111123
parameters,
112124
{
113125
headers: {
114126
'Content-Type': 'application/x-www-form-urlencoded'
115-
}
127+
},
128+
timeout: this.ConnectTimeout * 1000
116129
}
117130

118131
);
119-
120-
121132
} catch (err) {
122133
throw new Error("Error executing request to url: " + requestUrl, err);
123-
}
134+
}
124135

125136
let response = this.lastResponseRaw.data;
126137

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"bundleDependencies": [],
2727
"dependencies": {
2828
"axios": "^1.2.1",
29+
"axios-retry": "^3.5.1",
2930
"querystring": "^0.2.1"
3031
},
3132
"deprecated": false,

0 commit comments

Comments
 (0)