Skip to content

Commit

Permalink
feat: server 新增 DeadlockDetectEvent,以便于发生疑似死锁时刻能够执行通知等行为
Browse files Browse the repository at this point in the history
  • Loading branch information
kercylan98 committed Dec 9, 2023
1 parent ad60e79 commit b4ade2c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
24 changes: 24 additions & 0 deletions server/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type ShuntChannelClosedEventHandler func(srv *Server, guid int64)
type ConnectionPacketPreprocessEventHandler func(srv *Server, conn *Conn, packet []byte, abort func(), usePacket func(newPacket []byte))
type MessageExecBeforeEventHandler func(srv *Server, message *Message) bool
type MessageReadyEventHandler func(srv *Server)
type OnDeadlockDetectEventHandler func(srv *Server, message *Message)

func newEvent(srv *Server) *event {
return &event{
Expand All @@ -50,6 +51,7 @@ func newEvent(srv *Server) *event {
connectionPacketPreprocessEventHandlers: slice.NewPriority[ConnectionPacketPreprocessEventHandler](),
messageExecBeforeEventHandlers: slice.NewPriority[MessageExecBeforeEventHandler](),
messageReadyEventHandlers: slice.NewPriority[MessageReadyEventHandler](),
dedeadlockDetectEventHandlers: slice.NewPriority[OnDeadlockDetectEventHandler](),
}
}

Expand All @@ -70,6 +72,7 @@ type event struct {
connectionPacketPreprocessEventHandlers *slice.Priority[ConnectionPacketPreprocessEventHandler]
messageExecBeforeEventHandlers *slice.Priority[MessageExecBeforeEventHandler]
messageReadyEventHandlers *slice.Priority[MessageReadyEventHandler]
dedeadlockDetectEventHandlers *slice.Priority[OnDeadlockDetectEventHandler]

consoleCommandEventHandlers map[string]*slice.Priority[ConsoleCommandEventHandler]
consoleCommandEventHandlerInitOnce sync.Once
Expand Down Expand Up @@ -435,6 +438,27 @@ func (slf *event) OnMessageReadyEvent() {
})
}

// RegDeadlockDetectEvent 在死锁检测触发时立即执行被注册的事件处理函数
func (slf *event) RegDeadlockDetectEvent(handler OnDeadlockDetectEventHandler, priority ...int) {
slf.dedeadlockDetectEventHandlers.Append(handler, slice.GetValue(priority, 0))
}

func (slf *event) OnDeadlockDetectEvent(message *Message) {
if slf.dedeadlockDetectEventHandlers.Len() == 0 {
return
}
defer func() {
if err := recover(); err != nil {
log.Error("Server", log.String("OnDeadlockDetectEvent", fmt.Sprintf("%v", err)))
debug.PrintStack()
}
}()
slf.dedeadlockDetectEventHandlers.RangeValue(func(index int, value OnDeadlockDetectEventHandler) bool {
value(slf.Server, message)
return true
})
}

func (slf *event) check() {
switch slf.network {
case NetworkHttp, NetworkGRPC, NetworkNone:
Expand Down
1 change: 1 addition & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,7 @@ func (slf *Server) dispatchMessage(dispatcher *dispatcher, msg *Message) {
case <-ctx.Done():
if err := ctx.Err(); err == context.DeadlineExceeded {
log.Warn("Server", log.String("MessageType", messageNames[msg.t]), log.String("Info", msg.String()), log.Any("SuspectedDeadlock", msg))
slf.OnDeadlockDetectEvent(msg)
}
}
}(ctx, msg)
Expand Down

0 comments on commit b4ade2c

Please sign in to comment.