Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VT-4723: Handle and Implement retry for connect timeout #265

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## [v4.34.2](https://github.com/plivo/plivo-go/tree/v4.34.2) (2022-10-13)
**Bug Fix - Retry Timeout**
- Implementing Retry logic for Connection timeout along with exponential retry backoff

## [v4.34.1](https://github.com/plivo/plivo-go/tree/v4.34.1) (2022-09-28)
**10DLC campaign creation**
- Adding more attributes to campaign creation request.
Expand Down
24 changes: 23 additions & 1 deletion lib/rest/axios.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as Exceptions from '../utils/exceptions';
import * as _ from "lodash";

import axios from 'axios';
import axiosRetry from 'axios-retry';
import queryString from 'querystring';

var HttpsProxyAgent = require('https-proxy-agent');
Expand All @@ -16,6 +17,27 @@ export function Axios(config) {
'Content-Type': 'application/json'
};

const shouldRetry = error => {
if (error.response == undefined) {
console.error("Recieved error code: " + error.code + ", message: " + error.message);
} else {
console.error("Recieved response status " + error.response.status);
}
return axiosRetry.isNetworkError(error) ||
axiosRetry.isRetryableError(error) ||
error.code === 'ECONNABORTED' ||
error.code === 'ENOTFOUND' ||
error.code === 'ECONNREFUSED' ||
(error.response && error.response.status >= 500);
};

const exponentialDelay = (retryNumber = 0, error, options) => {
const delay = Math.pow(2, retryNumber) * 100;
const randomSum = delay * 0.2 * Math.random(); // 0-20% of the delay
console.error("Retrying after delay of " + (delay + randomSum) + "ms");
return delay + randomSum;
};

const retryWrapper = (axios, options) => {
const max_time = options.retryTime;
let counter = 0;
Expand Down Expand Up @@ -181,7 +203,7 @@ export function Axios(config) {

return new Promise((resolve, reject) => {
if (isVoiceReq) {
retryWrapper(axios, {retryTime: 2, urls: apiVoiceUris, authId: config.authId, action: action});
axiosRetry(axios, { retries: 2, retryDelay: exponentialDelay, retryCondition: shouldRetry, shouldResetTimeout: true });
options.url = apiVoiceUris[0] + config.authId + '/' + action;
if (method === 'GET' && options.data !== '') {
let query = '?' + queryString.stringify(params);
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "plivo",
"version": "4.34.1",
"version": "4.34.2",
"description": "A Node.js SDK to make voice calls and send SMS using Plivo and to generate Plivo XML",
"homepage": "https://github.com/plivo/plivo-node",
"files": [
Expand Down Expand Up @@ -58,7 +58,8 @@
},
"dependencies": {
"@types/node": "^14.14.14",
"axios": "^0.21.1",
"axios": "^0.21.4",
"axios-retry": "^3.3.1",
"base-64": "^0.1.0",
"https-proxy-agent": "^5.0.0",
"build-url": "^1.0.10",
Expand Down