Akka 2: How to pause processing of messages?

前端 未结 2 2044
不知归路
不知归路 2021-02-14 09:04

On my journey to grasp the Actor model using Akka many questions pop up. Here is another one. Say we have an Actor which has to stop processing messages for a given time because

相关标签:
2条回答
  • 2021-02-14 09:41
    case object NextEmail
    class EmailActor extends Actor {
    
    self ! NextEmail
    
      def receive = {
        case NextEmail =>
          sendEmailIfAnyToSend
          context.system.scheduler.scheduleOnce(3 seconds, self, NextEmail)                 
      }
    }
    
    0 讨论(0)
  • 2021-02-14 09:53

    General Answer

    Actors process messages always as fast as they can, where processing means taking them out of their mailbox and passing them into the actor’s behavior. The behavior is thus the place where your answer lies: change it to something more appropriate during periods of time which require non-nominal actions.

    Throttling

    If one component is processing messages at a lower rate than they are produced, it will have to drop messages eventually. Either use a bounded mailbox or put a manager in front which keeps track of the worker’s progress and goes into “reply with negative result” mode during periods of stress.

    When an actor wants to throttle its own output rate, use the context.system.scheduler.

    This should answer your first two points.

    Recovery

    During periods where a required resource is unavailable, you have two options depending on the requirements: either queue messages internally, or go into “out-of-order” reply mode. You can also mix, i.e. queue with certain time and space limits and fail when the limits are hit.

    Further Considerations

    Always keep the units of work processed by actors so small that the actor can react within its latency requirements. The latter could be very relaxed (running for hours uninterruptibly) or very strict (must process messages at kHz rate).

    0 讨论(0)
提交回复
热议问题