Skip to content

Commit

Permalink
Merge pull request #805 from gobuffalo/fix-799-with-cleanup
Browse files Browse the repository at this point in the history
prevent unnecessary go routines by limiting them when only the model is `AfterFindable`
  • Loading branch information
sio4 committed Jan 18, 2023
2 parents e2fe45b + 89774fa commit 44cf250
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions callbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ type AfterEagerFindable interface {
}

func (m *Model) afterFind(c *Connection, eager bool) error {
if x, ok := m.Value.(AfterFindable); ok && !eager {
if err := x.AfterFind(c); err != nil {
return err
if eager {
if x, ok := m.Value.(AfterEagerFindable); ok {
if err := x.AfterEagerFind(c); err != nil {
return err
}
}
}
if x, ok := m.Value.(AfterEagerFindable); ok && eager {
if err := x.AfterEagerFind(c); err != nil {
return err
} else {
if x, ok := m.Value.(AfterFindable); ok {
if err := x.AfterFind(c); err != nil {
return err
}
}
}

Expand All @@ -42,20 +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 x, ok := y.Interface().(AfterFindable); ok && !eager {
return x.AfterFind(c)
}
elem := rv.Index(i).Addr()

if x, ok := y.Interface().(AfterEagerFindable); ok && eager {
if eager {
if x, ok := elem.Interface().(AfterEagerFindable); ok {
wg.Go(func() error {
return x.AfterEagerFind(c)
}
return nil
})
}(i)
})
}
} 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 44cf250

Please sign in to comment.