Skip to content

Commit

Permalink
Merge pull request #22 from platinummonkey/backport-upstream-changes
Browse files Browse the repository at this point in the history
  • Loading branch information
platinummonkey authored Dec 10, 2019
2 parents 766b864 + 8446a1c commit 4f6fb1a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
10 changes: 8 additions & 2 deletions limit/aimd.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
type AIMDLimit struct {
name string
limit int
increaseBy int
backOffRatio float64

listeners []core.LimitChangeListener
Expand All @@ -27,25 +28,30 @@ func NewDefaultAIMLimit(
registry core.MetricRegistry,
tags ...string,
) *AIMDLimit {
return NewAIMDLimit(name, 10, 0.9, registry, tags...)
return NewAIMDLimit(name, 10, 0.9, 1, registry, tags...)
}

// NewAIMDLimit will create a new AIMDLimit.
func NewAIMDLimit(
name string,
initialLimit int,
backOffRatio float64,
increaseBy int,
registry core.MetricRegistry,
tags ...string,
) *AIMDLimit {
if registry == nil {
registry = core.EmptyMetricRegistryInstance
}
if increaseBy <= 0 {
increaseBy = 1
}

l := &AIMDLimit{
name: name,
limit: initialLimit,
backOffRatio: backOffRatio,
increaseBy: increaseBy,
listeners: make([]core.LimitChangeListener, 0),
registry: registry,
}
Expand Down Expand Up @@ -85,7 +91,7 @@ func (l *AIMDLimit) OnSample(startTime int64, rtt int64, inFlight int, didDrop b
l.limit = int(math.Max(1, math.Min(float64(l.limit-1), float64(int(float64(l.limit)*l.backOffRatio)))))
l.notifyListeners(l.limit)
} else if inFlight >= l.limit {
l.limit++
l.limit += l.increaseBy
l.notifyListeners(l.limit)
}
return
Expand Down
8 changes: 4 additions & 4 deletions limit/aimd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ func TestAIMDLimit(t *testing.T) {
t.Run("Default", func(t2 *testing.T) {
t2.Parallel()
asrt := assert.New(t2)
l := NewAIMDLimit("test", 10, 0.9, nil)
l := NewAIMDLimit("test", 10, 0.9, 1, nil)
asrt.Equal(10, l.EstimatedLimit())
asrt.Equal(0.9, l.BackOffRatio())
})

t.Run("IncreaseOnSuccess", func(t2 *testing.T) {
t2.Parallel()
asrt := assert.New(t2)
l := NewAIMDLimit("test", 10, 0.9, nil)
l := NewAIMDLimit("test", 10, 0.9, 1, nil)
listener := testNotifyListener{changes: make([]int, 0)}
l.NotifyOnChange(listener.updater())
l.OnSample(-1, (time.Millisecond * 1).Nanoseconds(), 10, false)
Expand All @@ -39,15 +39,15 @@ func TestAIMDLimit(t *testing.T) {
t.Run("DecreaseOnDrops", func(t2 *testing.T) {
t2.Parallel()
asrt := assert.New(t2)
l := NewAIMDLimit("test", 10, 0.9, nil)
l := NewAIMDLimit("test", 10, 0.9, 1, nil)
l.OnSample(-1, 1, 1, true)
asrt.Equal(9, l.EstimatedLimit())
})

t.Run("String", func(t2 *testing.T) {
t2.Parallel()
asrt := assert.New(t2)
l := NewAIMDLimit("test", 10, 0.9, nil)
l := NewAIMDLimit("test", 10, 0.9, 1, nil)
asrt.Equal("AIMDLimit{limit=10, backOffRatio=0.9000}", l.String())
})
}

0 comments on commit 4f6fb1a

Please sign in to comment.