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

Commit 1197154

Browse files
authoredOct 26, 2020
Merge pull request #255 from yujidude/axiosFix
Fix for infinite axios-retry on 5xx responses and axios client option leakage
2 parents 5094429 + 65b33e4 commit 1197154

File tree

4 files changed

+85
-15
lines changed

4 files changed

+85
-15
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

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"circle-lint": ".buildscript/circle.sh",
2323
"dependencies": "yarn",
2424
"size": "size-limit",
25-
"test": "standard && nyc ava && .buildscript/e2e.sh",
25+
"test": "standard && nyc ava --timeout=20s&& .buildscript/e2e.sh",
2626
"report-coverage": "nyc report --reporter=lcov > coverage.lcov && codecov",
2727
"np": "np --no-publish",
2828
"release": "yarn run np"
@@ -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

+76
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,7 @@ const createClient = options => {
3335
}
3436

3537
test.before.cb(t => {
38+
let count = 0
3639
express()
3740
.use(bodyParser.json())
3841
.post('/v1/batch', (req, res) => {
@@ -62,6 +65,19 @@ test.before.cb(t => {
6265
return setTimeout(() => res.end(), 5000)
6366
}
6467

68+
if (batch[0] === 'axios-retry') {
69+
if (count++ === retryCount) return res.json({})
70+
return res.status(503).json({
71+
error: { message: 'Service Unavailable' }
72+
})
73+
}
74+
75+
if (batch[0] === 'axios-retry-forever') {
76+
return res.status(503).json({
77+
error: { message: 'Service Unavailable' }
78+
})
79+
}
80+
6581
res.json({})
6682
})
6783
.listen(port, t.end)
@@ -561,3 +577,63 @@ test('allows messages > 32kb', t => {
561577
client.track(event, noop)
562578
})
563579
})
580+
581+
test('ensure that failed requests are retried', async t => {
582+
const client = createClient({ retryCount: retryCount })
583+
const callback = spy()
584+
585+
client.queue = [
586+
{
587+
message: 'axios-retry',
588+
callback
589+
}
590+
]
591+
592+
await t.notThrows(client.flush())
593+
})
594+
595+
test('ensure that failed requests are not retried forever', async t => {
596+
const client = createClient()
597+
const callback = spy()
598+
599+
client.queue = [
600+
{
601+
message: 'axios-retry-forever',
602+
callback
603+
}
604+
]
605+
606+
await t.throws(client.flush())
607+
})
608+
609+
test('ensure other axios clients are not impacted by axios-retry', async t => {
610+
let client = createClient() // eslint-disable-line
611+
const axios = require('axios')
612+
613+
let callCounter = 0
614+
615+
// Client will return a successful response for any requests beyond the first
616+
let server = express()
617+
.use(bodyParser.json())
618+
.get('/v1/anotherEndpoint', (req, res) => {
619+
if (callCounter > 0) {
620+
res.status(200).send('Ok')
621+
} else {
622+
callCounter++
623+
res.status(503).send('Service down')
624+
}
625+
})
626+
.listen(separateAxiosClientPort)
627+
628+
await axios.get(`http://localhost:${separateAxiosClientPort}/v1/anotherEndpoint`)
629+
.then(response => {
630+
t.fail()
631+
})
632+
.catch(error => {
633+
if (error) {
634+
t.pass()
635+
}
636+
})
637+
638+
server.close()
639+
})

‎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.