Commit 7ccab36f authored by Dirk McCormick's avatar Dirk McCormick

refactor: adjust message queue debounce limits

parent 777c0d9a
...@@ -32,11 +32,11 @@ const ( ...@@ -32,11 +32,11 @@ const (
maxPriority = math.MaxInt32 maxPriority = math.MaxInt32
// sendMessageDebounce is the debounce duration when calling sendMessage() // sendMessageDebounce is the debounce duration when calling sendMessage()
sendMessageDebounce = time.Millisecond sendMessageDebounce = time.Millisecond
// when we reach sendMessaageCuttoff wants/cancels, we'll send the message immediately. // when we reach sendMessageCutoff wants/cancels, we'll send the message immediately.
sendMessageCuttoff = 100 sendMessageCutoff = 256
// when we debounce for more than sendMessageMaxDelay, we'll send the // when we debounce for more than sendMessageMaxDelay, we'll send the
// message immediately. // message immediately.
sendMessageMaxDelay = 100 * time.Millisecond sendMessageMaxDelay = 20 * time.Millisecond
) )
// MessageNetwork is any network that can connect peers and generate a message // MessageNetwork is any network that can connect peers and generate a message
...@@ -286,6 +286,8 @@ func (mq *MessageQueue) runQueue() { ...@@ -286,6 +286,8 @@ func (mq *MessageQueue) runQueue() {
// Create a timer for debouncing scheduled work. // Create a timer for debouncing scheduled work.
scheduleWork := time.NewTimer(0) scheduleWork := time.NewTimer(0)
if !scheduleWork.Stop() { if !scheduleWork.Stop() {
// Need to drain the timer if Stop() returns false
// See: https://golang.org/pkg/time/#Timer.Stop
<-scheduleWork.C <-scheduleWork.C
} }
...@@ -302,12 +304,13 @@ func (mq *MessageQueue) runQueue() { ...@@ -302,12 +304,13 @@ func (mq *MessageQueue) runQueue() {
if workScheduled.IsZero() { if workScheduled.IsZero() {
workScheduled = when workScheduled = when
} else if !scheduleWork.Stop() { } else if !scheduleWork.Stop() {
// Need to drain the timer if Stop() returns false
<-scheduleWork.C <-scheduleWork.C
} }
// If we have too many updates and/or we've waited too // If we have too many updates and/or we've waited too
// long, send immediately. // long, send immediately.
if mq.pendingWorkCount() > sendMessageCuttoff || if mq.pendingWorkCount() > sendMessageCutoff ||
time.Since(workScheduled) >= sendMessageMaxDelay { time.Since(workScheduled) >= sendMessageMaxDelay {
mq.sendIfReady() mq.sendIfReady()
workScheduled = time.Time{} workScheduled = time.Time{}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment