pekko/akka-stream/src/main/scala/akka/stream/impl/Throttle.scala

89 lines
2.9 KiB
Scala
Raw Normal View History

/*
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
*/
2015-11-08 19:27:03 -05:00
package akka.stream.impl
import akka.annotation.InternalApi
import akka.stream.ThrottleMode.Enforcing
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
import scala.concurrent.duration.{ FiniteDuration, _ }
/**
* INTERNAL API
*/
@InternalApi private[akka] object Throttle {
final val AutomaticMaximumBurst = -1
private case object TimerKey
}
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
// 100 ms is a realistic minimum between tokens, otherwise the maximumBurst is adjusted
// to be able to support higher rates
val effectiveMaximumBurst: Long =
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")
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
override def preStart(): Unit = tokenBucket.init()
2015-11-08 19:27:03 -05:00
override def onUpstreamFinish(): Unit =
if (!(isAvailable(out) && isTimerActive(Throttle.TimerKey))) {
completeStage()
}
2016-01-23 17:55:03 -05:00
override def onPush(): Unit = {
val elem = grab(in)
val cost = costCalculation(elem)
val delayNanos = tokenBucket.offer(cost)
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 {
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
override def onPull(): Unit = pull(in)
2015-11-08 19:27:03 -05: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
setHandlers(in, out, this)
}
2015-11-08 19:27:03 -05:00
override def toString = "Throttle"
}