fsm
fsm copied to clipboard
Proper way to call Events
Sorry if this is a stupid question. Related To #37
Why can't we fire events in Callback? where are we supposed to call Event Transition?
I'm trying to write a protocol lib, that reacts based on the server response. So from the callback I'm sending messages to the server, and based on the response I fire a specific event.
Thanks for your help
Any idea? For now I'm lunching my event callbacks calls in a goroutine. It seems to do the trick, but can fail in race condition.
Can you provide a short code example?
I'm trying to convert a nodejs lib protocol based on machina. I believe my problem comes from the fact that many of my events are async.
Here is an eluded of the code:
// Launch a message listener
go messagesListener()
// Starts connection protocol
FSM.Event("requestConnection")
fsm.NewFSM(
"uninitialized",
fsm.Events{
{Name: "requestConnection", Src: []string{"uninitialized"}, Dst: "connecting"},
{Name: "confirmConnection", Src: []string{"connecting"}, Dst: "connected"},
{Name: "deniedConnection", Src: []string{"connecting"}, Dst: "uninitialized"},
{Name: "inbound_CONNECT_RESPONSE", Src: []string{"connecting"}, Dst: "connecting"},
...
},
fsm.Callbacks{
"enter_connecting": func(e *fsm.Event) {
...
// try to connect 3 times
// After 3 times with no response go back on "uninitialized"
FSM.Event("deniedConnection")
...
},
"inbound_CONNECT_RESPONSE": func(e *fsm.Event) {
...
// If the response code is ok we move to "connected"
FSM.Event("confirmConnection")
// If the target is busy we wait for 1 min before retrying
}
}
)
func messagesListener() {
// if connection response received
FSM.Event("inbound_CONNECT_RESPONSE")
}
Hello,
Any idea?
I'm thinking of 2 modifications:
-
Have an extra handler AFTER the state has been set and unlocked, so we can trigger new events from there
-
have a queue of events? so when an async event arrives it doesn't end up on the mutex.
Any update on this?