2018-01-23 15:54:39 +01:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const throttle = require('p-throttle')
|
|
|
|
|
2019-02-08 14:59:21 +01:00
|
|
|
const _request = require('./lib/request')
|
|
|
|
|
|
|
|
const withThrottling = (createClient, limit = 5, interval = 1000) => {
|
2019-09-28 22:15:22 +02:00
|
|
|
const createThrottledClient = (profile, userAgent, opt = {}) => {
|
|
|
|
const request = 'request' in opt ? opt.request : _request
|
|
|
|
|
2019-02-08 14:59:21 +01:00
|
|
|
const throttledRequest = throttle(request, limit, interval)
|
2019-09-28 22:15:22 +02:00
|
|
|
|
|
|
|
return createClient(profile, userAgent, {
|
|
|
|
...opt,
|
|
|
|
request: throttledRequest
|
|
|
|
})
|
2019-02-08 14:59:21 +01:00
|
|
|
}
|
|
|
|
return createThrottledClient
|
2018-01-23 15:54:39 +01:00
|
|
|
}
|
|
|
|
|
2019-02-08 14:59:21 +01:00
|
|
|
module.exports = withThrottling
|