mirror of
https://github.com/public-transport/db-vendo-client.git
synced 2025-02-22 22:59:35 +02:00
createThrottledClient -> withThrottling 💥
This commit is contained in:
parent
c04e041338
commit
748f8ce6b0
3 changed files with 19 additions and 12 deletions
|
@ -16,10 +16,12 @@
|
|||
There's opt-in support for throttling requests to the endpoint.
|
||||
|
||||
```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')
|
||||
|
||||
// create a throttled HAFAS client with Deutsche Bahn profile
|
||||
const createThrottledClient = withThrottling(createClient)
|
||||
const client = createThrottledClient(dbProfile, 'my-awesome-program')
|
||||
|
||||
// Berlin Jungfernheide to München Hbf
|
||||
|
@ -28,11 +30,12 @@ client.journeys('8011167', '8000261', {results: 1})
|
|||
.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
|
||||
// 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
|
||||
|
|
|
@ -2,13 +2,14 @@
|
|||
|
||||
const test = require('tape')
|
||||
|
||||
const createThrottledClient = require('../throttle')
|
||||
const withThrottling = require('../throttle')
|
||||
const createClient = require('..')
|
||||
const vbbProfile = require('../p/vbb')
|
||||
|
||||
const userAgent = 'public-transport/hafas-client:test'
|
||||
const spichernstr = '900000042101'
|
||||
|
||||
test('throttle works', (t) => {
|
||||
test('withThrottling works', (t) => {
|
||||
let calls = 0
|
||||
const transformReqBody = (body) => {
|
||||
calls++
|
||||
|
@ -16,7 +17,8 @@ test('throttle works', (t) => {
|
|||
}
|
||||
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})
|
||||
|
||||
t.plan(3)
|
||||
|
|
14
throttle.js
14
throttle.js
|
@ -2,12 +2,14 @@
|
|||
|
||||
const throttle = require('p-throttle')
|
||||
|
||||
const request = require('./lib/request')
|
||||
const createClient = require('.')
|
||||
const _request = require('./lib/request')
|
||||
|
||||
const createThrottledClient = (profile, userAgent, limit = 5, interval = 1000) => {
|
||||
const throttledRequest = throttle(request, limit, interval)
|
||||
return createClient(profile, userAgent, throttledRequest)
|
||||
const withThrottling = (createClient, limit = 5, interval = 1000) => {
|
||||
const createThrottledClient = (profile, userAgent, request = _request) => {
|
||||
const throttledRequest = throttle(request, limit, interval)
|
||||
return createClient(profile, userAgent, throttledRequest)
|
||||
}
|
||||
return createThrottledClient
|
||||
}
|
||||
|
||||
module.exports = createThrottledClient
|
||||
module.exports = withThrottling
|
||||
|
|
Loading…
Add table
Reference in a new issue