Can I set maximum size for an actor\'s mailbox in Scala?
Take the Producer-Consumer problem. With threads I can block the producers when the buffer fills up. I saw a
The Actor.mailboxSize
method returns the number of pending messages in the Actor's mailbox.
This can be used for throttling the producer in various ways.
For example, one possibility could be,
The producer checks if the consumer's mailboxSize
is greater than some threshold. If it is, then it sends a SpecialMessage
to the consumer, and blocks on a semaphore. When the consumer receives this SpecialMessage
it releases the semaphore. The producer can now merrily continue it's business.
This avoids polling as well as any dropped messages.