parent
afffc3beb9
commit
0a0c8e7960
@ -14,7 +14,7 @@ database = chirpnest
|
|||||||
user = chirpnest
|
user = chirpnest
|
||||||
password = chirpnest
|
password = chirpnest
|
||||||
host = localhost:5672
|
host = localhost:5672
|
||||||
path = chirpnest
|
queue = chirpnest
|
||||||
|
|
||||||
[bot]
|
[bot]
|
||||||
admin = 0
|
admin = 0
|
||||||
|
@ -34,11 +34,6 @@ func clientKeepAlive(k, v interface{}) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func clientIsAlive(id int64) bool {
|
|
||||||
_, ok := clientsKeepAlive.Load(id)
|
|
||||||
return ok
|
|
||||||
}
|
|
||||||
|
|
||||||
func clientSendCWMsg(userID64 int64, s string) {
|
func clientSendCWMsg(userID64 int64, s string) {
|
||||||
c := TGCommand{
|
c := TGCommand{
|
||||||
Type: commandSendMsg,
|
Type: commandSendMsg,
|
||||||
|
16
def.go
16
def.go
@ -19,18 +19,10 @@ type MQKeepAlive struct {
|
|||||||
Date time.Time `json:"date"`
|
Date time.Time `json:"date"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type MQSession struct {
|
type MQClient struct {
|
||||||
Host string
|
Connection *amqp.Connection
|
||||||
SSL bool
|
Channel *amqp.Channel
|
||||||
User string
|
Queue amqp.Queue
|
||||||
Password string
|
|
||||||
Path string
|
|
||||||
Queue string
|
|
||||||
MQConnection *amqp.Connection
|
|
||||||
MQChannel *amqp.Channel
|
|
||||||
MQQueue *amqp.Queue
|
|
||||||
MQDelivery <-chan *ampq.Delivery
|
|
||||||
isConnected bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type ChatWarsClient struct {
|
type ChatWarsClient struct {
|
||||||
|
6
main.go
6
main.go
@ -32,7 +32,7 @@ type Config struct {
|
|||||||
User string
|
User string
|
||||||
Password string
|
Password string
|
||||||
Host string
|
Host string
|
||||||
Path string
|
Queue string
|
||||||
}
|
}
|
||||||
Bot struct {
|
Bot struct {
|
||||||
Admin int64
|
Admin int64
|
||||||
@ -60,7 +60,7 @@ var (
|
|||||||
JobQueue chan Job
|
JobQueue chan Job
|
||||||
|
|
||||||
msgParsingRules map[int]MessageParsingRule
|
msgParsingRules map[int]MessageParsingRule
|
||||||
clientsQueue map[int64]*MQSession
|
clientsQueue map[int64]*MQClient
|
||||||
|
|
||||||
clientsCW *sync.Map
|
clientsCW *sync.Map
|
||||||
clientsKeepAlive *sync.Map
|
clientsKeepAlive *sync.Map
|
||||||
@ -153,7 +153,7 @@ func main() {
|
|||||||
TGCmdQueue = make(chan TGCommand, TGCmdQueueSize)
|
TGCmdQueue = make(chan TGCommand, TGCmdQueueSize)
|
||||||
MQTGCmdQueue = make(chan TGCommand, MQTGCmdQueueSize)
|
MQTGCmdQueue = make(chan TGCommand, MQTGCmdQueueSize)
|
||||||
JobQueue = make(chan Job, JobQueueSize)
|
JobQueue = make(chan Job, JobQueueSize)
|
||||||
clientsQueue = make(map[int64]*MQSession)
|
clientsQueue = make(map[int64]*MQClient)
|
||||||
|
|
||||||
clientsCW = new(sync.Map)
|
clientsCW = new(sync.Map)
|
||||||
clientsKeepAlive = new(sync.Map)
|
clientsKeepAlive = new(sync.Map)
|
||||||
|
88
mq.go
88
mq.go
@ -1,89 +1 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
|
||||||
"error"
|
|
||||||
|
|
||||||
"github.com/streadway/amqp"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (s MQSession) Open() error {
|
|
||||||
if s.isConnected {
|
|
||||||
return error.Error("Session is already connected.")
|
|
||||||
}
|
|
||||||
|
|
||||||
if SLL == false {
|
|
||||||
s.MQConnection, err = amqp.Dial("amqp://" + Session.User + ":" + Session.Password + "@" + Session.Host + "/" + Session.Path)
|
|
||||||
} else {
|
|
||||||
return error.Error("SSL connection not implemented")
|
|
||||||
}
|
|
||||||
logOnError(err, "Open : Failed to connect to RabbitMQ")
|
|
||||||
if err != nil {
|
|
||||||
s.MQConnection.Close()
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
s.MQChannel, err = s.MQConnection.Channel()
|
|
||||||
logOnError(err, "Open : Failed to open channel")
|
|
||||||
if err != nil {
|
|
||||||
s.MQChannel.Close()
|
|
||||||
s.MQConnection.Close()
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
s.MQQueue, err = s.Channel.QueueDeclare(
|
|
||||||
s.Queue, // name
|
|
||||||
false, // durable
|
|
||||||
false, // delete when unused
|
|
||||||
false, // exclusive
|
|
||||||
false, // no-wait
|
|
||||||
nil, // arguments
|
|
||||||
|
|
||||||
)
|
|
||||||
logOnError(err, "Open : Failed to declare queue")
|
|
||||||
if err != nil {
|
|
||||||
s.MQChannel.Close()
|
|
||||||
s.MQConnection.Close()
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
s.MQDelivery, err = s.MQChannel.Consume(
|
|
||||||
s.MQQueue.Name, // queue
|
|
||||||
"", // consumer
|
|
||||||
true, // auto-ack
|
|
||||||
false, // exclusive
|
|
||||||
false, // no-local
|
|
||||||
false, // no-wait
|
|
||||||
nil, // args
|
|
||||||
)
|
|
||||||
logOnError(err, "Open : Failed to register a consumer")
|
|
||||||
if err != nil {
|
|
||||||
s.MQChannel.Close()
|
|
||||||
s.MQConnection.Close()
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
s.isConnected = true
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s MQSession) Publish(content string, msg string) error {
|
|
||||||
err := s.MQChannel.Publish(
|
|
||||||
"", // exchange
|
|
||||||
s.MQQueue.Name, // routing key
|
|
||||||
false, // mandatory
|
|
||||||
false, // immediate
|
|
||||||
amqp.Publishing{
|
|
||||||
ContentType: content,
|
|
||||||
Body: []byte(m),
|
|
||||||
})
|
|
||||||
logOnError(err, "Publish : Publish")
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s MQSession) Close() {
|
|
||||||
if s.isConnected {
|
|
||||||
s.MQChannel.Close()
|
|
||||||
s.MQConnection.Close()
|
|
||||||
s.isConnected = false
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
256
workers.go
256
workers.go
@ -16,33 +16,63 @@ import (
|
|||||||
func MQGetMsgWorker(id int, msgs chan<- ChatWarsMessage) {
|
func MQGetMsgWorker(id int, msgs chan<- ChatWarsMessage) {
|
||||||
//log.Printf("MQGetMsgWorker[" + strconv.Itoa(id) + "] : Starting.")
|
//log.Printf("MQGetMsgWorker[" + strconv.Itoa(id) + "] : Starting.")
|
||||||
var x ChatWarsMessage
|
var x ChatWarsMessage
|
||||||
s := MQSession{
|
|
||||||
User: cfg.Rabbit.User,
|
|
||||||
Password: cfg.Rabbit.Password,
|
|
||||||
Host: cfg.Rabbit.Host,
|
|
||||||
Path: cfg.Rabbit.Path,
|
|
||||||
SSL: false,
|
|
||||||
Queue: "msg",
|
|
||||||
isConnected: false,
|
|
||||||
}
|
|
||||||
for true {
|
for true {
|
||||||
err := s.Open()
|
conn, err := amqp.Dial("amqp://" + cfg.Rabbit.User + ":" + cfg.Rabbit.Password + "@" + cfg.Rabbit.Host + "/" + cfg.Rabbit.Queue)
|
||||||
for err != nil {
|
logOnError(err, "MQGetMsgWorker["+strconv.Itoa(id)+"] : Failed to connect to RabbitMQ")
|
||||||
logOnError(err, "MQGetMsgWorker["+strconv.Itoa(id)+"] : Connection to RabbitMQ failed.")
|
if err != nil {
|
||||||
time.Sleep(15)
|
conn.Close()
|
||||||
err = s.Open()
|
time.Sleep(15 * time.Second)
|
||||||
}
|
} else {
|
||||||
log.Printf("MQGetMsgWorker[" + strconv.Itoa(id) + "] : Connected to RabbitMQ")
|
ch, err := conn.Channel()
|
||||||
|
logOnError(err, "MQGetMsgWorker["+strconv.Itoa(id)+"] : Failed to open a channel")
|
||||||
|
if err != nil {
|
||||||
|
ch.Close()
|
||||||
|
time.Sleep(15 * time.Second)
|
||||||
|
} else {
|
||||||
|
q, err := ch.QueueDeclare(
|
||||||
|
"msg", // name
|
||||||
|
false, // durable
|
||||||
|
false, // delete when unused
|
||||||
|
false, // exclusive
|
||||||
|
false, // no-wait
|
||||||
|
nil, // arguments
|
||||||
|
)
|
||||||
|
logOnError(err, "MQGetMsgWorker["+strconv.Itoa(id)+"] : Failed to declare a queue")
|
||||||
|
if err != nil {
|
||||||
|
ch.Close()
|
||||||
|
conn.Close()
|
||||||
|
time.Sleep(15 * time.Second)
|
||||||
|
} else {
|
||||||
|
m, err := ch.Consume(
|
||||||
|
q.Name, // queue
|
||||||
|
"", // consumer
|
||||||
|
true, // auto-ack
|
||||||
|
false, // exclusive
|
||||||
|
false, // no-local
|
||||||
|
false, // no-wait
|
||||||
|
nil, // args
|
||||||
|
)
|
||||||
|
logOnError(err, "MQGetMsgWorker["+strconv.Itoa(id)+"] : Failed to register a consumer")
|
||||||
|
if err != nil {
|
||||||
|
ch.Close()
|
||||||
|
conn.Close()
|
||||||
|
time.Sleep(15 * time.Second)
|
||||||
|
} else {
|
||||||
|
for d := range m {
|
||||||
|
// log.Printf("MQGetMsgWorker["+strconv.Itoa(id)+"] : Received a message: %s", string(d.Body))
|
||||||
|
err = json.Unmarshal(d.Body, &x)
|
||||||
|
logOnError(err, "MQGetMsgWorker["+strconv.Itoa(id)+"] : Can't unmarshal.\n"+string(d.Body))
|
||||||
|
if err == nil {
|
||||||
|
msgs <- x
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ch.Close()
|
||||||
|
conn.Close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for d := range s.MQDelivery {
|
|
||||||
// log.Printf("MQGetMsgWorker["+strconv.Itoa(id)+"] : Received a message: %s", string(d.Body))
|
|
||||||
err = json.Unmarshal(d.Body, &x)
|
|
||||||
logOnError(err, "MQGetMsgWorker["+strconv.Itoa(id)+"] : Can't unmarshal.\n"+string(d.Body))
|
|
||||||
if err == nil {
|
|
||||||
msgs <- x
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
s.Close()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("MQGetMsgWorker[" + strconv.Itoa(id) + "] : Closing.")
|
log.Printf("MQGetMsgWorker[" + strconv.Itoa(id) + "] : Closing.")
|
||||||
@ -313,20 +343,21 @@ func TGCmdWorker(id int, b *tb.Bot, cmds <-chan TGCommand) {
|
|||||||
func MQTGCmdWorker(id int, cmds <-chan TGCommand) {
|
func MQTGCmdWorker(id int, cmds <-chan TGCommand) {
|
||||||
//log.Printf("MQTGCmdWorker[" + strconv.Itoa(id) + "] : Starting.")
|
//log.Printf("MQTGCmdWorker[" + strconv.Itoa(id) + "] : Starting.")
|
||||||
for c := range cmds {
|
for c := range cmds {
|
||||||
j, err := json.Marshal(c)
|
if _, ok := clientsKeepAlive.Load(c.FromUserID64); ok {
|
||||||
logOnError(err, "MQTGCmdWorker["+strconv.Itoa(id)+"] : Marshal(c)")
|
j, err := json.Marshal(c)
|
||||||
|
logOnError(err, "MQTGCmdWorker["+strconv.Itoa(id)+"] : Marshal(c)")
|
||||||
if clientIsAlive(c.FromUserID64) {
|
//log.Printf("MQTGCmdWorker["+strconv.Itoa(id)+"] : new command.\n%s\n", string(j))
|
||||||
err = clientsQueue[c.FromUserID64].Session.Publish("application/json", j)
|
err = clientsQueue[c.FromUserID64].Channel.Publish(
|
||||||
|
"", // exchange
|
||||||
|
clientsQueue[c.FromUserID64].Queue.Name, // routing key
|
||||||
|
false, // mandatory
|
||||||
|
false, // immediate
|
||||||
|
amqp.Publishing{
|
||||||
|
ContentType: "application/json",
|
||||||
|
Body: []byte(j),
|
||||||
|
})
|
||||||
logOnError(err, "MQTGCmdWorker["+strconv.Itoa(id)+"] : Publishing message.")
|
logOnError(err, "MQTGCmdWorker["+strconv.Itoa(id)+"] : Publishing message.")
|
||||||
for err != nil && clientIsAlive(c.FromUserID64) {
|
|
||||||
clientsQueue[c.FromUserID64].Session.Close()
|
|
||||||
time.Sleep(5 * time.Second)
|
|
||||||
clientsQueue[c.FromUserID64].Session.Open()
|
|
||||||
err = clientsQueue[c.FromUserID64].Session.Publish("application/json", j)
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
err = nil
|
|
||||||
log.Printf("MQTGCmdWorker["+strconv.Itoa(id)+"] : client %d offline.\n", c.FromUserID64)
|
log.Printf("MQTGCmdWorker["+strconv.Itoa(id)+"] : client %d offline.\n", c.FromUserID64)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -336,73 +367,110 @@ func MQTGCmdWorker(id int, cmds <-chan TGCommand) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func MQKeepAliveWorker() {
|
func MQKeepAliveWorker() {
|
||||||
s := MQSession{
|
|
||||||
User: cfg.Rabbit.User,
|
|
||||||
Password: cfg.Rabbit.Password,
|
|
||||||
Host: cfg.Rabbit.Host,
|
|
||||||
Path: cfg.Rabbit.Path,
|
|
||||||
SSL: false,
|
|
||||||
Queue: "keepalive",
|
|
||||||
isConnected: false,
|
|
||||||
}
|
|
||||||
//log.Printf("MQKeepAliveWorker : Starting.")
|
//log.Printf("MQKeepAliveWorker : Starting.")
|
||||||
for true {
|
for true {
|
||||||
err := s.Open()
|
conn, err := amqp.Dial("amqp://" + cfg.Rabbit.User + ":" + cfg.Rabbit.Password + "@" + cfg.Rabbit.Host + "/" + cfg.Rabbit.Queue)
|
||||||
for err != nil {
|
logOnError(err, "MQKeepAliveWorker : Failed to connect to RabbitMQ")
|
||||||
logOnError(err, "MQKeepAliveWorker : Failed to connect to RabbitMQ")
|
if err != nil {
|
||||||
|
conn.Close()
|
||||||
time.Sleep(15 * time.Second)
|
time.Sleep(15 * time.Second)
|
||||||
err = s.Open()
|
} else {
|
||||||
}
|
ch, err := conn.Channel()
|
||||||
for d := range m {
|
logOnError(err, "MQKeepAliveWorker : Failed to open a channel")
|
||||||
// log.Printf("MQKeepAliveWorker : Received a message: %s", string(d.Body))
|
if err != nil {
|
||||||
x := MQKeepAlive{}
|
ch.Close()
|
||||||
err = json.Unmarshal(d.Body, &x)
|
conn.Close()
|
||||||
logOnError(err, "MQKeepAliveWorker : Can't unmarshal.\n"+string(d.Body))
|
time.Sleep(15 * time.Second)
|
||||||
if err == nil {
|
} else {
|
||||||
if x.Date.Add(10 * time.Second).Before(time.Now()) {
|
q, err := ch.QueueDeclare(
|
||||||
// outdated keep-alive coming from client
|
"keepalive", // name
|
||||||
} else if v, ok := clientsKeepAlive.Load(x.UserID64); ok {
|
false, // durable
|
||||||
k := v.(*MQKeepAlive)
|
false, // delete when unused
|
||||||
k.Date = x.Date
|
false, // exclusive
|
||||||
|
false, // no-wait
|
||||||
|
nil, // arguments
|
||||||
|
)
|
||||||
|
logOnError(err, "MQKeepAliveWorker : Failed to declare a queue")
|
||||||
|
if err != nil {
|
||||||
|
ch.Close()
|
||||||
|
conn.Close()
|
||||||
|
time.Sleep(15 * time.Second)
|
||||||
} else {
|
} else {
|
||||||
cs := MQSession{
|
m, err := ch.Consume(
|
||||||
User: cfg.Rabbit.User,
|
q.Name, // queue
|
||||||
Password: cfg.Rabbit.Password,
|
"", // consumer
|
||||||
Host: cfg.Rabbit.Host,
|
true, // auto-ack
|
||||||
Path: x.Queue,
|
false, // exclusive
|
||||||
SSL: false,
|
false, // no-local
|
||||||
Queue: "msg",
|
false, // no-wait
|
||||||
isConnected: false,
|
nil, // args
|
||||||
}
|
)
|
||||||
err = cs.Open()
|
logOnError(err, "MQKeepAliveWorker : Failed to register a consumer")
|
||||||
logOnError(err, "MQKeepAliveWorker : Failed to open MQ session")
|
if err != nil {
|
||||||
clientsKeepAlive.Store(x.UserID64, &x)
|
ch.Close()
|
||||||
clientsQueue[x.UserID64] = &cs
|
conn.Close()
|
||||||
|
time.Sleep(15 * time.Second)
|
||||||
|
} else {
|
||||||
|
for d := range m {
|
||||||
|
// log.Printf("MQKeepAliveWorker : Received a message: %s", string(d.Body))
|
||||||
|
x := MQKeepAlive{}
|
||||||
|
err = json.Unmarshal(d.Body, &x)
|
||||||
|
logOnError(err, "MQKeepAliveWorker : Can't unmarshal.\n"+string(d.Body))
|
||||||
|
if err == nil {
|
||||||
|
if x.Date.Add(10 * time.Second).Before(time.Now()) {
|
||||||
|
// outdated keep-alive coming from client
|
||||||
|
} else if v, ok := clientsKeepAlive.Load(x.UserID64); ok {
|
||||||
|
k := v.(*MQKeepAlive)
|
||||||
|
k.Date = x.Date
|
||||||
|
} else {
|
||||||
|
clt := MQClient{}
|
||||||
|
clt.Connection, err = amqp.Dial("amqp://" + cfg.Rabbit.User + ":" + cfg.Rabbit.Password + "@" + cfg.Rabbit.Host + "/" + x.Queue)
|
||||||
|
logOnError(err, "MQKeepAliveWorker : Failed to connect to RabbitMQ")
|
||||||
|
clt.Channel, err = clt.Connection.Channel()
|
||||||
|
logOnError(err, "MQKeepAliveWorker : Failed to open a channel")
|
||||||
|
clt.Queue, err = clt.Channel.QueueDeclare(
|
||||||
|
"msg", // name
|
||||||
|
false, // durable
|
||||||
|
false, // delete when unused
|
||||||
|
false, // exclusive
|
||||||
|
false, // no-wait
|
||||||
|
nil, // arguments
|
||||||
|
)
|
||||||
|
logOnError(err, "MQKeepAliveWorker : Failed to declare a queue")
|
||||||
|
clientsKeepAlive.Store(x.UserID64, &x)
|
||||||
|
clientsQueue[x.UserID64] = &clt
|
||||||
|
|
||||||
c := TGCommand{
|
c := TGCommand{
|
||||||
Type: commandSendMsg,
|
Type: commandSendMsg,
|
||||||
ToUserID64: x.UserID64,
|
ToUserID64: x.UserID64,
|
||||||
Text: "Your client is connected.",
|
Text: "Your client is connected.",
|
||||||
}
|
}
|
||||||
TGCmdQueue <- c
|
TGCmdQueue <- c
|
||||||
c = TGCommand{
|
c = TGCommand{
|
||||||
Type: commandSendMsg,
|
Type: commandSendMsg,
|
||||||
ToUserID64: cfg.Bot.Admin,
|
ToUserID64: cfg.Bot.Admin,
|
||||||
Text: fmt.Sprintf("Client `%s` is connected.", x.Nickname),
|
Text: fmt.Sprintf("Client `%s` is connected.", x.Nickname),
|
||||||
}
|
}
|
||||||
TGCmdQueue <- c
|
TGCmdQueue <- c
|
||||||
|
|
||||||
clientSendCWMsg(x.UserID64, `🏅Me`)
|
clientSendCWMsg(x.UserID64, `🏅Me`)
|
||||||
/*
|
/*
|
||||||
c = TGCommand{
|
c = TGCommand{
|
||||||
Type: commandSendMsg,
|
Type: commandSendMsg,
|
||||||
FromUserID64: x.UserID64,
|
FromUserID64: x.UserID64,
|
||||||
ToChatID64: userID64ChtWrsBot,
|
ToChatID64: userID64ChtWrsBot,
|
||||||
Text: `/hero`,
|
Text: `/hero`,
|
||||||
|
}
|
||||||
|
MQTGCmdQueue <- c
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
MQTGCmdQueue <- c
|
ch.Close()
|
||||||
*/
|
conn.Close()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user