adaptive debounce

This commit is contained in:
Pascal Fischer
2025-10-08 22:20:27 +02:00
parent 2dc5e7eb7a
commit 3984f0ee08
2 changed files with 79 additions and 19 deletions
+26 -8
View File
@@ -3,16 +3,19 @@ package server
import (
"context"
"sync"
"time"
"github.com/netbirdio/netbird/management/server/telemetry"
)
type UpdateBuffer struct {
mu sync.Mutex
cond *sync.Cond
update *UpdateMessage
closed bool
metrics *telemetry.UpdateChannelMetrics
mu sync.Mutex
cond *sync.Cond
update *UpdateMessage
closed bool
metrics *telemetry.UpdateChannelMetrics
overwriteCount int // Number of overwrites since last Pop
lastPopTime time.Time // Time of last Pop
}
func NewUpdateBuffer(metrics *telemetry.UpdateChannelMetrics) *UpdateBuffer {
@@ -30,6 +33,7 @@ func (b *UpdateBuffer) Push(update *UpdateMessage) {
b.metrics.CountBufferPush()
} else {
b.metrics.CountBufferOverwrite()
b.overwriteCount++
}
b.update = update
@@ -41,7 +45,7 @@ func (b *UpdateBuffer) Push(update *UpdateMessage) {
b.metrics.CountBufferIgnore()
}
func (b *UpdateBuffer) Pop(ctx context.Context) (*UpdateMessage, bool) {
func (b *UpdateBuffer) Pop(ctx context.Context) (*UpdateMessage, int, time.Duration, bool) {
b.mu.Lock()
defer b.mu.Unlock()
@@ -60,11 +64,25 @@ func (b *UpdateBuffer) Pop(ctx context.Context) (*UpdateMessage, bool) {
}
if b.closed {
return nil, false
return nil, 0, 0, false
}
msg := b.update
overwrites := b.overwriteCount
// Calculate time since last pop
now := time.Now()
var timeSinceLastPop time.Duration
if !b.lastPopTime.IsZero() {
timeSinceLastPop = now.Sub(b.lastPopTime)
}
// Reset counters
b.update = nil
return msg, true
b.overwriteCount = 0
b.lastPopTime = now
return msg, overwrites, timeSinceLastPop, true
}
func (b *UpdateBuffer) Close() {