2018-10-29 17:19:37 +08:00
|
|
|
/*
|
2022-02-04 12:36:44 +01:00
|
|
|
* Copyright (C) 2015-2022 Lightbend Inc. <https://www.lightbend.com>
|
2015-11-08 19:27:03 -05:00
|
|
|
*/
|
2018-03-13 23:45:55 +09:00
|
|
|
|
2015-11-08 19:27:03 -05:00
|
|
|
package akka.stream.impl
|
|
|
|
|
|
2017-03-16 21:04:07 +02:00
|
|
|
import akka.annotation.InternalApi
|
2022-03-25 16:20:31 +08:00
|
|
|
import akka.stream.ThrottleMode.Enforcing
|
2020-04-27 20:32:18 +08:00
|
|
|
import akka.stream._
|
2015-11-08 19:27:03 -05:00
|
|
|
import akka.stream.impl.fusing.GraphStages.SimpleLinearGraphStage
|
|
|
|
|
import akka.stream.stage._
|
2016-02-25 11:19:52 +01:00
|
|
|
import akka.util.NanoTimeTokenBucket
|
2015-11-08 19:27:03 -05:00
|
|
|
|
2022-03-25 16:20:31 +08:00
|
|
|
import scala.concurrent.duration.{ FiniteDuration, _ }
|
|
|
|
|
|
2018-04-02 16:07:16 +02:00
|
|
|
/**
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
*/
|
|
|
|
|
@InternalApi private[akka] object Throttle {
|
|
|
|
|
final val AutomaticMaximumBurst = -1
|
2022-03-25 16:20:31 +08:00
|
|
|
private case object TimerKey
|
2018-04-02 16:07:16 +02:00
|
|
|
}
|
|
|
|
|
|
2015-11-08 19:27:03 -05:00
|
|
|
/**
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
*/
|
2019-03-13 10:56:20 +01:00
|
|
|
@InternalApi private[akka] class Throttle[T](
|
|
|
|
|
val cost: Int,
|
|
|
|
|
val per: FiniteDuration,
|
|
|
|
|
val maximumBurst: Int,
|
|
|
|
|
val costCalculation: (T) => Int,
|
|
|
|
|
val mode: ThrottleMode)
|
2019-03-11 10:38:24 +01:00
|
|
|
extends SimpleLinearGraphStage[T] {
|
2016-01-18 17:49:32 +01:00
|
|
|
require(cost > 0, "cost must be > 0")
|
2016-02-25 11:19:52 +01:00
|
|
|
require(per.toNanos > 0, "per time must be > 0")
|
|
|
|
|
require(per.toNanos >= cost, "Rates larger than 1 unit / nanosecond are not supported")
|
2015-11-08 19:27:03 -05:00
|
|
|
|
2016-02-25 11:19:52 +01:00
|
|
|
// There is some loss of precision here because of rounding, but this only happens if nanosBetweenTokens is very
|
|
|
|
|
// small which is usually at rates where that precision is highly unlikely anyway as the overhead of this stage
|
|
|
|
|
// is likely higher than the required accuracy interval.
|
|
|
|
|
private val nanosBetweenTokens = per.toNanos / cost
|
2018-04-02 16:07:16 +02:00
|
|
|
// 100 ms is a realistic minimum between tokens, otherwise the maximumBurst is adjusted
|
|
|
|
|
// to be able to support higher rates
|
2021-07-06 18:01:41 +02:00
|
|
|
val effectiveMaximumBurst: Long =
|
2018-04-02 16:07:16 +02:00
|
|
|
if (maximumBurst == Throttle.AutomaticMaximumBurst) math.max(1, ((100 * 1000 * 1000) / nanosBetweenTokens))
|
|
|
|
|
else maximumBurst
|
|
|
|
|
require(!(mode == ThrottleMode.Enforcing && effectiveMaximumBurst < 0), "maximumBurst must be > 0 in Enforcing mode")
|
|
|
|
|
|
2022-03-25 16:20:31 +08:00
|
|
|
override def createLogic(inheritedAttributes: Attributes): GraphStageLogic =
|
|
|
|
|
new TimerGraphStageLogic(shape) with InHandler with OutHandler {
|
|
|
|
|
private val tokenBucket = new NanoTimeTokenBucket(effectiveMaximumBurst, nanosBetweenTokens)
|
|
|
|
|
private var currentElement: T = _
|
2015-11-08 19:27:03 -05:00
|
|
|
|
2022-03-25 16:20:31 +08:00
|
|
|
override def preStart(): Unit = tokenBucket.init()
|
2015-11-08 19:27:03 -05:00
|
|
|
|
2022-03-25 16:20:31 +08:00
|
|
|
override def onUpstreamFinish(): Unit =
|
|
|
|
|
if (!(isAvailable(out) && isTimerActive(Throttle.TimerKey))) {
|
|
|
|
|
completeStage()
|
|
|
|
|
}
|
2016-01-23 17:55:03 -05:00
|
|
|
|
2022-03-25 16:20:31 +08:00
|
|
|
override def onPush(): Unit = {
|
|
|
|
|
val elem = grab(in)
|
|
|
|
|
val cost = costCalculation(elem)
|
|
|
|
|
val delayNanos = tokenBucket.offer(cost)
|
2016-02-24 13:19:10 +01:00
|
|
|
|
2022-03-25 16:20:31 +08:00
|
|
|
if (delayNanos == 0L) push(out, elem)
|
|
|
|
|
else {
|
|
|
|
|
if (mode eq Enforcing) failStage(new RateExceededException("Maximum throttle throughput exceeded."))
|
2016-01-23 17:55:03 -05:00
|
|
|
else {
|
2022-03-25 16:20:31 +08:00
|
|
|
currentElement = elem
|
|
|
|
|
scheduleOnce(Throttle.TimerKey, delayNanos.nanos)
|
2016-01-23 17:55:03 -05:00
|
|
|
}
|
2015-11-08 19:27:03 -05:00
|
|
|
}
|
|
|
|
|
}
|
2016-02-25 11:19:52 +01:00
|
|
|
|
2022-03-25 16:20:31 +08:00
|
|
|
override def onPull(): Unit = pull(in)
|
2015-11-08 19:27:03 -05:00
|
|
|
|
2022-03-25 16:20:31 +08:00
|
|
|
override protected def onTimer(key: Any): Unit = {
|
|
|
|
|
push(out, currentElement)
|
|
|
|
|
currentElement = null.asInstanceOf[T]
|
|
|
|
|
if (isClosed(in)) completeStage()
|
|
|
|
|
}
|
2015-11-08 19:27:03 -05:00
|
|
|
|
2022-03-25 16:20:31 +08:00
|
|
|
setHandlers(in, out, this)
|
|
|
|
|
}
|
2015-11-08 19:27:03 -05:00
|
|
|
|
|
|
|
|
override def toString = "Throttle"
|
2016-05-03 18:58:26 -07:00
|
|
|
}
|