Skip to content

tailFile: leakybucket algo err #95

Open
@RuiHan2023

Description

@RuiHan2023

https://github.com/hpcloud/tail/blob/master/ratelimiter/leakybucket.go

newfill float64 = b.Fill + float64(amount)
b.Fill -= float64(elapsed) / float64(b.LeakInterval)
It is strange: add amount, dec time ?!

should be:


--- a/src/github.com/hpcloud/tail/ratelimiter/leakybucket.go
+++ b/src/github.com/hpcloud/tail/ratelimiter/leakybucket.go
@@ -30,7 +30,8 @@ func (b *LeakyBucket) updateFill() {
        if b.Fill > 0 {
                elapsed := now.Sub(b.Lastupdate)

-               b.Fill -= float64(elapsed) / float64(b.LeakInterval)
+               // fix bug: bucket algo err
+               b.Fill -= float64(b.Size) * float64(elapsed) / float64(b.LeakInterval)
                if b.Fill < 0 {
                        b.Fill = 0
                }
@@ -40,7 +41,6 @@ func (b *LeakyBucket) updateFill() {

 func (b *LeakyBucket) Pour(amount uint16) bool {
        b.updateFill()
-
        var newfill float64 = b.Fill + float64(amount)

        if newfill > float64(b.Size) {
@@ -54,7 +54,7 @@ func (b *LeakyBucket) Pour(amount uint16) bool {

 // The time at which this bucket will be completely drained
 func (b *LeakyBucket) DrainedAt() time.Time {
-       return b.Lastupdate.Add(time.Duration(b.Fill * float64(b.LeakInterval)))
+       return b.Lastupdate.Add(time.Duration(b.Fill / float64(b.Size) * float64(b.LeakInterval)))
 }


this leakybucket's rate: size/interval
burst: size

please see: https://en.wikipedia.org/wiki/Leaky_bucket#Parameters

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions