-
Notifications
You must be signed in to change notification settings - Fork 239
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new func async.Go and Stringify
- Loading branch information
Showing
4 changed files
with
44 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package async | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/go-eagle/eagle/pkg/log" | ||
"github.com/go-eagle/eagle/pkg/utils" | ||
) | ||
|
||
// Go 异步执行 asyncFunc() 函数,会进行 recover() 操作,如果出现 panic() 则会记录日志 | ||
// name 用于 recover 后的日志和 metrics 统计 | ||
func Go(ctx context.Context, name string, asyncFunc func()) { | ||
go func() { | ||
defer func() { | ||
if err := recover(); err != nil { | ||
stack := utils.StackTrace(name, err) | ||
log.WithContext(ctx).Errorf("async: name: %s panic: %s stack: %s", name, err, stack) | ||
// TODO: metrics | ||
} | ||
}() | ||
|
||
asyncFunc() | ||
}() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters