Class

akka.contrib.throttle

TimerBasedThrottler

Related Doc: package throttle

Permalink

class TimerBasedThrottler extends Actor with FSM[State, Data]

A throttler that uses a timer to control the message delivery rate.

Throttling

A throttler is an actor that is defined through a target actor and a rate (of type akka.contrib.throttle.Throttler.Rate). You set or change the target and rate at any time through the akka.contrib.throttle.Throttler.SetTarget and akka.contrib.throttle.Throttler.SetRate messages, respectively. When you send the throttler any other message msg, it will put the message msg into an internal queue and eventually send all queued messages to the target, at a speed that respects the given rate. If no target is currently defined then the messages will be queued and will be delivered as soon as a target gets set.

A throttler understands actor messages of type akka.contrib.throttle.Throttler.SetTarget, akka.contrib.throttle.Throttler.SetRate, in addition to any other messages, which the throttler will consider as messages to be sent to the target.

Transparency

Notice that the throttler forwards messages, i.e., the target will see the original message sender (and not the throttler) as the sender of the message.

Persistence

Throttlers usually use an internal queue to keep the messages that need to be sent to the target. You therefore cannot rely on the throttler's inbox size in order to learn how much messages are outstanding.

It is left to the implementation whether the internal queue is persisted over application restarts or actor failure.

Processing messages

The target should process messages as fast as possible. If the target requires substantial time to process messages, it should distribute its work to other actors (using for example something like a BalancingDispatcher), otherwise the resulting system will always work below the threshold rate.

Example: Suppose the throttler has a rate of 3msg/s and the target requires 1s to process a message. This system will only process messages at a rate of 1msg/s: the target will receive messages at at most 3msg/s but as it handles them synchronously and each of them takes 1s, its inbox will grow and grow. In such a situation, the target should distribute its messages to a set of worker actors so that individual messages can be handled in parallel.

Example

For example, if you set a rate like "3 messages in 1 second", the throttler will send the first three messages immediately to the target actor but will need to impose a delay before sending out further messages:

// A simple actor that prints whatever it receives
class Printer extends Actor {
  def receive = {
    case x => println(x)
  }
}

val printer = system.actorOf(Props[Printer], "printer")

// The throttler for this example, setting the rate
val throttler = system.actorOf(Props(classOf[TimerBasedThrottler], 3 msgsPer 1.second))

// Set the target
throttler ! SetTarget(Some(printer))
// These three messages will be sent to the printer immediately
throttler ! "1"
throttler ! "2"
throttler ! "3"
// These two will wait at least until 1 second has passed
throttler ! "4"
throttler ! "5"

Implementation notes

This throttler implementation internally installs a timer that repeats every rate.durationInMillis and enables rate.numberOfCalls additional calls to take place. A TimerBasedThrottler uses very few system resources, provided the rate's duration is not too fine-grained (which would cause a lot of timer invocations); for example, it does not store the calling history as other throttlers may need to do.

However, a TimerBasedThrottler only provides weak guarantees on the rate (see also this blog post):

For some applications, these guarantees may not be sufficient.

Known issues

Annotations
@deprecated
Deprecated

(Since version 2.5.0) Use streams, see migration guide

Source
TimerBasedThrottler.scala
See also

akka.contrib.throttle.Throttler

Linear Supertypes
FSM[TimerBasedThrottler.State, Data], ActorLogging, Listeners, Actor, AnyRef, Any
Type Hierarchy
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. TimerBasedThrottler
  2. FSM
  3. ActorLogging
  4. Listeners
  5. Actor
  6. AnyRef
  7. Any
Implicitly
  1. by any2stringadd
  2. by StringFormat
  3. by Ensuring
  4. by ArrowAssoc
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Instance Constructors

  1. new TimerBasedThrottler(rate: Rate)

    Permalink

Type Members

  1. type Event = actor.FSM.Event[Data]

    Permalink
    Definition Classes
    FSM
  2. type Receive = PartialFunction[Any, Unit]

    Permalink
    Definition Classes
    Actor
  3. type State = actor.FSM.State[TimerBasedThrottler.State, Data]

    Permalink
    Definition Classes
    FSM
  4. type StateFunction = PartialFunction[Event, State]

    Permalink
    Definition Classes
    FSM
  5. type StopEvent = actor.FSM.StopEvent[TimerBasedThrottler.State, Data]

    Permalink
    Definition Classes
    FSM
  6. type Timeout = Option[FiniteDuration]

    Permalink
    Definition Classes
    FSM
  7. final class TransformHelper extends AnyRef

    Permalink
    Definition Classes
    FSM
  8. type TransitionHandler = PartialFunction[(TimerBasedThrottler.State, TimerBasedThrottler.State), Unit]

    Permalink
    Definition Classes
    FSM

Value Members

  1. final def !=(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  3. def +(other: String): String

    Permalink
    Implicit information
    This member is added by an implicit conversion from TimerBasedThrottler to any2stringadd[TimerBasedThrottler] performed by method any2stringadd in scala.Predef.
    Definition Classes
    any2stringadd
  4. val ->: actor.FSM.->.type

    Permalink
    Definition Classes
    FSM
  5. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  6. val Event: actor.FSM.Event.type

    Permalink
    Definition Classes
    FSM
  7. val StateTimeout: actor.FSM.StateTimeout.type

    Permalink
    Definition Classes
    FSM
  8. val StopEvent: actor.FSM.StopEvent.type

    Permalink
    Definition Classes
    FSM
  9. def aroundPostRestart(reason: Throwable): Unit

    Permalink
    Attributes
    protected[akka]
    Definition Classes
    Actor
    Annotations
    @InternalApi()
  10. def aroundPostStop(): Unit

    Permalink
    Attributes
    protected[akka]
    Definition Classes
    Actor
    Annotations
    @InternalApi()
  11. def aroundPreRestart(reason: Throwable, message: Option[Any]): Unit

    Permalink
    Attributes
    protected[akka]
    Definition Classes
    Actor
    Annotations
    @InternalApi()
  12. def aroundPreStart(): Unit

    Permalink
    Attributes
    protected[akka]
    Definition Classes
    Actor
    Annotations
    @InternalApi()
  13. def aroundReceive(receive: actor.Actor.Receive, msg: Any): Unit

    Permalink
    Attributes
    protected[akka]
    Definition Classes
    Actor
    Annotations
    @InternalApi()
  14. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  15. final def cancelTimer(name: String): Unit

    Permalink
    Definition Classes
    FSM
  16. def clone(): AnyRef

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  17. implicit val context: ActorContext

    Permalink
    Definition Classes
    Actor
  18. def ensuring(cond: (TimerBasedThrottler) ⇒ Boolean, msg: ⇒ Any): TimerBasedThrottler

    Permalink
    Implicit information
    This member is added by an implicit conversion from TimerBasedThrottler to Ensuring[TimerBasedThrottler] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  19. def ensuring(cond: (TimerBasedThrottler) ⇒ Boolean): TimerBasedThrottler

    Permalink
    Implicit information
    This member is added by an implicit conversion from TimerBasedThrottler to Ensuring[TimerBasedThrottler] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  20. def ensuring(cond: Boolean, msg: ⇒ Any): TimerBasedThrottler

    Permalink
    Implicit information
    This member is added by an implicit conversion from TimerBasedThrottler to Ensuring[TimerBasedThrottler] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  21. def ensuring(cond: Boolean): TimerBasedThrottler

    Permalink
    Implicit information
    This member is added by an implicit conversion from TimerBasedThrottler to Ensuring[TimerBasedThrottler] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  22. final def eq(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  23. def equals(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  24. def finalize(): Unit

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  25. def formatted(fmtstr: String): String

    Permalink
    Implicit information
    This member is added by an implicit conversion from TimerBasedThrottler to StringFormat[TimerBasedThrottler] performed by method StringFormat in scala.Predef.
    Definition Classes
    StringFormat
    Annotations
    @inline()
  26. final def getClass(): Class[_]

    Permalink
    Definition Classes
    AnyRef → Any
  27. def gossip(msg: Any)(implicit sender: ActorRef): Unit

    Permalink
    Attributes
    protected
    Definition Classes
    Listeners
  28. final def goto(nextStateName: TimerBasedThrottler.State): State

    Permalink
    Definition Classes
    FSM
  29. def hashCode(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  30. final def initialize(): Unit

    Permalink
    Definition Classes
    FSM
  31. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  32. final def isTimerActive(name: String): Boolean

    Permalink
    Definition Classes
    FSM
  33. def listenerManagement: actor.Actor.Receive

    Permalink
    Attributes
    protected
    Definition Classes
    Listeners
  34. val listeners: Set[ActorRef]

    Permalink
    Attributes
    protected
    Definition Classes
    Listeners
  35. def log: LoggingAdapter

    Permalink
    Definition Classes
    ActorLogging
  36. def logTermination(reason: Reason): Unit

    Permalink
    Attributes
    protected
    Definition Classes
    FSM
  37. final def ne(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  38. final def nextStateData: Data

    Permalink
    Definition Classes
    FSM
  39. final def notify(): Unit

    Permalink
    Definition Classes
    AnyRef
  40. final def notifyAll(): Unit

    Permalink
    Definition Classes
    AnyRef
  41. final def onTermination(terminationHandler: PartialFunction[StopEvent, Unit]): Unit

    Permalink
    Definition Classes
    FSM
  42. final def onTransition(transitionHandler: TransitionHandler): Unit

    Permalink
    Definition Classes
    FSM
  43. def postRestart(reason: Throwable): Unit

    Permalink
    Definition Classes
    Actor
    Annotations
    @throws( classOf[java.lang.Exception] )
  44. def postStop(): Unit

    Permalink
    Definition Classes
    FSM → Actor
  45. def preRestart(reason: Throwable, message: Option[Any]): Unit

    Permalink
    Definition Classes
    Actor
    Annotations
    @throws( classOf[java.lang.Exception] )
  46. def preStart(): Unit

    Permalink
    Definition Classes
    Actor
    Annotations
    @throws( classOf[java.lang.Exception] )
  47. var rate: Rate

    Permalink
  48. def receive: Receive

    Permalink
    Definition Classes
    FSM → Actor
  49. implicit final val self: ActorRef

    Permalink
    Definition Classes
    Actor
  50. final def sender(): ActorRef

    Permalink
    Definition Classes
    Actor
  51. final def setStateTimeout(state: TimerBasedThrottler.State, timeout: Timeout): Unit

    Permalink
    Definition Classes
    FSM
  52. final def setTimer(name: String, msg: Any, timeout: FiniteDuration, repeat: Boolean): Unit

    Permalink
    Definition Classes
    FSM
  53. final def startWith(stateName: TimerBasedThrottler.State, stateData: Data, timeout: Timeout): Unit

    Permalink
    Definition Classes
    FSM
  54. final def stateData: Data

    Permalink
    Definition Classes
    FSM
  55. final def stateName: TimerBasedThrottler.State

    Permalink
    Definition Classes
    FSM
  56. final def stay(): State

    Permalink
    Definition Classes
    FSM
  57. final def stop(reason: Reason, stateData: Data): State

    Permalink
    Definition Classes
    FSM
  58. final def stop(reason: Reason): State

    Permalink
    Definition Classes
    FSM
  59. final def stop(): State

    Permalink
    Definition Classes
    FSM
  60. def supervisorStrategy: SupervisorStrategy

    Permalink
    Definition Classes
    Actor
  61. final def synchronized[T0](arg0: ⇒ T0): T0

    Permalink
    Definition Classes
    AnyRef
  62. def toString(): String

    Permalink
    Definition Classes
    AnyRef → Any
  63. implicit final def total2pf(transitionHandler: (TimerBasedThrottler.State, TimerBasedThrottler.State) ⇒ Unit): TransitionHandler

    Permalink
    Definition Classes
    FSM
  64. final def transform(func: StateFunction): TransformHelper

    Permalink
    Definition Classes
    FSM
  65. def unhandled(message: Any): Unit

    Permalink
    Definition Classes
    Actor
  66. final def wait(): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  67. final def wait(arg0: Long, arg1: Int): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  68. final def wait(arg0: Long): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  69. final def when(stateName: TimerBasedThrottler.State, stateTimeout: FiniteDuration)(stateFunction: StateFunction): Unit

    Permalink
    Definition Classes
    FSM
  70. final def whenUnhandled(stateFunction: StateFunction): Unit

    Permalink
    Definition Classes
    FSM
  71. def [B](y: B): (TimerBasedThrottler, B)

    Permalink
    Implicit information
    This member is added by an implicit conversion from TimerBasedThrottler to ArrowAssoc[TimerBasedThrottler] performed by method ArrowAssoc in scala.Predef.
    Definition Classes
    ArrowAssoc

Shadowed Implicit Value Members

  1. def ->[B](y: B): (TimerBasedThrottler, B)

    Permalink
    Implicit information
    This member is added by an implicit conversion from TimerBasedThrottler to ArrowAssoc[TimerBasedThrottler] performed by method ArrowAssoc in scala.Predef.
    Shadowing
    This implicitly inherited member is shadowed by one or more members in this class.
    To access this member you can use a type ascription:
    (timerBasedThrottler: ArrowAssoc[TimerBasedThrottler]).->(y)
    Definition Classes
    ArrowAssoc
    Annotations
    @inline()

Inherited from FSM[TimerBasedThrottler.State, Data]

Inherited from ActorLogging

Inherited from Listeners

Inherited from Actor

Inherited from AnyRef

Inherited from Any

Inherited by implicit conversion any2stringadd from TimerBasedThrottler to any2stringadd[TimerBasedThrottler]

Inherited by implicit conversion StringFormat from TimerBasedThrottler to StringFormat[TimerBasedThrottler]

Inherited by implicit conversion Ensuring from TimerBasedThrottler to Ensuring[TimerBasedThrottler]

Inherited by implicit conversion ArrowAssoc from TimerBasedThrottler to ArrowAssoc[TimerBasedThrottler]

Ungrouped