From 900a32c6ac6beede127ea604e62d1b9374595e0f Mon Sep 17 00:00:00 2001 From: Yonghwan SO Date: Fri, 13 Jan 2023 23:58:13 +0900 Subject: [PATCH] limit invoking go routines when only the model is AfterFindable (fixes #799) --- callbacks.go | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/callbacks.go b/callbacks.go index 7b443a3a..8173ebaf 100644 --- a/callbacks.go +++ b/callbacks.go @@ -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()