try using locks for not spamming live message
This commit is contained in:
parent
f9dbd2a6a6
commit
02fc3d217d
3
main.go
3
main.go
@ -38,6 +38,9 @@ var (
|
||||
ownUserID32 = int32(0)
|
||||
lastOwnTDMsg time.Time
|
||||
lastOwnTDMsgMux sync.Mutex
|
||||
lastChatTDMsg map[uint64]time.Time
|
||||
lastChatTDMsgMux map[uint64]sync.Mutex
|
||||
lastChatMsgMux sync.RWMutex
|
||||
MQCWMsgQueue chan ChatWarsMessage
|
||||
MQTGCmdQueue chan TGCommand
|
||||
)
|
||||
|
22
td.go
22
td.go
@ -69,6 +69,28 @@ func ListenTG(c *tdlib.Client) {
|
||||
|
||||
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
|
||||
|
||||
fmt.Printf("[%d-%02d-%02d %02d:%02d:%02d-00:00]", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second())
|
||||
|
Loading…
Reference in New Issue
Block a user