mirror of
https://github.com/public-transport/db-vendo-client.git
synced 2025-02-23 07:09:35 +02:00
createClientWithRetry -> withRetrying 💥
This commit is contained in:
parent
748f8ce6b0
commit
fbde6a171b
3 changed files with 33 additions and 27 deletions
|
@ -43,11 +43,13 @@ const client = createThrottledClient(dbProfile, 'my-awesome-program')
|
||||||
There's opt-in support for retrying failed requests to the endpoint.
|
There's opt-in support for retrying failed requests to the endpoint.
|
||||||
|
|
||||||
```js
|
```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')
|
const dbProfile = require('hafas-client/p/db')
|
||||||
|
|
||||||
// create a client with Deutsche Bahn profile that will retry on HAFAS errors
|
// 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
|
// Berlin Jungfernheide to München Hbf
|
||||||
client.journeys('8011167', '8000261', {results: 1})
|
client.journeys('8011167', '8000261', {results: 1})
|
||||||
|
@ -55,15 +57,16 @@ client.journeys('8011167', '8000261', {results: 1})
|
||||||
.catch(console.error)
|
.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
|
```js
|
||||||
// retry 2 times, after 10 seconds & 30 seconds
|
// retry 2 times, after 10 seconds & 30 seconds
|
||||||
const client = createClientWithRetry(dbProfile, 'my-awesome-program', {
|
const createRetryingClient = withRetrying(createClient, {
|
||||||
retries: 2,
|
retries: 2,
|
||||||
minTimeout: 10 * 1000,
|
minTimeout: 10 * 1000,
|
||||||
factor: 3
|
factor: 3
|
||||||
})
|
})
|
||||||
|
const client = createRetryingClient(dbProfile, 'my-awesome-program')
|
||||||
```
|
```
|
||||||
|
|
||||||
## Writing a profile
|
## Writing a profile
|
||||||
|
|
38
retry.js
38
retry.js
|
@ -3,7 +3,6 @@
|
||||||
const retry = require('p-retry')
|
const retry = require('p-retry')
|
||||||
|
|
||||||
const _request = require('./lib/request')
|
const _request = require('./lib/request')
|
||||||
const createClient = require('.')
|
|
||||||
|
|
||||||
const retryDefaults = {
|
const retryDefaults = {
|
||||||
retries: 3,
|
retries: 3,
|
||||||
|
@ -11,26 +10,29 @@ const retryDefaults = {
|
||||||
minTimeout: 5 * 1000
|
minTimeout: 5 * 1000
|
||||||
}
|
}
|
||||||
|
|
||||||
const createClientWithRetry = (profile, userAgent, retryOpts = {}, request = _request) => {
|
const withRetrying = (createClient, retryOpts = {}) => {
|
||||||
retryOpts = Object.assign({}, retryDefaults, retryOpts)
|
retryOpts = Object.assign({}, retryDefaults, retryOpts)
|
||||||
|
|
||||||
const requestWithRetry = (profile, userAgent, opt, data) => {
|
const createRetryingClient = (profile, userAgent, request = _request) => {
|
||||||
const attempt = () => {
|
const retryingRequest = (profile, userAgent, opt, data) => {
|
||||||
return request(profile, userAgent, opt, data)
|
const attempt = () => {
|
||||||
.catch((err) => {
|
return request(profile, userAgent, opt, data)
|
||||||
if (err.isHafasError) throw err // continue
|
.catch((err) => {
|
||||||
if (err.code === 'ENOTFOUND') { // abort
|
if (err.isHafasError) throw err // continue
|
||||||
const abortErr = new retry.AbortError(err)
|
if (err.code === 'ENOTFOUND') { // abort
|
||||||
Object.assign(abortErr, err)
|
const abortErr = new retry.AbortError(err)
|
||||||
throw abortErr
|
Object.assign(abortErr, err)
|
||||||
}
|
throw abortErr
|
||||||
throw err // continue
|
}
|
||||||
})
|
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
|
||||||
|
|
|
@ -2,14 +2,14 @@
|
||||||
|
|
||||||
const test = require('tape')
|
const test = require('tape')
|
||||||
|
|
||||||
const createClientWithRetry = require('../retry')
|
const withRetrying = require('../retry')
|
||||||
|
const createClient = require('..')
|
||||||
const vbbProfile = require('../p/vbb')
|
const vbbProfile = require('../p/vbb')
|
||||||
const _request = require('../lib/request')
|
|
||||||
|
|
||||||
const userAgent = 'public-transport/hafas-client:test'
|
const userAgent = 'public-transport/hafas-client:test'
|
||||||
const spichernstr = '900000042101'
|
const spichernstr = '900000042101'
|
||||||
|
|
||||||
test('retry works', (t) => {
|
test('withRetrying works', (t) => {
|
||||||
// for the first 3 calls, return different kinds of errors
|
// for the first 3 calls, return different kinds of errors
|
||||||
let calls = 0
|
let calls = 0
|
||||||
const failingRequest = (profile, userAgent, opt, data) => {
|
const failingRequest = (profile, userAgent, opt, data) => {
|
||||||
|
@ -28,12 +28,13 @@ test('retry works', (t) => {
|
||||||
return Promise.resolve([])
|
return Promise.resolve([])
|
||||||
}
|
}
|
||||||
|
|
||||||
const client = createClientWithRetry(vbbProfile, userAgent, {
|
const createRetryingClient = withRetrying(createClient, {
|
||||||
retries: 3,
|
retries: 3,
|
||||||
minTimeout: 100,
|
minTimeout: 100,
|
||||||
factor: 2,
|
factor: 2,
|
||||||
randomize: false
|
randomize: false
|
||||||
}, failingRequest)
|
})
|
||||||
|
const client = createRetryingClient(vbbProfile, userAgent, failingRequest)
|
||||||
|
|
||||||
t.plan(1 + 4)
|
t.plan(1 + 4)
|
||||||
client.departures(spichernstr, {duration: 1})
|
client.departures(spichernstr, {duration: 1})
|
||||||
|
|
Loading…
Add table
Reference in a new issue