db-vendo-client/retry.js

44 lines
1,009 B
JavaScript
Raw Normal View History

2019-02-08 13:13:53 +01:00
'use strict'
const retry = require('p-retry')
2019-02-08 13:42:32 +01:00
const _request = require('./lib/request')
2019-02-08 13:13:53 +01:00
2019-02-08 14:16:43 +01:00
const retryDefaults = {
2019-02-08 13:13:53 +01:00
retries: 3,
factor: 3,
minTimeout: 5 * 1000
}
const withRetrying = (createClient, retryOpts = {}) => {
2019-02-08 14:16:43 +01:00
retryOpts = Object.assign({}, retryDefaults, retryOpts)
const createRetryingClient = (profile, userAgent, opt = {}) => {
const request = 'request' in opt ? opt.request : _request
const retryingRequest = (profile, userAgent, opt, data) => {
const attempt = () => {
return request(profile, userAgent, opt, data)
.catch((err) => {
if (err.isHafasError) throw err // continue
if (err.code === 'ENOTFOUND') { // abort
const abortErr = new retry.AbortError(err)
Object.assign(abortErr, err)
throw abortErr
}
throw err // continue
})
}
return retry(attempt, retryOpts)
2019-02-08 13:13:53 +01:00
}
return createClient(profile, userAgent, {
...opt,
request: retryingRequest
})
}
return createRetryingClient
2019-02-08 13:13:53 +01:00
}
module.exports = withRetrying