Skip to content

Commit

Permalink
Merge pull request #4 from processout/fix-race-condition-v2
Browse files Browse the repository at this point in the history
Fix race condition
  • Loading branch information
gee-m authored Jan 31, 2020
2 parents 57eb120 + d101374 commit c674cbd
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 c674cbd

Please sign in to comment.