Search code examples
f#actorakka.net

How to store state in an F# Akka.NET Actor?


In C# ReceiveActors I can just have state as private fields in the class. How should I do this in an idiomatic way with the F# API?

Is this a good idea? Any alternatives?

let handleMessage (mailbox: Actor<'a>) msg =
    let mutable i = 1
    match msg with
    | Some x -> i <- i + x
    | None -> ()

Solution

  • The way you've proposed is entirely appropriate as a means of storing the state within the actor. The concurrency constraints of only processing 1 message at any time means that it's not possible to get into invalid states as a result of contention on a shared memory location.

    However, it's not the most idiomatic option. Akka.Net provides an F# API to work with actors in a similar way to F# MailboxProcessors. In this case you define your actor as a tail recursive function which calls itself with some new state. Here's an example

    spawn system "hello" <|
        fun mailbox ->
            let rec loop state =
                actor {
                    let! msg = mailbox.Receive ()
                    printfn "Received %A. Now received %s messages" msg state
                    return! loop (state + 1) //Increment a counter for the number of times the actor has received a message
                }
            loop 0
    

    For full documentation on the Akka.Net F# API see http://getakka.net/wiki/FSharp%20API