Skip to content

Commit

Permalink
add rocketmq panic handler
Browse files Browse the repository at this point in the history
  • Loading branch information
luduoxin committed Aug 28, 2024
1 parent 77676c5 commit b259af6
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions mq/aliyunmq/rocketmq.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package aliyunmq

import (
"errors"
"runtime"

"github.com/apache/rocketmq-client-go/v2/primitive"
"github.com/sunmi-OS/gocore/v2/conf/viper"
"github.com/sunmi-OS/gocore/v2/glog"
)

// RocketMQConfig 基础配置
Expand All @@ -20,7 +23,7 @@ type RocketMQConfig struct {
IsLocal bool
}

// initConfig 通过viper初始化配置
// initConfig Initial configuration
func initConfig(configName string) RocketMQConfig {
mqConfig := RocketMQConfig{
NameServer: viper.GetEnvConfig(configName + ".NameServer").String(),
Expand All @@ -33,12 +36,29 @@ func initConfig(configName string) RocketMQConfig {
if err != nil {
panic(err)
}
// 默认日志等级 Error
// Set the default log level to error
LogError()
// Set the panic handler
primitive.PanicHandler = func(i interface{}) {
stacktrace := stack()
glog.ErrorF("rocketmq panic recovered:%+v, stacktrace:%s", i, stacktrace)
}
return mqConfig
}

// checkConfig 检查配置完整性
func stack() []byte {
buf := make([]byte, 1024)
for {
n := runtime.Stack(buf, false)
bufSize := len(buf)
if n < bufSize {
return buf[:n]
}
buf = make([]byte, bufSize*2)
}
}

// checkConfig Check configuration integrity
func checkConfig(conf RocketMQConfig) (err error) {
if conf.AccessKey == "" || conf.NameServer == "" || conf.SecretKey == "" {
err = errors.New("missing required configuration")
Expand Down

0 comments on commit b259af6

Please sign in to comment.