mirror of
https://github.com/gogf/gf.git
synced 2025-04-05 03:05:05 +08:00
add context parameter (#1919)
This commit is contained in:
parent
3ae23df4b3
commit
52056644d4
@ -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.
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
})
|
||||
}
|
||||
|
@ -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))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -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.
|
||||
|
@ -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) {
|
||||
|
||||
})
|
||||
}
|
||||
|
@ -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")
|
||||
})
|
||||
})
|
||||
|
Loading…
x
Reference in New Issue
Block a user