Skip to content

Commit

Permalink
feat: add new func async.Go and Stringify (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
qloog authored Oct 29, 2024
1 parent 90bf901 commit 6d3ec92
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
24 changes: 24 additions & 0 deletions pkg/async/async.go
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()
}()
}
4 changes: 2 additions & 2 deletions pkg/utils/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"runtime"
)

// PrintStackTrace print stack info
func PrintStackTrace(msg string, err interface{}) string {
// StackTrace return stack info
func StackTrace(msg string, err interface{}) string {
buf := make([]byte, 64*1024)
buf = buf[:runtime.Stack(buf, false)]
return fmt.Sprintf("%s, err: %s\nstack: %s", msg, err, buf)
Expand Down
4 changes: 2 additions & 2 deletions pkg/utils/debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import (

func TestPrintStackTrace(t *testing.T) {
t.Run("mock a error", func(t *testing.T) {
err := PrintStackTrace("mock a error", errors.New("throw a error"))
err := StackTrace("mock a error", errors.New("throw a error"))
t.Log(err)
})

t.Run("mock a recover", func(t *testing.T) {
defer func() {
if r := recover(); r != nil {
err := PrintStackTrace("mock a recover", r)
err := StackTrace("mock a recover", r)
t.Log(err)
}
}()
Expand Down
16 changes: 16 additions & 0 deletions pkg/utils/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"strconv"
"strings"
"unsafe"

jsoniter "github.com/json-iterator/go"
)

// IsEmpty 是否是空字符串
Expand Down Expand Up @@ -94,3 +96,17 @@ func StringToBytes(s string) []byte {
h := [3]uintptr{x[0], x[1], x[1]}
return *(*[]byte)(unsafe.Pointer(&h))
}

// 对于序列化和反序列化场景较多的服务可以使用性能更高的json-iterator
// https://github.com/json-iterator/go
var Json = jsoniter.Config{
EscapeHTML: true,
SortMapKeys: true,
ValidateJsonRawMessage: true,
UseNumber: true,
}.Froze()

func Stringify(obj interface{}) string {
b, _ := Json.MarshalToString(obj)
return b
}

0 comments on commit 6d3ec92

Please sign in to comment.