我想为observable创建一个速率限制器,它有条件地改变它限制值的方式。该用例适用于不断接收要下载的新网址的下载程序。我希望能够排队传入的URL并在两种排队方法之间切换。这两种方法是通过限制速率(例如,每2秒不超过10个请求)和并发请求的数量(例如,一次不能发出不超过10个请求)。
可以像下面这样实现一个简单的速率限制器(借用here):
const rateLimit = (limit, rate, scheduler = asyncScheduler) => {
let tokens = limit
const tokenChanged = new BehaviorSubject(tokens)
const consumeToken = () => tokenChanged.next(--tokens)
const renewToken = () => tokenChanged.next(++tokens)
const availableTokens = tokenChanged.pipe(filter(() => tokens > 0))
return source =>
source.pipe(
mergeMap(val =>
availableTokens.pipe(
take(1),
map(() => {
consumeToken()
timer(rate, scheduler).subscribe(renewToken)
return val
})
)
)
)
}
const o = urlSource.pipe(
rateLimit(10, 2000),
mergeMap(downloadUrl)
)
o.toPromise()
这是一个简单的并发限制器:
const o = urlSource.pipe(
mergeMap(downloadUrl, maxConcurrent)
)
o.toPromise()
最后,我可以创建这个组合切换器来选择使用哪种类型的限制器:
const toggleableLimiter = (func, limit, rate, concurrent, toggleObservable) => {
let useRateLimiter = true
toggleObservable.subscribe(() => (useRateLimiter = !useRateLimiter))
const rateLimiter = rateLimit(limit, rate)
return source => {
const operators = useRateLimiter
? [rateLimiter, mergeMap(func)]
: [mergeMap(func, concurrent)]
return source.pipe(...operators)
}
}
const e = new EventEmitter()
const toggler = fromEvent(e, 'toggle')
const o = urlSource.pipe(
toggleableLimiter(downloadUrl, 2, 1000, 2, toggler)
)
o.toPromise()
// using rate limiter
e.emit('toggle')
// incoming values now use concurrent limiter
这一切都可以解决我的问题。我可以使用事件发射器在两种方法之间切换。但问题是,在事件发生之前传递给toggleableLimiter
的任何内容都必须遵守该限制器运算符。我想知道的是,我是否可以有条件地将值保留在队列中,并选择如何随心所欲地限制排队值。
答案 0 :(得分:0)
确定!我有一个解决方案,不幸的是它涉及自己处理队列。我认为这是必要的,因为可观察性和背压的性质,我在这个issue中找到了很多关于这个主题的讨论。在过去,可以使用controlled
运算符更简单地处理此问题,但它是deprecated。相反,我只是自己使用timer
可观察和包装mergeMap
的并发控制(尽管mergeMap
仍在内部管理并发作为安全措施。
const rateLimitToggle = (func, limit, rate, maxConcurrent, toggler) => {
const rateTimer = Rx.timer(0, rate).pipe(ops.mapTo(true))
return source =>
new Rx.Observable(subscriber => {
const concurrentLimiter = new Rx.Subject()
// stateful vars
const queue = []
let inProgress = 0
let closed = false
const enqueue = val => {
queue.push(val)
concurrentLimiter.next()
}
const dequeue = useRateLimit => {
const availableSlots = useRateLimit ? limit : maxConcurrent - inProgress
const numberToDequeue = Math.min(availableSlots, queue.length)
const nextVals = queue.splice(0, numberToDequeue)
inProgress += availableSlots
return nextVals
}
Rx.merge(Rx.of(true), toggler)
.pipe(
ops.switchMap(useRateLimiter => (useRateLimiter ? rateTimer : concurrentLimiter)),
ops.takeWhile(() => !closed || queue.length),
ops.mergeMap(dequeue),
ops.mergeMap(val => func(val), maxConcurrent)
)
.subscribe(val => {
inProgress--
concurrentLimiter.next()
subscriber.next(val)
})
source.subscribe({
next(val) {
enqueue(val)
},
complete() {
closed = true
}
})
})
}
示例用法:
const timeout = n => val => {
console.log('started', val)
return new Promise(resolve => setTimeout(() => resolve(val), n))
}
const emitter = new EventEmitter()
const toggler = Rx.fromEvent(emitter, 'useRateLimiter')
const downloader = Rx.range(0, 10).pipe(
rateLimitToggle(timeout(1000), 2, 1000, 10, toggler)
)
downloader.subscribe(val => console.log('finished', val))
setTimeout(() => {
console.log('now use concurrentLimiter')
emitter.emit('useRateLimiter', false)
}, 2000)
/* outputs:
started 0
started 1
started 2
started 3 // 0 - 3 all executed under rateLimiter
finished 0
finished 1
now use concurrentLimiter
started 4 // 4 - 9 executed under concurrentLimiter
started 5
started 6
started 7
started 8
started 9
finished 2
finished 3
finished 4
finished 5
finished 6
finished 7
finished 8
finished 9
*/