From 7ccab36f6a6e3038d94ef11b60d645b1de442feb Mon Sep 17 00:00:00 2001 From: Dirk McCormick Date: Thu, 13 Feb 2020 12:01:52 -0500 Subject: [PATCH] refactor: adjust message queue debounce limits --- internal/messagequeue/messagequeue.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/internal/messagequeue/messagequeue.go b/internal/messagequeue/messagequeue.go index 4610a95b..e60d52c3 100644 --- a/internal/messagequeue/messagequeue.go +++ b/internal/messagequeue/messagequeue.go @@ -32,11 +32,11 @@ const ( maxPriority = math.MaxInt32 // sendMessageDebounce is the debounce duration when calling sendMessage() sendMessageDebounce = time.Millisecond - // when we reach sendMessaageCuttoff wants/cancels, we'll send the message immediately. - sendMessageCuttoff = 100 + // when we reach sendMessageCutoff wants/cancels, we'll send the message immediately. + sendMessageCutoff = 256 // when we debounce for more than sendMessageMaxDelay, we'll send the // message immediately. - sendMessageMaxDelay = 100 * time.Millisecond + sendMessageMaxDelay = 20 * time.Millisecond ) // MessageNetwork is any network that can connect peers and generate a message @@ -286,6 +286,8 @@ func (mq *MessageQueue) runQueue() { // Create a timer for debouncing scheduled work. scheduleWork := time.NewTimer(0) if !scheduleWork.Stop() { + // Need to drain the timer if Stop() returns false + // See: https://golang.org/pkg/time/#Timer.Stop <-scheduleWork.C } @@ -302,12 +304,13 @@ func (mq *MessageQueue) runQueue() { if workScheduled.IsZero() { workScheduled = when } else if !scheduleWork.Stop() { + // Need to drain the timer if Stop() returns false <-scheduleWork.C } // If we have too many updates and/or we've waited too // long, send immediately. - if mq.pendingWorkCount() > sendMessageCuttoff || + if mq.pendingWorkCount() > sendMessageCutoff || time.Since(workScheduled) >= sendMessageMaxDelay { mq.sendIfReady() workScheduled = time.Time{}