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
case object NextEmail
class EmailActor extends Actor {
self ! NextEmail
def receive = {
case NextEmail =>
sendEmailIfAnyToSend
context.system.scheduler.scheduleOnce(3 seconds, self, NextEmail)
}
}
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.
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.
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.
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).