[][src]Trait riker::actor::Actor

pub trait Actor: Send {
    type Msg: Message;
    fn receive(
        &mut self,
        ctx: &Context<Self::Msg>,
        msg: Self::Msg,
        sender: Option<ActorRef<Self::Msg>>
    ); fn pre_start(&mut self, ctx: &Context<Self::Msg>) { ... }
fn post_start(&mut self, ctx: &Context<Self::Msg>) { ... }
fn post_stop(&mut self) { ... }
fn other_receive(
        &mut self,
        ctx: &Context<Self::Msg>,
        msg: ActorMsg<Self::Msg>,
        sender: Option<ActorRef<Self::Msg>>
    ) { ... }
fn system_receive(
        &mut self,
        ctx: &Context<Self::Msg>,
        msg: SystemMsg<Self::Msg>,
        sender: Option<ActorRef<Self::Msg>>
    ) { ... }
fn persistence_conf(&self) -> Option<PersistenceConf> { ... }
fn apply_event(&mut self, ctx: &Context<Self::Msg>, evt: Self::Msg) { ... }
fn replay_event(&mut self, ctx: &Context<Self::Msg>, evt: Self::Msg) { ... }
fn supervisor_strategy(&self) -> Strategy { ... } }

An Actor represents a struct that will be scheduled for execution when it is sent a message.

Actors expose an API through messaging. The only means to interact with an actor is through the messaging protocol via the actor's reference, an ActorRef.

When ActorRef.tell is used to send a message to an actor, the message is placed in the actor's mailbox and the actor is scheduled for execution.

When the actor is executed the receive function is invoked for each message.

Associated Types

type Msg: Message

Loading content...

Required methods

fn receive(
    &mut self,
    ctx: &Context<Self::Msg>,
    msg: Self::Msg,
    sender: Option<ActorRef<Self::Msg>>
)

Invoked when an actor receives a message

It is guaranteed that only one message in the actor's mailbox is processed at any one time, including receive, other_receive and system_receive.

Loading content...

Provided methods

fn pre_start(&mut self, ctx: &Context<Self::Msg>)

Invoked when an actor is being started by the system.

Any initialization inherent to the actor's role should be performed here.

Panics in pre_start do not invoke the supervision strategy and the actor will be terminated.

fn post_start(&mut self, ctx: &Context<Self::Msg>)

Invoked after an actor has started.

Any post initialization can be performed here, such as writing to a log file, emmitting metrics.

Panics in post_start follow the supervision strategy.

fn post_stop(&mut self)

Invoked after an actor has been stopped.

fn other_receive(
    &mut self,
    ctx: &Context<Self::Msg>,
    msg: ActorMsg<Self::Msg>,
    sender: Option<ActorRef<Self::Msg>>
)

Invoked when an actor receives a Riker predefined message

It is guaranteed that only one message in the actor's mailbox is processed at any one time, including receive, other_receive and system_receive.

fn system_receive(
    &mut self,
    ctx: &Context<Self::Msg>,
    msg: SystemMsg<Self::Msg>,
    sender: Option<ActorRef<Self::Msg>>
)

Invoked when an actor receives a Riker system message

It is guaranteed that only one message in the actor's mailbox is processed at any one time, including receive, other_receive and system_receive.

fn persistence_conf(&self) -> Option<PersistenceConf>

Return a Some(PersistenceConf) to enable actor persistence.

Examples

struct User {
    id: String,
}
 
impl Actor for User {
    type Msg = String;
 
    fn persistence_conf(&self) -> Option<PersistenceConf> {
        Some(PersistenceConf {
            id: self.id.clone(),
            keyspace: "user".into()
        })
    }
 
}

Events persisted using ctx.persist_event() will be replayed in order by passing to apply_event when an actor is started, or restarted by a supervisor

fn apply_event(&mut self, ctx: &Context<Self::Msg>, evt: Self::Msg)

Invoked after an event is successfully inserted into the event store.

State changes are safe to make here.

Since you should only change state (e.g. self.some_val) when you know the event has been successfully stored in the event store, apply_event is the only place that this is guaranteed after ctx.persist_event().

ctx.persist_event() stops further processing of the actor's mailbox messages until the event is successfully inserted in to the event store. Thus you are guaranteed that apply_event is invoked before the next message is received.

Examples

 
struct Sensor {
    id: String,
    last: u32,
    count: u32,
    avg: f64,
}
 
impl Actor for Sensor {
    type Msg = u32;
 
    fn persistence_conf(&self) -> Option<PersistenceConf> {
        Some(PersistenceConf {
            id: self.id.clone(),
            keyspace: "sensor_1".into(),
        })
    }
 
    fn receive(&mut self,
                ctx: &Context<Self::Msg>,
                msg: Self::Msg,
                sender: Option<ActorRef<Self::Msg>>) {
        // Receive a new sensor reading and store it
        ctx.persist_event(msg);
    }
 
