Skip to content

Commit

Permalink
limit invoking go routines when only the model is AfterFindable (fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sio4 committed Jan 13, 2023
1 parent 1082c83 commit 89774fa
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions callbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,21 @@ func (m *Model) afterFind(c *Connection, eager bool) error {

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 eager {
if x, ok := y.Interface().(AfterEagerFindable); ok {
return x.AfterEagerFind(c)
}
} else {
if x, ok := y.Interface().(AfterFindable); ok {
return x.AfterFind(c)
}
}

return nil
})
}(i)
elem := rv.Index(i).Addr()

if eager {
if x, ok := elem.Interface().(AfterEagerFindable); ok {
wg.Go(func() error {
return x.AfterEagerFind(c)
})
}
} else {
if x, ok := elem.Interface().(AfterFindable); ok {
wg.Go(func() error {
return x.AfterFind(c)
})
}
}
}

return wg.Wait()
Expand Down

0 comments on commit 89774fa

Please sign in to comment.