Message Priority Actor - thoughtworks-hpc/cdcf GitHub Wiki

Why you should use it

CAF has built-in message priority support. But in our test, in the particular version of CAF in CDCF, we find that the priority in messages received on remotely spawned actors seems lost in the process of message transmission. That is why we create the actor type MessagePriorityActor.

Note: Other version of CAF may not have the aforementioned problem, in that case, MessagePriorityActor is not necessary.

What is it

MessagePriorityActor is a derivative of caf::event_based_actor.

When using the functionality of MessagePriorityActor, user's actor should derive from it and will always be dynamically typed.

How to use it

Send message to MessagePriorityActor

When sending message to MessagePriorityActor, you have to specify the priority of the message by using high_priority_atom for high priority message or normal_priority_atom for normal priority message.

MessagePriorityActor has default queue for saving normal priority message and urgent queue for saving high priority message. Messages in urgent queue will be handled first.

Since the default priority for message is normal, normal_priority_atom can be omitted.

auto Dest = system.spawn<CalculatorWithPriority>();

// send high priority message to Dest
self->send(Dest, high_priority_atom::value, 1, 2);

// send normal priority message to Dest
self->send(Dest, normal_priority_atom::value, 1, 2);
self->send(Dest, 1, 2);
self->request(Dest, caf::infinite, normal_priority_atom::value, 1, 2);

Handle message in MessagePriorityActor

When receiving message specified with high_priority_atom or normal_priority_atom, MessagePriorityActor will delete the atom from the message. Users should create the message handler for the actor without the priority atom, just like any normal message handler.

// your actor class should derive from MessagePriorityActor
class CalculatorWithPriority : public MessagePriorityActor {
 public:
  explicit CalculatorWithPriority(caf::actor_config& cfg)
      : MessagePriorityActor(cfg) {}
  caf::behavior make_behavior() override {
    // create handler without the priority atom
    return {[=](int a, int b) -> int {
            return a + b;
    }
  };
};