Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reference loop when using fsm.Next() #2334

Closed
matt2e opened this issue Aug 13, 2024 · 1 comment · Fixed by #1723
Closed

Reference loop when using fsm.Next() #2334

matt2e opened this issue Aug 13, 2024 · 1 comment · Fixed by #1723
Assignees

Comments

@matt2e
Copy link
Collaborator

matt2e commented Aug 13, 2024

Problem:

Not sure the best way to get around this, while also being obvious from a dx point of view.

Here's some options:

Option 1: Wrap it in a function:

func fsm() *ftl.FSMHandle {
	//ftl:retry 2 1s
	var fsm = ftl.FSM("fsm",
		ftl.Start(StateA),
		ftl.Transition(StateA, StateB),
        ...
	)
	return fsm
}

Positives:

  • works out of the box without schema extraction changes

Negatives:

  • hard to remember to use var rather than := (:= does not support having attached Docs out of the box)
    non obvious whether retry directive goes on function or variable (no warning/error if they get it wrong)

Option 2: Allow separate var declaration and assignment

//ftl:retry 2 1s
var fsm *ftl.FSMHandle

func init() {
	fsm = ftl.FSM("fsm",
		ftl.Start(StateA),
		ftl.Transition(StateA, StateB),
        ...
    )
}

Negatives:

  • retry directive is now separated from the rest of the definition of the FSM
@matt2e
Copy link
Collaborator Author

matt2e commented Aug 13, 2024

Going with this:
ftl.FSMNext(ctx, event)

@matt2e matt2e self-assigned this Aug 13, 2024
@github-actions github-actions bot removed the triage Issue needs triaging label Aug 13, 2024
github-merge-queue bot pushed a commit that referenced this issue Aug 15, 2024
closes #1664
closes #2334

This is particularly useful when an FSM state itself knows which state
it should be in next, for example transitioning a payment to "voided" if
an error occurs communicating with an external vendor.

- Adds `fsm.Next(ctx, event)` to the go runtime
    - Only one next transition is allowed per fsm instance
- When a transition completes, the next transition is queued up if it
exists
- When a transition returns an error, we wipe the next transition so
that the retry attempt can set the next transition again

In another PR we will move away from using `ftl.Next()` to avoid the
reference cycle issue.

---------

Co-authored-by: Matt Toohey <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant