Effects - Horusiath/Akkling GitHub Wiki

One of the major differences between Akkling and core Akka.FSharp APIs is concept of Effects. Effect a special descriptor, which may be returned from actor loop, that will cause it to perform some specific action. Currently Akkling core library defines following set of effects:

  • Unhandled will cause message received in current recursive iteration to be marked as unhandled. By default unhandled messages will be send to dead letters event stream.
  • Stop will cause current actor to immediately stop. It will send Terminated message to all monitoring actors. All messages waiting in actor mailbox or stash will be lost.
  • Ignore it's an equivalent of empty action.
  • Become triggers actor's behavior to change - it's an equivalent of return! call from actor computation expression.

Effects can be combined using @ operator. In such case they will be applied in order, they were defined.

Example:

let helloRef = spawn system "hello-actor" <| props(fun m ->
    let rec loop () = actor {
        let! msg = m.Receive ()
        match msg with
        | "stop" -> return Stop
        | "unhandle" -> return Unhandled
        | x -> 
            printfn "%s" x
            return! loop ()
    }
    loop ())

Composing actor behaviors

Akkling provides some additional operators allowing to compose different message receivers:

  • <|> executes right side always when left side was unhandled.
  • <&> executes right side only when left side was handled.