createThrottledClient -> withThrottling 💥

This commit is contained in:
Jannis R 2019-02-08 14:59:21 +01:00
parent c04e041338
commit 748f8ce6b0
No known key found for this signature in database
GPG key ID: 0FE83946296A88A5
3 changed files with 19 additions and 12 deletions

View file

@ -16,10 +16,12 @@
There's opt-in support for throttling requests to the endpoint. There's opt-in support for throttling requests to the endpoint.
```js ```js
const createThrottledClient = require('hafas-client/throttle') const withThrottling = require('hafas-client/throttle')
const createClient = require('hafas-client')
const dbProfile = require('hafas-client/p/db') const dbProfile = require('hafas-client/p/db')
// create a throttled HAFAS client with Deutsche Bahn profile // create a throttled HAFAS client with Deutsche Bahn profile
const createThrottledClient = withThrottling(createClient)
const client = createThrottledClient(dbProfile, 'my-awesome-program') const client = createThrottledClient(dbProfile, 'my-awesome-program')
// Berlin Jungfernheide to München Hbf // Berlin Jungfernheide to München Hbf
@ -28,11 +30,12 @@ client.journeys('8011167', '8000261', {results: 1})
.catch(console.error) .catch(console.error)
``` ```
You can pass custom values for the nr of requests (`limit`) per interval into `createThrottledClient`: You can pass custom values for the nr of requests (`limit`) per interval into `withThrottling`:
```js ```js
// 2 requests per second // 2 requests per second
const client = createThrottledClient(dbProfile, 'my-awesome-program', 2, 1000) const createThrottledClient = withThrottling(createClient, 2, 1000)
const client = createThrottledClient(dbProfile, 'my-awesome-program')
``` ```
## Retrying failed requests ## Retrying failed requests

View file

@ -2,13 +2,14 @@
const test = require('tape') const test = require('tape')
const createThrottledClient = require('../throttle') const withThrottling = require('../throttle')
const createClient = require('..')
const vbbProfile = require('../p/vbb') const vbbProfile = require('../p/vbb')
const userAgent = 'public-transport/hafas-client:test' const userAgent = 'public-transport/hafas-client:test'
const spichernstr = '900000042101' const spichernstr = '900000042101'
test('throttle works', (t) => { test('withThrottling works', (t) => {
let calls = 0 let calls = 0
const transformReqBody = (body) => { const transformReqBody = (body) => {
calls++ calls++
@ -16,7 +17,8 @@ test('throttle works', (t) => {
} }
const mockProfile = Object.assign({}, vbbProfile, {transformReqBody}) const mockProfile = Object.assign({}, vbbProfile, {transformReqBody})
const client = createThrottledClient(mockProfile, userAgent, 2, 1000) const createThrottlingClient = withThrottling(createClient, 2, 1000)
const client = createThrottlingClient(mockProfile, userAgent)
for (let i = 0; i < 10; i++) client.departures(spichernstr, {duration: 1}) for (let i = 0; i < 10; i++) client.departures(spichernstr, {duration: 1})
t.plan(3) t.plan(3)

View file

@ -2,12 +2,14 @@
const throttle = require('p-throttle') const throttle = require('p-throttle')
const request = require('./lib/request') const _request = require('./lib/request')
const createClient = require('.')
const createThrottledClient = (profile, userAgent, limit = 5, interval = 1000) => { const withThrottling = (createClient, limit = 5, interval = 1000) => {
const createThrottledClient = (profile, userAgent, request = _request) => {
const throttledRequest = throttle(request, limit, interval) const throttledRequest = throttle(request, limit, interval)
return createClient(profile, userAgent, throttledRequest) return createClient(profile, userAgent, throttledRequest)
} }
return createThrottledClient
}
module.exports = createThrottledClient module.exports = withThrottling