From 275597c0dc28d2eccc36d26bdfdf021968102952 Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Tue, 5 Nov 2024 11:53:03 +0000 Subject: [PATCH] style: format code with Gofumpt and Prettier This commit fixes the style issues introduced in 50cfcd3 according to the output from Gofumpt and Prettier. Details: https://github.com/vdaas/vald/pull/2733 Signed-off-by: kpango --- internal/cache/cache.go | 2 +- internal/cache/gache/gache.go | 2 +- internal/cache/gache/option.go | 2 +- internal/cache/option.go | 2 +- internal/core/algorithm/ngt/ngt_test.go | 2 +- internal/net/dialer.go | 2 +- internal/net/dialer_test.go | 5 +++-- internal/safety/safety.go | 9 +++++++-- 8 files changed, 16 insertions(+), 10 deletions(-) diff --git a/internal/cache/cache.go b/internal/cache/cache.go index 76edebf2344..741b8fd9cb3 100644 --- a/internal/cache/cache.go +++ b/internal/cache/cache.go @@ -30,7 +30,7 @@ type cache[V any] struct { cacher cacher.Type expireDur time.Duration expireCheckDur time.Duration - expiredHook func(context.Context, string) + expiredHook func(context.Context, string, V) } // New returns the Cache instance or error. diff --git a/internal/cache/gache/gache.go b/internal/cache/gache/gache.go index 246d70ae95b..6f56beba387 100644 --- a/internal/cache/gache/gache.go +++ b/internal/cache/gache/gache.go @@ -29,7 +29,7 @@ type cache[V any] struct { gache gache.Gache[V] expireDur time.Duration expireCheckDur time.Duration - expiredHook func(context.Context, string) + expiredHook func(context.Context, string, V) } // New loads a cache model and returns a new cache struct. diff --git a/internal/cache/gache/option.go b/internal/cache/gache/option.go index 7f3a617d1ee..d6022f9ba69 100644 --- a/internal/cache/gache/option.go +++ b/internal/cache/gache/option.go @@ -42,7 +42,7 @@ func WithGache[V any](g gache.Gache[V]) Option[V] { } // WithExpiredHook returns Option after set expiredHook when f is not nil. -func WithExpiredHook[V any](f func(context.Context, string)) Option[V] { +func WithExpiredHook[V any](f func(context.Context, string, V)) Option[V] { return func(c *cache[V]) { if f != nil { c.expiredHook = f diff --git a/internal/cache/option.go b/internal/cache/option.go index ef5a7583ae0..08966d60787 100644 --- a/internal/cache/option.go +++ b/internal/cache/option.go @@ -36,7 +36,7 @@ func defaultOptions[V any]() []Option[V] { } // WithExpiredHook returns Option after set expiredHook when f is not nil. -func WithExpiredHook[V any](f func(context.Context, string)) Option[V] { +func WithExpiredHook[V any](f func(context.Context, string, V)) Option[V] { return func(c *cache[V]) { if f != nil { c.expiredHook = f diff --git a/internal/core/algorithm/ngt/ngt_test.go b/internal/core/algorithm/ngt/ngt_test.go index 240e1f7b8ae..74b7c5d98c4 100644 --- a/internal/core/algorithm/ngt/ngt_test.go +++ b/internal/core/algorithm/ngt/ngt_test.go @@ -780,7 +780,7 @@ func Test_gen(t *testing.T) { objectType: Uint8, mu: &sync.RWMutex{}, cmu: &sync.RWMutex{}, - smu: &sync.Mutex{}, + smu: &sync.Mutex{}, epl: DefaultErrorBufferLimit, }, }, diff --git a/internal/net/dialer.go b/internal/net/dialer.go index c01ecc5927b..d7159bb40d2 100644 --- a/internal/net/dialer.go +++ b/internal/net/dialer.go @@ -454,7 +454,7 @@ func (d *dialer) tlsHandshake( return tconn, nil } -func (d *dialer) cacheExpireHook(ctx context.Context, addr string) { +func (d *dialer) cacheExpireHook(ctx context.Context, addr string, _ *dialerCache) { if err := safety.RecoverFunc(func() (err error) { _, err = d.lookup(ctx, addr) return diff --git a/internal/net/dialer_test.go b/internal/net/dialer_test.go index 22183ede9e8..9c6261aa0cc 100644 --- a/internal/net/dialer_test.go +++ b/internal/net/dialer_test.go @@ -1516,7 +1516,8 @@ func Test_dialer_dial(t *testing.T) { func Test_dialer_cacheExpireHook(t *testing.T) { t.Parallel() type args struct { - addr string + addr string + cache *dialerCache } type want struct{} type test struct { @@ -1585,7 +1586,7 @@ func Test_dialer_cacheExpireHook(t *testing.T) { test.beforeFunc(d) } - d.cacheExpireHook(ctx, test.args.addr) + d.cacheExpireHook(ctx, test.args.addr, test.args.cache) if err := checkFunc(d); err != nil { tt.Errorf("error = %v", err) } diff --git a/internal/safety/safety.go b/internal/safety/safety.go index f26ae577bbd..697f99e3de9 100644 --- a/internal/safety/safety.go +++ b/internal/safety/safety.go @@ -19,7 +19,9 @@ package safety import ( "runtime" + "runtime/debug" + "github.com/vdaas/vald/internal/conv" "github.com/vdaas/vald/internal/errors" "github.com/vdaas/vald/internal/info" "github.com/vdaas/vald/internal/log" @@ -35,16 +37,19 @@ func RecoverWithoutPanicFunc(fn func() error) func() error { func recoverFn(fn func() error, withPanic bool) func() error { return func() (err error) { + if fn == nil { + return nil + } defer func() { if r := recover(); r != nil { - infoStr := info.Get().String() + ds := debug.Stack() + infoStr := info.Get().String() + "\r\n" + conv.Btoa(ds) log.Warnf("function %#v panic recovered: %#v\ninfo:\n%s", fn, r, infoStr) switch x := r.(type) { case runtime.Error: err = errors.ErrRuntimeError(err, x) if withPanic { log.Errorf("recovered but this thread is going to panic: the reason is runtimer.Error\nerror:\t%v\ninfo:\n%s\nrecovered:\t%#v", err, infoStr, r) - panic(err) } case *string: