feat: 支持通过 super.StackGO 进行跨协程同步运行堆栈抓取
This commit is contained in:
parent
98234e5f86
commit
b5a4bc959d
|
@ -11,6 +11,7 @@ func NewStackGo() *StackGo {
|
||||||
|
|
||||||
// StackGo 用于获取上一个协程调用的堆栈信息
|
// StackGo 用于获取上一个协程调用的堆栈信息
|
||||||
// - 应当最先运行 Wait 函数,然后在其他协程中调用 Stack 函数或者 GiveUp 函数
|
// - 应当最先运行 Wait 函数,然后在其他协程中调用 Stack 函数或者 GiveUp 函数
|
||||||
|
// - 适用于跨协程同步通讯,例如单线程的消息处理统计耗时打印堆栈信息
|
||||||
type StackGo struct {
|
type StackGo struct {
|
||||||
stack chan *struct{} // 消息堆栈
|
stack chan *struct{} // 消息堆栈
|
||||||
collect chan []byte // 消息堆栈收集
|
collect chan []byte // 消息堆栈收集
|
||||||
|
@ -22,8 +23,6 @@ func (slf *StackGo) Wait() {
|
||||||
slf.stack = make(chan *struct{}, 0)
|
slf.stack = make(chan *struct{}, 0)
|
||||||
if s := <-slf.stack; s != nil {
|
if s := <-slf.stack; s != nil {
|
||||||
slf.collect <- debug.Stack()
|
slf.collect <- debug.Stack()
|
||||||
close(slf.collect)
|
|
||||||
slf.collect = nil
|
|
||||||
}
|
}
|
||||||
close(slf.stack)
|
close(slf.stack)
|
||||||
slf.stack = nil
|
slf.stack = nil
|
||||||
|
@ -33,10 +32,14 @@ func (slf *StackGo) Wait() {
|
||||||
// - 在调用 Wait 函数后调用该函数,将会返回上一个协程的堆栈信息
|
// - 在调用 Wait 函数后调用该函数,将会返回上一个协程的堆栈信息
|
||||||
// - 在调用 GiveUp 函数后调用该函数,将会 panic
|
// - 在调用 GiveUp 函数后调用该函数,将会 panic
|
||||||
func (slf *StackGo) Stack() []byte {
|
func (slf *StackGo) Stack() []byte {
|
||||||
slf.stack <- &struct{}{}
|
if slf.stack == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
slf.collect = make(chan []byte, 1)
|
slf.collect = make(chan []byte, 1)
|
||||||
|
slf.stack <- &struct{}{}
|
||||||
stack := <-slf.collect
|
stack := <-slf.collect
|
||||||
slf.stack = nil
|
close(slf.collect)
|
||||||
|
slf.collect = nil
|
||||||
return stack
|
return stack
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue