Skip to content

Commit

Permalink
other: v2 消息 recover
Browse files Browse the repository at this point in the history
  • Loading branch information
kercylan98 committed May 2, 2024
1 parent 7121b8b commit 26e78d6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
18 changes: 15 additions & 3 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ func NewServer(network Network, options ...*Options) Server {
srv.Options.init(srv).Apply(options...)
srv.broker = brokers.NewSparseGoroutine(srv.Options.GetSparseGoroutineNum(), func(index int) nexus.Queue[int, string] {
return queues.NewNonBlockingRW[int, string](index, srv.Options.GetServerMessageChannelSize(), srv.Options.GetServerMessageBufferInitialSize())
}, func(handler nexus.EventExecutor) {
handler()
})
}, srv.onEventProcess)
antsPool, err := ants.NewPool(ants.DefaultAntsPoolSize, ants.WithOptions(ants.Options{
ExpiryDuration: 10 * time.Second,
Nonblocking: true,
Expand Down Expand Up @@ -172,3 +170,17 @@ func (s *server) PublishSystemAsyncMessage(handler messageEvents.AsynchronousHan
func (s *server) GetStatus() *State {
return s.state.Status()
}

func (s *server) onEventProcess(handler nexus.EventExecutor) {
defer func(s *server) {
if v := recover(); v != nil {
switch err := v.(type) {
case error:
s.GetLogger().Error("onEventProcess", log.Err(err), log.Stack("stack"))
default:
s.GetLogger().Error("onEventProcess", log.Any("recover", v), log.Stack("stack"))
}
}
}(s)
handler()
}
4 changes: 2 additions & 2 deletions toolkit/log/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package log

import (
"github.com/kercylan98/minotaur/toolkit/constraints"
"github.com/pkg/errors"
"log/slog"
"runtime/debug"
"time"
)

Expand Down Expand Up @@ -246,7 +246,7 @@ func Group(key string, args ...any) Field {

// Stack 返回堆栈字段
func Stack(key string) Field {
return slog.Any(key, errors.New(""))
return slog.String(key, string(debug.Stack()))
}

// Err 构造一个带有错误值的字段
Expand Down

0 comments on commit 26e78d6

Please sign in to comment.