Drop / skip messages - C++ Actor Framework (CAF)

282 views Asked by At

Using the C++ Actor Framework (CAF), I want to be able to skip / drop messages. E.g. incoming messages are being received at 100Hz. I only want the receiving actor to process messages at 1Hz (skipping 99 messages per second).

Does CAF provide any native functionality to do this?

Thanks.

1

There are 1 answers

0
neverlord On

There is support for both skipping and dropping. Skipping leaves messages in the mailbox, allowing actors to process it some later point after changing their behavior. Dropping is generally viewed as an error (unexpected messages).

The mechanism to do is in CAF is via default handlers. CAF dispatches any message that was not processed by the current behavior to a "fallback", which then decides what to do with the unmatched input.

You can override this handler however you want, but CAF also offers standard implementations to choose from:

  • skip: leaves the message in the mailbox. This message gets automatically re-matched later.
  • drop: considered an error. Terminates the receiver with an unexpected_message error, also sending an error message to the sender.
  • print_and_drop: like drop, but also prints an error to stderr (this is the default).

CAF also comes with examples showcasing how to use these handlers, e.g., https://github.com/actor-framework/actor-framework/blob/master/examples/dynamic_behavior/skip_messages.cpp. If you are looking for a "silent" drop that discards the message without an error:

caf::skippable_result silent_drop(scheduled_actor*, message&) {
  return caf::message(); // "void" result
}

All that being said, if all you are looking for is simply checking that some amount of time has passed before processing the next message: why not just leave the message handler early?

caf::behavior my_actor(caf::stateful_actor<my_state>* self) {
  return {
    [](const my_input& x) {
      if (!self->state.active())
        return;
      // ...
    }.
  };
}

Here, the idea is that active returns true only if some amount of time has passed since last processing a message.