Skip to content

Commit

Permalink
Fix race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
asiragusa committed Jan 31, 2020
1 parent 57eb120 commit d101374
Showing 1 changed file with 6 additions and 19 deletions.
25 changes: 6 additions & 19 deletions fsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,29 +87,16 @@ func (r Ruleset) Permitted(start *State, goal *State) error {
attempt := T{start.ID(), goal.ID()}

if guards, ok := r[attempt]; ok {
outcome := make(chan error)

for _, guard := range guards {
go func(g Guard) {
err := g(start, goal)
start.id = start.ID()
goal.id = goal.ID()
outcome <- err
}(guard)
}
// spew.Dump(start, goal)
// fmt.Println("---")

for range guards {
select {
case err := <-outcome:
if err != nil {
return fmt.Errorf(errGuardFailedFormat,
start.ID(), goal.ID(), err.Error())
}
err := guard(start, goal)
if err != nil {
return fmt.Errorf(errGuardFailedFormat, start.ID(), goal.ID(), err.Error())
}
}

start.id = start.ID()
goal.id = goal.ID()
}
return nil
}
return fmt.Errorf(errNoRulesFormat, start.ID(), goal.ID())
Expand Down

0 comments on commit d101374

Please sign in to comment.