diff --git a/CHANGELOG.md b/CHANGELOG.md index 400b417b..6d6216fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/lib/rest/axios.js b/lib/rest/axios.js index 99502844..c820b0c0 100644 --- a/lib/rest/axios.js +++ b/lib/rest/axios.js @@ -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'); @@ -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; @@ -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); diff --git a/package.json b/package.json index 06c049de..75409ea8 100644 --- a/package.json +++ b/package.json @@ -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": [ @@ -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",