This commit is contained in:
shoopea 2019-06-14 11:24:43 +08:00
parent 92c47a28ee
commit c94c8efd46
3 changed files with 46 additions and 41 deletions

View File

@ -4,6 +4,34 @@ import (
"strings"
)
func clientKeepAlive(k, v interface{}) bool {
c := v.(MQKeepAlive)
if c.Date.Add(2 * KeepAliveHeartBeatSeconds * time.Second).Before(time.Now()) {
msgs, err := clientsQueue[v.UserID64].Channel.QueuePurge(clientsQueue[v.UserID64].Queue.Name, false)
logOnError(err, "clientKeepAlive : Channel.QueuePurge()")
err = clientsQueue[v.UserID64].Channel.Close()
logOnError(err, "clientKeepAlive : Channel.Close()")
err = clientsQueue[v.UserID64].Connection.Close()
logOnError(err, "clientKeepAlive : Connection.Close()")
c := TGCommand{
Type: commandSendMsg,
ToUserID64: v.UserID64,
Text: "Timeout, purging and closing command queue.",
}
TGCmdQueue <- c
c = TGCommand{
Type: commandSendMsg,
ToUserID64: cfg.Bot.Admin,
Text: fmt.Sprintf("Client %s timed out (%d messages purged).", v.Nickname, msgs),
}
TGCmdQueue <- c
clientsKeepAlive.Delete(c.UserID64)
}
return true
}
func clientSendCWMsg(userID64 int64, s string) {
c := TGCommand{
Type: commandSendMsg,

1
def.go
View File

@ -396,6 +396,7 @@ const (
MQTGCmdWorkers = 3
MQTGCmdQueueSize = 100
SQLJobSliceSize = 25
KeepAliveHeartBeatSeconds = 5
)
var (

View File

@ -440,31 +440,7 @@ func MQTidyKeepAliveWorker() {
//log.Printf("MQTidyKeepAliveWorker : Starting.")
for true {
t := time.Now()
for _, v := range clientsKeepAlive {
if v.Date.Add(90 * time.Second).Before(time.Now()) {
msgs, err := clientsQueue[v.UserID64].Channel.QueuePurge(clientsQueue[v.UserID64].Queue.Name, false)
logOnError(err, "MQTidyKeepAliveWorker : Channel.QueuePurge()")
err = clientsQueue[v.UserID64].Channel.Close()
logOnError(err, "MQTidyKeepAliveWorker : Channel.Close()")
err = clientsQueue[v.UserID64].Connection.Close()
logOnError(err, "MQTidyKeepAliveWorker : Connection.Close()")
c := TGCommand{
Type: commandSendMsg,
ToUserID64: v.UserID64,
Text: "Timeout, purging and closing command queue.",
}
TGCmdQueue <- c
c = TGCommand{
Type: commandSendMsg,
ToUserID64: cfg.Bot.Admin,
Text: fmt.Sprintf("Client %s timed out (%d messages purged).", v.Nickname, msgs),
}
TGCmdQueue <- c
clientsKeepAlive.Delete(v.UserID64)
}
}
clientsKeepAlive.Range(clientKeepAlive)
time.Sleep(time.Until(t.Add(time.Second)))
}
log.Printf("MQTidyKeepAliveWorker : Closing.")