Fix var leaky bucket initialization for bucket manager

in bucket manager we never call NewVarBucket() so fields are not correct
This commit is contained in:
Taras Kushnir
2025-08-25 08:16:24 +03:00
parent b9023439a5
commit 5657fa3158
+9 -5
View File
@@ -108,15 +108,19 @@ type VarLeakyBucket[TKey comparable] struct {
}
func NewVarBucket[TKey comparable](key TKey, capacity TLevel, leakInterval time.Duration, t time.Time) *VarLeakyBucket[TKey] {
b := &VarLeakyBucket[TKey]{
leakRate: 1.0, // to start like the ConstLeakyBucket, we leak 1 level per leakInterval
pendingSum: 0,
count: 1, // this is needed to have "previous" empty bucket for averages
}
b := &VarLeakyBucket[TKey]{}
b.Init(key, capacity, leakInterval, t)
return b
}
func (lb *VarLeakyBucket[TKey]) Init(key TKey, capacity TLevel, leakInterval time.Duration, tnow time.Time) {
lb.ConstLeakyBucket.Init(key, capacity, leakInterval, tnow)
lb.leakRate = 1.0 // to start like the ConstLeakyBucket, we leak 1 level per leakInterval
lb.pendingSum = 0
lb.count = 1 // this is needed to have "previous" empty bucket for averages
}
func (lb *VarLeakyBucket[TKey]) LeakInterval() time.Duration {
nanoseconds := float64(lb.leakInterval.Nanoseconds()) / lb.leakRate
return time.Duration(nanoseconds) * time.Nanosecond