try using locks for not spamming live message

This commit is contained in:
shoopea 2019-11-05 10:34:26 +08:00
parent f9dbd2a6a6
commit 02fc3d217d
2 changed files with 31 additions and 6 deletions

15
main.go
View File

@ -34,12 +34,15 @@ var (
cfg Config cfg Config
ownUserID64 = int64(0) ownUserID64 = int64(0)
ownUserID32 = int32(0) ownUserID32 = int32(0)
lastOwnTDMsg time.Time lastOwnTDMsg time.Time
lastOwnTDMsgMux sync.Mutex lastOwnTDMsgMux sync.Mutex
MQCWMsgQueue chan ChatWarsMessage lastChatTDMsg map[uint64]time.Time
MQTGCmdQueue chan TGCommand lastChatTDMsgMux map[uint64]sync.Mutex
lastChatMsgMux sync.RWMutex
MQCWMsgQueue chan ChatWarsMessage
MQTGCmdQueue chan TGCommand
) )
func main() { func main() {

22
td.go
View File

@ -69,6 +69,28 @@ func ListenTG(c *tdlib.Client) {
m.Date = time.Unix(int64(updateMsg.Message.Date), 0) m.Date = time.Unix(int64(updateMsg.Message.Date), 0)
lastChatMsgMux.RLock()
if _, ok := lastChatTDMsg[m.ChatID64]; !ok {
lastChatMsgMux.RUnlock()
lastChatMsgMux.Lock()
if _, ok := lastChatTDMsg[m.ChatID64]; !ok {
lastChatTDMsg[m.ChatID64] = time.Now().Add(-1 * time.Second)
lastChatTDMsgMux[m.ChatID64] = new(sync.Mutex)
}
lastChatMsgMux.Unlock()
}
for {
lastChatTDMsgMux[m.ChatID64].Lock()
now := time.Now()
if lastChatTDMsg[m.ChatID64].Add(time.Second).Before(now) {
lastChatTDMsg[m.ChatID64] = now
lastChatTDMsgMux[m.ChatID64].Unlock()
break
}
lastChatTDMsgMux[m.ChatID64].Unlock()
time.Sleep(time.Until(now.Add(time.Second)))
}
MQCWMsgQueue <- m MQCWMsgQueue <- m
fmt.Printf("[%d-%02d-%02d %02d:%02d:%02d-00:00]", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second()) fmt.Printf("[%d-%02d-%02d %02d:%02d:%02d-00:00]", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second())