Skip to content

Commit

Permalink
Fix concurrency problem in afterFind
Browse files Browse the repository at this point in the history
  • Loading branch information
breml committed May 3, 2021
1 parent 5a8f51c commit 8467227
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions callbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package pop

import (
"reflect"

"golang.org/x/sync/errgroup"
)

// AfterFindable callback will be called after a record, or records,
Expand All @@ -28,21 +26,18 @@ func (m *Model) afterFind(c *Connection) error {
return nil
}

wg := &errgroup.Group{}
for i := 0; i < rv.Len(); i++ {
func(i int) {
wg.Go(func() error {
y := rv.Index(i)
y = y.Addr()
if x, ok := y.Interface().(AfterFindable); ok {
return x.AfterFind(c)
}
return nil
})
}(i)
y := rv.Index(i)
y = y.Addr()
x, ok := y.Interface().(AfterFindable)
if !ok {
continue
}
if err := x.AfterFind(c); err != nil {
return err
}
}

return wg.Wait()
return nil
}

// BeforeSaveable callback will be called before a record is
Expand Down

0 comments on commit 8467227

Please sign in to comment.