Skip to content

Commit

Permalink
Merge pull request #175 from sunmi-OS/feature/imp
Browse files Browse the repository at this point in the history
add rocketmq panic handler
luduoxin authored Aug 28, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents 211b0af + b259af6 commit fc892d8
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
@@ -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 基础配置
@@ -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(),
@@ -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")

0 comments on commit fc892d8

Please sign in to comment.