mirror of
https://github.com/netbirdio/netbird.git
synced 2026-10-08 22:49:10 +02:00
adaptive debounce
This commit is contained in:
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user