This commit is contained in:
shoopea 2019-06-28 16:18:48 +08:00
parent e70e4677cd
commit 6b320ff0ee
2 changed files with 42 additions and 40 deletions

14
mq.go
View File

@ -7,5 +7,19 @@ import (
func (c *MQClient) Open() error { func (c *MQClient) Open() error {
conn, err := amqp.Dial("amqp://" + c.User + ":" + c.Password + "@" + c.Host + "/" + c.Path) conn, err := amqp.Dial("amqp://" + c.User + ":" + c.Password + "@" + c.Host + "/" + c.Path)
c.Connection = conn c.Connection = conn
if err != nil {
c.Connection.Close()
return err
}
ch, err := c.Connection.Channel()
c.Channel = ch
if err != nil {
c.Channel.Close()
c.Connection.Close()
return err
}
return err return err
} }

View File

@ -27,18 +27,7 @@ func MQGetMsgWorker(id int, msgs chan<- ChatWarsMessage) {
err := c.Open() err := c.Open()
logOnError(err, "MQGetMsgWorker["+strconv.Itoa(id)+"] : Failed to connect to RabbitMQ")
if err != nil { if err != nil {
c.Connection.Close()
time.Sleep(15 * time.Second)
} else {
ch, err := c.Connection.Channel()
c.Channel = ch
logOnError(err, "MQGetMsgWorker["+strconv.Itoa(id)+"] : Failed to open a channel")
if err != nil {
c.Channel.Close()
time.Sleep(15 * time.Second)
} else {
q, err := c.Channel.QueueDeclare( q, err := c.Channel.QueueDeclare(
"msg", // name "msg", // name
false, // durable false, // durable
@ -83,7 +72,6 @@ func MQGetMsgWorker(id int, msgs chan<- ChatWarsMessage) {
} }
} }
}
log.Printf("MQGetMsgWorker[" + strconv.Itoa(id) + "] : Closing.") log.Printf("MQGetMsgWorker[" + strconv.Itoa(id) + "] : Closing.")