修复ksync.go崩溃时获得崩溃特征码有可能并发崩溃问题lastAttributeCode

This commit is contained in:
liuxiaobo 2025-05-31 08:40:41 +08:00
parent 1de3a11c56
commit 9a6a73cae5

View File

@ -5,29 +5,28 @@ import (
"github.com/fox/fox/log" "github.com/fox/fox/log"
"github.com/wanghuiyt/ding" "github.com/wanghuiyt/ding"
"runtime/debug" "runtime/debug"
"strings"
) )
// 辅助函数列表 // 辅助函数列表
var ( var (
DingAccessToken = "" // 钉钉token DingAccessToken = "" // 钉钉token
DingSecret = "" // 钉钉加签 DingSecret = "" // 钉钉加签
lastAttributeCode = "" // 最近一条error信息 //lastAttributeCode = "" // 最近一条error信息
) )
func getAttributeCode(stackErr string) string { //func getAttributeCode(stackErr string) string {
lines := strings.Split(stackErr, "\n") // lines := strings.Split(stackErr, "\n")
// 检查是否有足够的行数并提取第9行索引为8因为索引从0开始 // // 检查是否有足够的行数并提取第9行索引为8因为索引从0开始
if len(lines) >= 9 { // if len(lines) >= 9 {
goIndex := strings.LastIndex(lines[8], ".go") // goIndex := strings.LastIndex(lines[8], ".go")
if goIndex != -1 { // if goIndex != -1 {
filteredStr := lines[8][:goIndex] // filteredStr := lines[8][:goIndex]
filteredStr = strings.TrimLeft(filteredStr, "\t ") // filteredStr = strings.TrimLeft(filteredStr, "\t ")
return filteredStr // return filteredStr
} // }
} // }
return "" // return ""
} //}
// 发送消息给钉钉 // 发送消息给钉钉
func sendMessage(msg interface{}, stackErr string, dingToken, dingSecret string) { func sendMessage(msg interface{}, stackErr string, dingToken, dingSecret string) {
@ -43,13 +42,13 @@ func sendMessage(msg interface{}, stackErr string, dingToken, dingSecret string)
func Recover(recoverFunc func()) { func Recover(recoverFunc func()) {
if msg := recover(); msg != nil { if msg := recover(); msg != nil {
stackErr := string(debug.Stack()) stackErr := string(debug.Stack())
attributeCode := getAttributeCode(stackErr) //attributeCode := getAttributeCode(stackErr)
if lastAttributeCode != attributeCode { //if lastAttributeCode != attributeCode {
lastAttributeCode = attributeCode // lastAttributeCode = attributeCode
log.ErrorF("Recover panic:%v", msg) log.ErrorF("Recover panic:%v", msg)
log.Error(stackErr) log.Error(stackErr)
sendMessage(msg, stackErr, DingAccessToken, DingSecret) sendMessage(msg, stackErr, DingAccessToken, DingSecret)
} //}
if recoverFunc != nil { if recoverFunc != nil {
recoverFunc() recoverFunc()
} }