Add RunSafe with context (#3224)
Co-authored-by: yangtao <mrynag8614@163.com> Co-authored-by: Kevin Wan <wanjunfeng@gmail.com>
This commit is contained in:
@@ -2,6 +2,7 @@ package threading
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"runtime"
|
||||
"strconv"
|
||||
|
||||
@@ -13,6 +14,11 @@ func GoSafe(fn func()) {
|
||||
go RunSafe(fn)
|
||||
}
|
||||
|
||||
// GoSafeCtx runs the given fn using another goroutine, recovers if fn panics with ctx.
|
||||
func GoSafeCtx(ctx context.Context, fn func()) {
|
||||
go RunSafeCtx(ctx, fn)
|
||||
}
|
||||
|
||||
// RoutineId is only for debug, never use it in production.
|
||||
func RoutineId() uint64 {
|
||||
b := make([]byte, 64)
|
||||
@@ -31,3 +37,10 @@ func RunSafe(fn func()) {
|
||||
|
||||
fn()
|
||||
}
|
||||
|
||||
// RunSafeCtx runs the given fn, recovers if fn panics with ctx.
|
||||
func RunSafeCtx(ctx context.Context, fn func()) {
|
||||
defer rescue.RecoverCtx(ctx)
|
||||
|
||||
fn()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user