Skip to content

Commit

Permalink
增加队列崩溃回调
Browse files Browse the repository at this point in the history
  • Loading branch information
davyxu committed May 5, 2019
1 parent 55f6ab6 commit b1d28cc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion peer/redix/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (self *redisConnector) tryConnect() {
}
}

log.Infof("Create redis pool connection: %s", addr)
log.Infof("Create redis pool connection: %s name: %s index: %d", addr, self.Name(), self.DBIndex)

return client, nil
})
Expand Down
19 changes: 17 additions & 2 deletions queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,28 @@ type EventQueue interface {
EnableCapturePanic(v bool)
}

type CapturePanicNotifyFunc func(interface{}, EventQueue)

type eventQueue struct {
*Pipe

endSignal sync.WaitGroup

capturePanic bool

onPanic CapturePanicNotifyFunc
}

// 启动崩溃捕获
func (self *eventQueue) EnableCapturePanic(v bool) {
self.capturePanic = v
}

// 设置捕获崩溃通知
func (self *eventQueue) SetCapturePanicNotify(callback CapturePanicNotifyFunc) {
self.onPanic = callback
}

// 派发事件处理回调到队列中
func (self *eventQueue) Post(callback func()) {

Expand All @@ -55,8 +64,7 @@ func (self *eventQueue) protectedCall(callback func()) {
defer func() {

if err := recover(); err != nil {
fmt.Println("[ERRO] panic captured!")
debug.PrintStack()
self.onPanic(err, self)
}

}()
Expand Down Expand Up @@ -117,6 +125,13 @@ func NewEventQueue() EventQueue {

return &eventQueue{
Pipe: NewPipe(),

// 默认的崩溃捕获打印
onPanic: func(raw interface{}, queue EventQueue) {

fmt.Printf("%v \n%s\n", raw, string(debug.Stack()))
debug.PrintStack()
},
}
}

Expand Down

0 comments on commit b1d28cc

Please sign in to comment.