【发布时间】:2020-12-16 19:23:19
【问题描述】:
zio-streams 提供 throttleShape 其中
/**
* Delays the chunks of this stream according to the given bandwidth parameters using the token bucket
* algorithm. Allows for burst in the processing of elements by allowing the token bucket to accumulate
* tokens up to a `units + burst` threshold. The weight of each chunk is determined by the `costFn`
* function.
*/
final def throttleShape(units: Long, duration: Duration, burst: Long = 0)(
costFn: Chunk[O] => Long
): ZStream[R with Clock, E, O]
我很难理解参数unit、duration、burst 和costFun 是如何使用的。从我阅读token bucket
throttleShape(1, 1.second)(_ => 1)
表示处理一个元素需要一个令牌(costFun = _ => 1),而一个令牌(unit = 1)在一秒钟后被补充(duration = 1.second)。然而,我对各种值的实验似乎并没有导致任何限制,除了
throttleShape(1, 1.second)(_ => 2)
这使它挂起。例如,如何解释以下使用无限持续时间的 sn-ps(来自 PR)中的节流
Stream(1, 2, 3, 4)
.throttleShape(1, Duration.Infinity)(_ => 0)
.runCollect
Stream(1, 2, 3, 4)
.throttleShape(2, Duration.Infinity)(_ => 1)
.take(2)
.runCollect
具体来说,假设我想每分钟最多处理 100 个元素,那么应该如何指定 throttleShape?
【问题讨论】:
标签: scala throttling zio