diff --git a/frame/g/g_func.go b/frame/g/g_func.go index f4a169325..23e5ae7b9 100644 --- a/frame/g/g_func.go +++ b/frame/g/g_func.go @@ -63,14 +63,14 @@ func Throw(exception interface{}) { // Try implements try... logistics using internal panic...recover. // It returns error if any exception occurs, or else it returns nil. -func Try(try func()) (err error) { - return gutil.Try(try) +func Try(ctx context.Context, try func(ctx context.Context)) (err error) { + return gutil.Try(ctx, try) } // TryCatch implements try...catch... logistics using internal panic...recover. // It automatically calls function `catch` if any exception occurs ans passes the exception as an error. -func TryCatch(try func(), catch ...func(exception error)) { - gutil.TryCatch(try, catch...) +func TryCatch(ctx context.Context, try func(ctx context.Context), catch ...func(ctx context.Context, exception error)) { + gutil.TryCatch(ctx, try, catch...) } // IsNil checks whether given `value` is nil. diff --git a/net/ghttp/ghttp_request_middleware.go b/net/ghttp/ghttp_request_middleware.go index 0b5cf8454..4c4a9aa63 100644 --- a/net/ghttp/ghttp_request_middleware.go +++ b/net/ghttp/ghttp_request_middleware.go @@ -7,6 +7,7 @@ package ghttp import ( + "context" "net/http" "reflect" @@ -44,8 +45,8 @@ func (m *middleware) Next() { // Router values switching. m.request.routerMap = item.Values - - gutil.TryCatch(func() { + ctx := m.request.context + gutil.TryCatch(ctx, func(ctx context.Context) { // Execute bound middleware array of the item if it's not empty. if m.handlerMDIndex < len(item.Handler.Middleware) { md := item.Handler.Middleware[m.handlerMDIndex] @@ -98,7 +99,7 @@ func (m *middleware) Next() { // There should be a "Next" function to be called in the middleware in order to manage the workflow. loop = false } - }, func(exception error) { + }, func(ctx context.Context, exception error) { if v, ok := exception.(error); ok && gerror.HasStack(v) { // It's already an error that has stack info. m.request.error = v diff --git a/net/gsvc/gsvc_discovery.go b/net/gsvc/gsvc_discovery.go index 518b87c03..ff7fdc96f 100644 --- a/net/gsvc/gsvc_discovery.go +++ b/net/gsvc/gsvc_discovery.go @@ -81,9 +81,9 @@ func watchAndUpdateService(watcher Watcher, service Service, watchFunc ServiceWa if len(services) > 0 { watchedServiceMap.Set(service.GetName(), services[0]) if watchFunc != nil { - gutil.TryCatch(func() { + gutil.TryCatch(ctx, func(ctx context.Context) { watchFunc(services[0]) - }, func(exception error) { + }, func(ctx context.Context, exception error) { intlog.Errorf(ctx, `%+v`, exception) }) } diff --git a/os/grpool/grpool.go b/os/grpool/grpool.go index ba9c6e46d..5d0350711 100644 --- a/os/grpool/grpool.go +++ b/os/grpool/grpool.go @@ -19,6 +19,9 @@ import ( // Func is the pool function which contains context parameter. type Func func(ctx context.Context) +// RecoverFunc is the pool runtime panic recover function which contains context parameter. +type RecoverFunc func(ctx context.Context, err error) + // Pool manages the goroutines using pool. type Pool struct { limit int // Max goroutine count limit. @@ -63,7 +66,7 @@ func Add(ctx context.Context, f Func) error { // The optional `recoverFunc` is called when any panic during executing of `userFunc`. // If `recoverFunc` is not passed or given nil, it ignores the panic from `userFunc`. // The job will be executed asynchronously. -func AddWithRecover(ctx context.Context, userFunc Func, recoverFunc ...func(err error)) error { +func AddWithRecover(ctx context.Context, userFunc Func, recoverFunc ...RecoverFunc) error { return pool.AddWithRecover(ctx, userFunc, recoverFunc...) } @@ -108,15 +111,15 @@ func (p *Pool) Add(ctx context.Context, f Func) error { // The optional `recoverFunc` is called when any panic during executing of `userFunc`. // If `recoverFunc` is not passed or given nil, it ignores the panic from `userFunc`. // The job will be executed asynchronously. -func (p *Pool) AddWithRecover(ctx context.Context, userFunc Func, recoverFunc ...func(err error)) error { +func (p *Pool) AddWithRecover(ctx context.Context, userFunc Func, recoverFunc ...RecoverFunc) error { return p.Add(ctx, func(ctx context.Context) { defer func() { if exception := recover(); exception != nil { if len(recoverFunc) > 0 && recoverFunc[0] != nil { if v, ok := exception.(error); ok && gerror.HasStack(v) { - recoverFunc[0](v) + recoverFunc[0](ctx, v) } else { - recoverFunc[0](gerror.Newf(`%+v`, exception)) + recoverFunc[0](ctx, gerror.Newf(`%+v`, exception)) } } } diff --git a/os/grpool/grpool_z_unit_test.go b/os/grpool/grpool_z_unit_test.go index 00568b35f..ceb0b9225 100644 --- a/os/grpool/grpool_z_unit_test.go +++ b/os/grpool/grpool_z_unit_test.go @@ -115,7 +115,7 @@ func Test_AddWithRecover(t *testing.T) { grpool.AddWithRecover(ctx, func(ctx context.Context) { array.Append(1) panic(1) - }, func(err error) { + }, func(ctx context.Context, err error) { array.Append(1) }) grpool.AddWithRecover(ctx, func(ctx context.Context) { diff --git a/util/gutil/gutil.go b/util/gutil/gutil.go index c140c2e5b..c8fbcfc0a 100644 --- a/util/gutil/gutil.go +++ b/util/gutil/gutil.go @@ -8,6 +8,7 @@ package gutil import ( + "context" "reflect" "github.com/gogf/gf/v2/errors/gerror" @@ -26,7 +27,7 @@ func Throw(exception interface{}) { // Try implements try... logistics using internal panic...recover. // It returns error if any exception occurs, or else it returns nil. -func Try(try func()) (err error) { +func Try(ctx context.Context, try func(ctx context.Context)) (err error) { defer func() { if exception := recover(); exception != nil { if v, ok := exception.(error); ok && gerror.HasStack(v) { @@ -36,23 +37,23 @@ func Try(try func()) (err error) { } } }() - try() + try(ctx) return } // TryCatch implements try...catch... logistics using internal panic...recover. // It automatically calls function `catch` if any exception occurs ans passes the exception as an error. -func TryCatch(try func(), catch ...func(exception error)) { +func TryCatch(ctx context.Context, try func(ctx context.Context), catch ...func(ctx context.Context, exception error)) { defer func() { if exception := recover(); exception != nil && len(catch) > 0 { if v, ok := exception.(error); ok && gerror.HasStack(v) { - catch[0](v) + catch[0](ctx, v) } else { - catch[0](gerror.Newf(`%+v`, exception)) + catch[0](ctx, gerror.Newf(`%+v`, exception)) } } }() - try() + try(ctx) } // IsEmpty checks given `value` empty or not. diff --git a/util/gutil/gutil_z_bench_test.go b/util/gutil/gutil_z_bench_test.go index 2920143ad..6ef31405f 100644 --- a/util/gutil/gutil_z_bench_test.go +++ b/util/gutil/gutil_z_bench_test.go @@ -9,6 +9,7 @@ package gutil import ( + "context" "testing" ) @@ -22,10 +23,11 @@ var ( ) func Benchmark_TryCatch(b *testing.B) { + ctx := context.TODO() for i := 0; i < b.N; i++ { - TryCatch(func() { + TryCatch(ctx, func(ctx context.Context) { - }, func(err error) { + }, func(ctx context.Context, err error) { }) } diff --git a/util/gutil/gutil_z_unit_test.go b/util/gutil/gutil_z_unit_test.go index fe69defc1..c47c63ffd 100755 --- a/util/gutil/gutil_z_unit_test.go +++ b/util/gutil/gutil_z_unit_test.go @@ -7,6 +7,7 @@ package gutil_test import ( + "context" "testing" "github.com/gogf/gf/v2/frame/g" @@ -14,10 +15,14 @@ import ( "github.com/gogf/gf/v2/util/gutil" ) +var ( + ctx = context.TODO() +) + func Test_Try(t *testing.T) { gtest.C(t, func(t *gtest.T) { s := `gutil Try test` - t.Assert(gutil.Try(func() { + t.Assert(gutil.Try(ctx, func(ctx context.Context) { panic(s) }), s) }) @@ -25,16 +30,16 @@ func Test_Try(t *testing.T) { func Test_TryCatch(t *testing.T) { gtest.C(t, func(t *gtest.T) { - gutil.TryCatch(func() { + gutil.TryCatch(ctx, func(ctx context.Context) { panic("gutil TryCatch test") }) }) gtest.C(t, func(t *gtest.T) { - gutil.TryCatch(func() { + gutil.TryCatch(ctx, func(ctx context.Context) { panic("gutil TryCatch test") - }, func(err error) { + }, func(ctx context.Context, err error) { t.Assert(err, "gutil TryCatch test") }) })