简体   繁体   English

动态改变 Akka Streams 的节流级别

[英]Dynamically changing the throttling level on Akka Streams

有什么方法可以动态地改变源的节流参数,以实现一个上升机制,例如每过一秒节流就会减少/增加?

Akka throttle function offers calculating throttle based on cost. Akka 油门功能提供基于成本计算油门。 We can use it to dynamically change throttling rate.我们可以用它来动态改变节流率。

Example:例子:

private val weight = new AtomicInteger(1)

Source(1 to 1000)
  .throttle(1000, 1.second, _ => 1000 / weight.get())
  .runWith(Sink.foreach(println))

while (true) {
  val newWeight = StdIn.readLine().toInt
  weight.set(newWeight)
}

Initial throttling rate is 1/s.初始节流率为 1/s。 Then we can change it by changing weight.然后我们可以通过改变权重来改变它。 I used factor of 1000 due to costFunction returning integer.由于 costFunction 返回整数,我使用了 1000 的因子。 It can be increased for better granularity.可以增加它以获得更好的粒度。

If you want to change throttling based on passing time you just need to change weight as the time passes.如果你想根据时间改变节流,你只需要随着时间的推移改变权重。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM