createClientWithRetry -> withRetrying 💥

This commit is contained in:
Jannis R 2019-02-08 15:07:35 +01:00
parent 748f8ce6b0
commit fbde6a171b
No known key found for this signature in database
GPG key ID: 0FE83946296A88A5
3 changed files with 33 additions and 27 deletions

View file

@ -43,11 +43,13 @@ const client = createThrottledClient(dbProfile, 'my-awesome-program')
There's opt-in support for retrying failed requests to the endpoint.
```js
const createClientWithRetry = require('hafas-client/retry')
const withRetrying = require('hafas-client/retry')
const createClient = require('hafas-client')
const dbProfile = require('hafas-client/p/db')
// create a client with Deutsche Bahn profile that will retry on HAFAS errors
const client = createClientWithRetry(dbProfile, 'my-awesome-program')
const createRetryingClient = withRetrying(createClient)
const client = createRetryingClient(dbProfile, 'my-awesome-program')
// Berlin Jungfernheide to München Hbf
client.journeys('8011167', '8000261', {results: 1})
@ -55,15 +57,16 @@ client.journeys('8011167', '8000261', {results: 1})
.catch(console.error)
```
You can pass custom options into `createClientWithRetry`. They will be passed into [`retry`](https://github.com/tim-kos/node-retry#tutorial).
You can pass custom options into `withRetrying`. They will be passed into [`retry`](https://github.com/tim-kos/node-retry#tutorial).
```js
// retry 2 times, after 10 seconds & 30 seconds
const client = createClientWithRetry(dbProfile, 'my-awesome-program', {
const createRetryingClient = withRetrying(createClient, {
retries: 2,
minTimeout: 10 * 1000,
factor: 3
})
const client = createRetryingClient(dbProfile, 'my-awesome-program')
```
## Writing a profile

View file

@ -3,7 +3,6 @@
const retry = require('p-retry')
const _request = require('./lib/request')
const createClient = require('.')
const retryDefaults = {
retries: 3,
@ -11,26 +10,29 @@ const retryDefaults = {
minTimeout: 5 * 1000
}
const createClientWithRetry = (profile, userAgent, retryOpts = {}, request = _request) => {
const withRetrying = (createClient, retryOpts = {}) => {
retryOpts = Object.assign({}, retryDefaults, retryOpts)
const requestWithRetry = (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
})
const createRetryingClient = (profile, userAgent, 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)
}
return retry(attempt, retryOpts)
}
return createClient(profile, userAgent, requestWithRetry)
return createClient(profile, userAgent, retryingRequest)
}
return createRetryingClient
}
module.exports = createClientWithRetry
module.exports = withRetrying

View file

@ -2,14 +2,14 @@
const test = require('tape')
const createClientWithRetry = require('../retry')
const withRetrying = require('../retry')
const createClient = require('..')
const vbbProfile = require('../p/vbb')
const _request = require('../lib/request')
const userAgent = 'public-transport/hafas-client:test'
const spichernstr = '900000042101'
test('retry works', (t) => {
test('withRetrying works', (t) => {
// for the first 3 calls, return different kinds of errors
let calls = 0
const failingRequest = (profile, userAgent, opt, data) => {
@ -28,12 +28,13 @@ test('retry works', (t) => {
return Promise.resolve([])
}
const client = createClientWithRetry(vbbProfile, userAgent, {
const createRetryingClient = withRetrying(createClient, {
retries: 3,
minTimeout: 100,
factor: 2,
randomize: false
}, failingRequest)
})
const client = createRetryingClient(vbbProfile, userAgent, failingRequest)
t.plan(1 + 4)
client.departures(spichernstr, {duration: 1})