From c9615cbdfb411cd636eb7b41e98592fa94b87216 Mon Sep 17 00:00:00 2001 From: mappu Date: Wed, 6 Jun 2018 19:39:22 +1200 Subject: [PATCH] extract message-coalesce-buffer to separate file --- NTFServer.go | 41 ----------------------------------------- coalesce.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 41 deletions(-) create mode 100644 coalesce.go diff --git a/NTFServer.go b/NTFServer.go index 6bf7c1d..d223436 100644 --- a/NTFServer.go +++ b/NTFServer.go @@ -15,11 +15,6 @@ import ( telegram "github.com/go-telegram-bot-api/telegram-bot-api" ) -const ( - coalesceWorkerRescanEvery time.Duration = 10 * time.Second - coalesceWorkerExpireAfter time.Duration = 20 * time.Second -) - // NTFServer methods all run on the same thread, so no mutexes are needed for field access type NTFServer struct { bot *telegram.BotAPI @@ -39,10 +34,6 @@ type NTFServer struct { coalesceBuffer map[string]time.Time } -func coalesceKey(hubNick, message string) string { - return hubNick + "\x00" + message -} - type upstreamMessage struct { telegramUserId int64 hubNick string @@ -267,38 +258,6 @@ func (this *NTFServer) Run() error { return nil // UNREACHABLE } -func (this *NTFServer) coalesceWorker() { - for { - time.Sleep(coalesceWorkerRescanEvery) - - deadLine := time.Now().Add(-coalesceWorkerExpireAfter) - - this.coalesceBufferMut.Lock() - for k, v := range this.coalesceBuffer { - if v.Before(deadLine) { - delete(this.coalesceBuffer, k) - } - } - this.coalesceBufferMut.Unlock() - } -} - -// Coalesce returns true if the message sticks to an existing one. -// It adds it into the coalesce buffer anyway. -func (this *NTFServer) Coalesce(hubNick, message string) bool { - ckey := coalesceKey(hubNick, message) - - this.coalesceBufferMut.Lock() - if _, ok := this.coalesceBuffer[ckey]; ok { - this.coalesceBufferMut.Unlock() - return true // stuck to existing - } else { - this.coalesceBuffer[ckey] = time.Now() - this.coalesceBufferMut.Unlock() - return false // first we've seen it - } -} - func (this *NTFServer) HandleHubMessage(msg upstreamMessage) { if this.verbose { log.Printf("Hub: %#v", msg) diff --git a/coalesce.go b/coalesce.go new file mode 100644 index 0000000..ef68f95 --- /dev/null +++ b/coalesce.go @@ -0,0 +1,46 @@ +package main + +import ( + "time" +) + +const ( + coalesceWorkerRescanEvery time.Duration = 10 * time.Second + coalesceWorkerExpireAfter time.Duration = 20 * time.Second +) + +func coalesceKey(hubNick, message string) string { + return hubNick + "\x00" + message +} + +func (this *NTFServer) coalesceWorker() { + for { + time.Sleep(coalesceWorkerRescanEvery) + + deadLine := time.Now().Add(-coalesceWorkerExpireAfter) + + this.coalesceBufferMut.Lock() + for k, v := range this.coalesceBuffer { + if v.Before(deadLine) { + delete(this.coalesceBuffer, k) + } + } + this.coalesceBufferMut.Unlock() + } +} + +// Coalesce returns true if the message sticks to an existing one. +// It adds it into the coalesce buffer anyway. +func (this *NTFServer) Coalesce(hubNick, message string) bool { + ckey := coalesceKey(hubNick, message) + + this.coalesceBufferMut.Lock() + if _, ok := this.coalesceBuffer[ckey]; ok { + this.coalesceBufferMut.Unlock() + return true // stuck to existing + } else { + this.coalesceBuffer[ckey] = time.Now() + this.coalesceBufferMut.Unlock() + return false // first we've seen it + } +}