This commit is contained in:
shoopea 2019-05-04 17:15:33 +08:00
parent 985353ba10
commit 92f77b3b17
3 changed files with 24 additions and 9 deletions

View File

@ -98,7 +98,10 @@ func main() {
MQCWMsgQueue = make(chan ChatWarsMessage, 100) MQCWMsgQueue = make(chan ChatWarsMessage, 100)
for w := 1; w <= 3; w++ { for w := 1; w <= 3; w++ {
go MQGetMsg(w, MQCWMsgQueue) go MQGetMsgWorker(w, MQCWMsgQueue)
}
for w := 1; w <= 6; w++ {
go SQLCWMsgWorker(w, MQCWMsgQueue)
} }
fmt.Println("Started !") fmt.Println("Started !")

16
mq.go
View File

@ -8,14 +8,14 @@ import (
"github.com/streadway/amqp" "github.com/streadway/amqp"
) )
func MQGetMsg(id int, msgs chan<- ChatWarsMessage) { func MQGetMsgWorker(id int, msgs chan<- ChatWarsMessage) {
var x ChatWarsMessage var x ChatWarsMessage
conn, err := amqp.Dial("amqp://" + cfg.Rabbit.User + ":" + cfg.Rabbit.Password + "@" + cfg.Rabbit.Host + "/" + cfg.Rabbit.Queue) conn, err := amqp.Dial("amqp://" + cfg.Rabbit.User + ":" + cfg.Rabbit.Password + "@" + cfg.Rabbit.Host + "/" + cfg.Rabbit.Queue)
failOnError(err, "MQGetMsg["+strconv.Itoa(id)+"] : Failed to connect to RabbitMQ") failOnError(err, "MQGetMsgWorker["+strconv.Itoa(id)+"] : Failed to connect to RabbitMQ")
defer conn.Close() defer conn.Close()
ch, err := conn.Channel() ch, err := conn.Channel()
failOnError(err, "MQGetMsg["+strconv.Itoa(id)+"] : Failed to open a channel") failOnError(err, "MQGetMsgWorker["+strconv.Itoa(id)+"] : Failed to open a channel")
defer ch.Close() defer ch.Close()
q, err := ch.QueueDeclare( q, err := ch.QueueDeclare(
@ -26,7 +26,7 @@ func MQGetMsg(id int, msgs chan<- ChatWarsMessage) {
false, // no-wait false, // no-wait
nil, // arguments nil, // arguments
) )
failOnError(err, "MQGetMsg["+strconv.Itoa(id)+"] : Failed to declare a queue") failOnError(err, "MQGetMsgWorker["+strconv.Itoa(id)+"] : Failed to declare a queue")
m, err := ch.Consume( m, err := ch.Consume(
q.Name, // queue q.Name, // queue
@ -37,18 +37,18 @@ func MQGetMsg(id int, msgs chan<- ChatWarsMessage) {
false, // no-wait false, // no-wait
nil, // args nil, // args
) )
failOnError(err, "MQGetMsg["+strconv.Itoa(id)+"] : Failed to register a consumer") failOnError(err, "MQGetMsgWorker["+strconv.Itoa(id)+"] : Failed to register a consumer")
for d := range m { for d := range m {
log.Printf("MQGetMsg["+strconv.Itoa(id)+"] : Received a message: %s", string(d.Body)) log.Printf("MQGetMsgWorker["+strconv.Itoa(id)+"] : Received a message: %s", string(d.Body))
if err = json.Unmarshal(d.Body, &x); err != nil { if err = json.Unmarshal(d.Body, &x); err != nil {
logOnError(err, "MQGetMsg["+strconv.Itoa(id)+"] : Can't unmarshal.\n"+string(d.Body)) logOnError(err, "MQGetMsgWorker["+strconv.Itoa(id)+"] : Can't unmarshal.\n"+string(d.Body))
} else { } else {
msgs <- x msgs <- x
} }
} }
log.Printf("MQGetMsg[" + strconv.Itoa(id) + "] : Closing.") log.Printf("MQGetMsgWorker[" + strconv.Itoa(id) + "] : Closing.")
} }

12
sql.go
View File

@ -122,6 +122,18 @@ func initDB() {
log.Println("Database set up") log.Println("Database set up")
} }
func SQLCWMsgWorker(id int, msgs <-chan ChatWarsMessage) {
for m := range msgs {
err := putMsg(m)
if err != nil {
logOnError(err, "SQLCWMsgWorker["+strconv.Itoa(id)+"] : Inserting message.")
}
}
log.Printf("SQLCWMsgWorker[" + strconv.Itoa(id) + "] : Closing.")
}
func putMsg(m ChatWarsMessage) error { func putMsg(m ChatWarsMessage) error {
res, err := db.Exec(`INSERT INTO obj (obj_type_id) res, err := db.Exec(`INSERT INTO obj (obj_type_id)
VALUES (3);`) VALUES (3);`)