Skip to content
This repository was archived by the owner on Feb 13, 2024. It is now read-only.

Commit a78b2ac

Browse files
committedOct 19, 2020
Fix for infinite axios-retry on 5xx responses and axios client options leakage
1 parent 5094429 commit a78b2ac

File tree

4 files changed

+65
-14
lines changed

4 files changed

+65
-14
lines changed
 

‎index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ class Analytics {
4545
enumerable: true,
4646
value: typeof options.enable === 'boolean' ? options.enable : true
4747
})
48-
49-
axiosRetry(axios, {
48+
this.axiosClient = axios.create()
49+
axiosRetry(this.axiosClient, {
5050
retries: options.retryCount || 3,
5151
retryCondition: this._isErrorRetryable,
5252
retryDelay: axiosRetry.exponentialDelay
@@ -279,7 +279,7 @@ class Analytics {
279279
req.timeout = typeof this.timeout === 'string' ? ms(this.timeout) : this.timeout
280280
}
281281

282-
axios(req)
282+
this.axiosClient(req)
283283
.then(() => done())
284284
.catch(err => {
285285
if (err.response) {

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
],
4242
"dependencies": {
4343
"@segment/loosely-validate-event": "^2.0.0",
44-
"axios": "^0.19.0",
44+
"axios": "^0.19.2",
4545
"axios-retry": "^3.0.2",
4646
"lodash.isstring": "^4.0.1",
4747
"md5": "^2.2.1",

‎test.js

+57
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ const context = {
1919

2020
const metadata = { nodeVersion: process.versions.node }
2121
const port = 4063
22+
const separateAxiosClientPort = 4064
23+
const retryCount = 5
2224

2325
const createClient = options => {
2426
options = Object.assign({
@@ -33,6 +35,8 @@ const createClient = options => {
3335
}
3436

3537
test.before.cb(t => {
38+
let count = 0
39+
3640
express()
3741
.use(bodyParser.json())
3842
.post('/v1/batch', (req, res) => {
@@ -62,6 +66,13 @@ test.before.cb(t => {
6266
return setTimeout(() => res.end(), 5000)
6367
}
6468

69+
if (batch[0] === 'axios-retry') {
70+
if (count++ === retryCount) return res.json({})
71+
return res.status(503).json({
72+
error: { message: 'Service Unavailable' }
73+
})
74+
}
75+
6576
res.json({})
6677
})
6778
.listen(port, t.end)
@@ -561,3 +572,49 @@ test('allows messages > 32kb', t => {
561572
client.track(event, noop)
562573
})
563574
})
575+
576+
test('ensure that failed requests are retried', async t => {
577+
const client = createClient({ retryCount: retryCount })
578+
const callback = spy()
579+
580+
client.queue = [
581+
{
582+
message: 'axios-retry',
583+
callback
584+
}
585+
]
586+
587+
await t.notThrows(client.flush())
588+
})
589+
590+
test('ensure other axios clients are not impacted by axios-retry', async t => {
591+
let client = createClient() // eslint-disable-line
592+
const axios = require('axios')
593+
594+
let callCounter = 0
595+
596+
// Client will return a successful response for any requests beyond the first
597+
let server = express()
598+
.use(bodyParser.json())
599+
.get('/v1/anotherEndpoint', (req, res) => {
600+
if (callCounter > 0) {
601+
res.status(200).send('Ok')
602+
} else {
603+
callCounter++
604+
res.status(503).send('Service down')
605+
}
606+
})
607+
.listen(separateAxiosClientPort)
608+
609+
await axios.get(`http://localhost:${separateAxiosClientPort}/v1/anotherEndpoint`)
610+
.then(response => {
611+
t.fail()
612+
})
613+
.catch(error => {
614+
if (error) {
615+
t.pass()
616+
}
617+
})
618+
619+
server.close()
620+
})

‎yarn.lock

+4-10
Original file line numberDiff line numberDiff line change
@@ -829,13 +829,12 @@ axios-retry@^3.0.2:
829829
dependencies:
830830
is-retry-allowed "^1.1.0"
831831

832-
axios@^0.19.0:
833-
version "0.19.0"
834-
resolved "https://registry.yarnpkg.com/axios/-/axios-0.19.0.tgz#8e09bff3d9122e133f7b8101c8fbdd00ed3d2ab8"
835-
integrity sha512-1uvKqKQta3KBxIz14F2v06AEHZ/dIoeKfbTRkK1E5oqjDnuEerLmYTgJB5AiQZHJcljpg1TuRzdjDR06qNk0DQ==
832+
axios@^0.19.2:
833+
version "0.19.2"
834+
resolved "https://registry.yarnpkg.com/axios/-/axios-0.19.2.tgz#3ea36c5d8818d0d5f8a8a97a6d36b86cdc00cb27"
835+
integrity sha512-fjgm5MvRHLhx+osE2xoekY70AhARk3a6hkN+3Io1jc00jtquGvxYlKlsFUhmUET0V5te6CcZI7lcv2Ym61mjHA==
836836
dependencies:
837837
follow-redirects "1.5.10"
838-
is-buffer "^2.0.2"
839838

840839
babel-code-frame@^6.26.0:
841840
version "6.26.0"
@@ -4218,11 +4217,6 @@ is-buffer@^1.0.2, is-buffer@^1.1.5, is-buffer@~1.1.1:
42184217
resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-1.1.6.tgz#efaa2ea9daa0d7ab2ea13a97b2b8ad51fefbe8be"
42194218
integrity sha512-NcdALwpXkTm5Zvvbk7owOUSvVvBKDgKP5/ewfXEznmQFfs4ZRmanOeKBTjRVjka3QFoN6XJ+9F3USqfHqTaU5w==
42204219

4221-
is-buffer@^2.0.2:
4222-
version "2.0.3"
4223-
resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-2.0.3.tgz#4ecf3fcf749cbd1e472689e109ac66261a25e725"
4224-
integrity sha512-U15Q7MXTuZlrbymiz95PJpZxu8IlipAp4dtS3wOdgPXx3mqBnslrWU14kxfHB+Py/+2PVKSr37dMAgM2A4uArw==
4225-
42264220
is-builtin-module@^1.0.0:
42274221
version "1.0.0"
42284222
resolved "https://registry.yarnpkg.com/is-builtin-module/-/is-builtin-module-1.0.0.tgz#540572d34f7ac3119f8f76c30cbc1b1e037affbe"

0 commit comments

Comments
 (0)
This repository has been archived.