fsm icon indicating copy to clipboard operation
fsm copied to clipboard

Proper way to call Events

Open ferllings opened this issue 7 years ago • 5 comments

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

ferllings avatar Nov 28 '18 16:11 ferllings

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.

ferllings avatar Nov 30 '18 15:11 ferllings

Can you provide a short code example?

maxekman avatar Dec 03 '18 07:12 maxekman

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")
}
        

ferllings avatar Dec 10 '18 08:12 ferllings

Hello,

Any idea?

I'm thinking of 2 modifications:

  1. Have an extra handler AFTER the state has been set and unlocked, so we can trigger new events from there

  2. have a queue of events? so when an async event arrives it doesn't end up on the mutex.

ferllings avatar Dec 18 '18 08:12 ferllings

Any update on this?

andersfylling avatar Nov 28 '21 05:11 andersfylling