optimize code of rabbitmq connection and event alarm struct
This commit is contained in:
parent
6618209bcc
commit
1c385ee60d
|
|
@ -19,7 +19,7 @@ const (
|
|||
deadQueueName = "event-alarm-dead-letter-queue"
|
||||
)
|
||||
|
||||
func initChannel(ctx context.Context) (*amqp.Channel, error) {
|
||||
func initEventAlarmChannel(ctx context.Context) (*amqp.Channel, error) {
|
||||
var channel *amqp.Channel
|
||||
var err error
|
||||
|
||||
|
|
@ -70,8 +70,9 @@ func initChannel(ctx context.Context) (*amqp.Channel, error) {
|
|||
return channel, nil
|
||||
}
|
||||
|
||||
func pushEventToRabbitMQ(ctx context.Context, msgChan chan string) {
|
||||
channel, err := initChannel(ctx)
|
||||
// PushEventToRabbitMQ define func to push event alarm message to rabbitMQ
|
||||
func PushEventToRabbitMQ(ctx context.Context, msgChan chan string) {
|
||||
channel, err := initEventAlarmChannel(ctx)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "initializing rabbitMQ channel failed", "error", err)
|
||||
return
|
||||
|
|
|
|||
|
|
@ -7,7 +7,6 @@ import (
|
|||
"crypto/x509"
|
||||
"encoding/pem"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
|
|
@ -28,7 +27,7 @@ var (
|
|||
type RabbitMQProxy struct {
|
||||
tlsConf *tls.Config
|
||||
conn *amqp.Connection
|
||||
cancel context.CancelFunc // 增加这个用于停止重连协程
|
||||
cancel context.CancelFunc
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
|
|
@ -148,12 +147,17 @@ func CloseRabbitProxy() {
|
|||
}
|
||||
|
||||
func generateRabbitMQURI(rCfg config.RabbitMQConfig) string {
|
||||
user := url.QueryEscape(rCfg.User)
|
||||
password := url.QueryEscape(rCfg.Password)
|
||||
// TODO 考虑拆分用户名密码配置项,兼容不同认证方式
|
||||
// user := url.QueryEscape(rCfg.User)
|
||||
// password := url.QueryEscape(rCfg.Password)
|
||||
|
||||
amqpURI := fmt.Sprintf("amqps://%s:%s@%s:%d/",
|
||||
user,
|
||||
password,
|
||||
// amqpURI := fmt.Sprintf("amqps://%s:%s@%s:%d/",
|
||||
// user,
|
||||
// password,
|
||||
// rCfg.Host,
|
||||
// rCfg.Port,
|
||||
// )
|
||||
amqpURI := fmt.Sprintf("amqps://%s:%d/",
|
||||
rCfg.Host,
|
||||
rCfg.Port,
|
||||
)
|
||||
|
|
|
|||
Loading…
Reference in New Issue