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

feat: Add fixed interval retry to requests #1

Merged
merged 2 commits into from
Dec 4, 2018
Merged
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
19 changes: 18 additions & 1 deletion lib/send.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
'use strict'

const https = require('https')
const http = require('http')
const concat = require('concat-stream')
const url = require('url')
const MAX_ATTEMPTS = 5
const RETRY_INTERVAL = process.env.BUGSNAG_RETRY_INTERVAL || 1000

module.exports = (endpoint, data, onSuccess, onError) => {
let attempts = 0
const maybeRetry = (err) => {
attempts++
if (err && err.isRetryable && attempts < MAX_ATTEMPTS) return setTimeout(go, RETRY_INTERVAL)
return onError(err)
}
const go = () => send(endpoint, data, onSuccess, maybeRetry)
go()
}

const send = (endpoint, data, onSuccess, onError) => {
const parsedUrl = url.parse(endpoint)
const payload = JSON.stringify(data)
const req = (parsedUrl.protocol === 'https:' ? https : http).request({
Expand All @@ -19,7 +34,9 @@ module.exports = (endpoint, data, onSuccess, onError) => {
res.pipe(concat(body => {
if (res.statusCode === 200) return onSuccess()
if (res.statusCode !== 400) {
return onError(new Error(`HTTP status ${res.statusCode} received from builds API`))
const err = new Error(`HTTP status ${res.statusCode} received from builds API`)
err.isRetryable = true
return onError(err)
}
try {
const err = new Error('Invalid payload sent to builds API')
Expand Down
48 changes: 48 additions & 0 deletions lib/test/send.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict'

process.env.BUGSNAG_RETRY_INTERVAL = 50

const test = require('tape')
const http = require('http')
const send = require('../send')
Expand Down Expand Up @@ -48,6 +50,29 @@ test('send(): unsuccessful (500)', t => {
})
})

test('send(): retry (500)', t => {
t.plan(1)
let n = 0
const server = http.createServer((req, res) => {
n++
if (n < 4) {
res.statusCode = 500
return res.end('error')
} else {
return res.end('ok')
}
})
server.listen()
send(`http://localhost:${server.address().port}`, {}, () => {
server.close()
t.equal(n, 4, 'it should make multiple attempts')
t.end()
}, (err) => {
server.close()
t.end(err)
})
})

test('send(): unsuccessful (400)', t => {
t.plan(3)
const server = http.createServer((req, res) => {
Expand All @@ -68,6 +93,29 @@ test('send(): unsuccessful (400)', t => {
})
})

test('send(): unsuccessful, doesn’t retry (400)', t => {
t.plan(4)
let n = 0
const server = http.createServer((req, res) => {
n++
res.statusCode = 400
res.setHeader('content-type', 'application/json')
res.end('{ "errors": [ "flipflop is not a valid crankworble" ] }')
})
server.listen()
send(`http://localhost:${server.address().port}`, {}, () => {
server.close()
t.fail('send should not succeed')
}, (err) => {
server.close()
t.ok(err)
t.equal(n, 1, 'It should never retry a bad request')
t.equal(err.message, 'Invalid payload sent to builds API')
t.deepEqual(err.errors, [ 'flipflop is not a valid crankworble' ])
t.end()
})
})

test('send(): unsuccessful (400, bad json)', t => {
t.plan(2)
const server = http.createServer((req, res) => {
Expand Down
Loading