Reputation: 1758
I have an Akka FSM actor that runs the following pseudocode after receiving a message while in ReadyState
lookupA ! Wrapper(Lookup("A"))
lookupB ! Wrapper(Lookup("B"))
lookupC ! Wrapper(Lookup("C"))
goto(LookingUpDataState) using DataFound(a = None, b = None, c = None)
The actor then waits for responses which can be either FullResult[T]
(extending ServiceResult[T]
) or Empty
(extending ServiceResult[Nothing]
). Successful lookup results are used to populate the DataFound instance's fields and Empty lookup results result in a logged error message and the termination of the actor.
My question is this: how can I determine which lookup failed, so that I can log the failure or fallback to a default value? All I can think of is examining the sender's ActorRef (hacky) or adding a unique ID field to all messages (high overhead).
This is a simple problem to solve using Ask's and Futures. Does an idiomatic Akka solution exist?
Upvotes: 3
Views: 1200
Reputation: 13140
There's a few patterns you could use here. You will have to resort to one of these options listed below. They all have some trade off (and benchmarking is king anyway), but I've listed them in order of "bad to good",
ask
pattern, so you'll spin up (internally that's how ask works) 3 actors, and they'll complete "their own" future. So this also has some cost on the sender because it has to start these special purpose actors (smaller than a normal actor, but still).My personal favourite (we tend to avoid ask
in general) would be tagging the requests in an Envelope(id, payload)
so the responses can also be wrapped in such Envelope(id, response)
. If you decide to call these simply envelope or more with domain terminology is up to you.
Hope this helps.
Upvotes: 5