1 Star 0 Fork 0

sun / common_pkg

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
saferun.go 570 Bytes
一键复制 编辑 原始数据 按行查看 历史
yzsunjianguo 提交于 2023-09-19 21:35 . init
package utils
import (
"context"
"fmt"
"time"
)
// SafeRun safe run
func SafeRun(ctx context.Context, fn func(ctx context.Context)) {
defer func() {
if e := recover(); e != nil {
fmt.Println(e)
}
}()
fn(ctx)
}
// SafeRunWithTimeout safe run with limit timeouts
func SafeRunWithTimeout(d time.Duration, fn func(cancel context.CancelFunc)) {
ctx, cancel := context.WithTimeout(context.Background(), d)
go func() {
defer func() {
if e := recover(); e != nil {
fmt.Println(e)
}
}()
fn(cancel)
}()
for range ctx.Done() {
return
}
}
Go
1
https://gitee.com/jianguosun_admin/common_pkg.git
git@gitee.com:jianguosun_admin/common_pkg.git
jianguosun_admin
common_pkg
common_pkg
v1.0.4

搜索帮助