    fn apply_event(&mut self, _ctx: &Context<Self::Msg>, evt: Self::Msg) {
        // Sensor reading has been stored
        // Local state can be updated
        self.last = evt;
        self.count += 1;
        self.avg = (self.last / self.count).into();
    }
}

fn replay_event(&mut self, ctx: &Context<Self::Msg>, evt: Self::Msg)

Invoked for each event when the actor is recovering.

State changes are safe to make here.

Since you should only change state (e.g. self.some_val) when you know the event exists in the event store, replay_event is safe to change the state.

replay_event is used instead of apply_event when recovering to allow for different bahavior. Typically replaying should only be used to change state for the purpose of recovering the actor's state and not perform additional messaging.

Examples

struct Sensor {
    id: String,
    last: u32,
    count: u32,
    avg: f64,
}
 
impl Actor for Sensor {
    type Msg = u32;
 
    fn persistence_conf(&self) -> Option<PersistenceConf> {
        Some(PersistenceConf {
            id: self.id.clone(),
            keyspace: "sensor_1".into(),
        })
    }
 
    fn receive(&mut self,
                ctx: &Context<Self::Msg>,
                msg: Self::Msg,
                sender: Option<ActorRef<Self::Msg>>) {
        // Receive a new sensor reading and store it
        ctx.persist_event(msg);
    }

    fn replay_event(&mut self, _ctx: &Context<Self::Msg>, evt: Self::Msg) {
        // Received a previously stored sensor reading
        // Update local state
        self.last = evt;
        self.count += 1;
        self.avg = (self.last / self.count).into();
    }
}

fn supervisor_strategy(&self) -> Strategy

Return a supervisor strategy that will be used when handling failed child actors.

Loading content...

Implementations on Foreign Types

impl<A: Actor + ?Sized> Actor for Box<A>
[src]

type Msg = A::Msg

Loading content...

Implementors

impl<Evs: EventStore> Actor for EsManager<Evs>
[src]

type Msg = Evs::Msg

fn pre_start(&mut self, ctx: &Context<Self::Msg>)
[src]

fn post_start(&mut self, ctx: &Context<Self::Msg>)
[src]

fn post_stop(&mut self)
[src]

fn system_receive(
    &mut self,
    ctx: &Context<Self::Msg>,
    msg: SystemMsg<Self::Msg>,
    sender: Option<ActorRef<Self::Msg>>
)
[src]

fn persistence_conf(&self) -> Option<PersistenceConf>
[src]

fn apply_event(&mut self, ctx: &Context<Self::Msg>, evt: Self::Msg)
[src]

fn replay_event(&mut self, ctx: &Context<Self::Msg>, evt: Self::Msg)
[src]

fn supervisor_strategy(&self) -> Strategy
[src]

impl<Msg> Actor for Io<Msg> where
    Msg: Message
[src]

type Msg = Msg

fn pre_start(&mut self, ctx: &Context<Self::Msg>)
[src]

fn post_start(&mut self, ctx: &Context<Self::Msg>)
[src]

fn post_stop(&mut self)
[src]

fn system_receive(
    &mut self,
    ctx: &Context<Self::Msg>,
    msg: SystemMsg<Self::Msg>,
    sender: Option<ActorRef<Self::Msg>>
)
[src]

fn persistence_conf(&self) -> Option<PersistenceConf>
[src]

fn apply_event(&mut self, ctx: &Context<Self::Msg>, evt: Self::Msg)
[src]

fn replay_event(&mut self, ctx: &Context<Self::Msg>, evt: Self::Msg)
[src]

fn supervisor_strategy(&self) -> Strategy
[src]

impl<Msg: Message> Actor for Channel<Msg>
[src]

type Msg = Msg

fn post_start(&mut self, ctx: &Context<Self::Msg>)
[src]

fn post_stop(&mut self)
[src]

fn persistence_conf(&self) -> Option<PersistenceConf>
[src]

fn apply_event(&mut self, ctx: &Context<Self::Msg>, evt: Self::Msg)
[src]

fn replay_event(&mut self, ctx: &Context<Self::Msg>, evt: Self::Msg)
[src]

fn supervisor_strategy(&self) -> Strategy
[src]

impl<Msg: Message> Actor for SystemChannel<Msg>
[src]

type Msg = Msg

fn post_start(&mut self, ctx: &Context<Self::Msg>)
[src]

fn post_stop(&mut self)
[src]

fn persistence_conf(&self) -> Option<PersistenceConf>
[src]

fn apply_event(&mut self, ctx: &Context<Self::Msg>, evt: Self::Msg)
[src]

fn replay_event(&mut self, ctx: &Context<Self::Msg>, evt: Self::Msg)
[src]

fn supervisor_strategy(&self) -> Strategy
[src]

Loading content...