From d87a696b356b85fd0487307c940aae6a5d6d48e9 Mon Sep 17 00:00:00 2001 From: Cody Lee Date: Tue, 10 Dec 2019 10:50:04 -0600 Subject: [PATCH 1/2] backport https://github.com/Netflix/concurrency-limits/pull/153 --- limit/aimd.go | 8 +++++++- limit/aimd_test.go | 8 ++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/limit/aimd.go b/limit/aimd.go index 352ec34..da12f9c 100644 --- a/limit/aimd.go +++ b/limit/aimd.go @@ -12,6 +12,7 @@ import ( type AIMDLimit struct { name string limit int + increaseBy int backOffRatio float64 listeners []core.LimitChangeListener @@ -35,17 +36,22 @@ 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, } @@ -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 diff --git a/limit/aimd_test.go b/limit/aimd_test.go index bf76dd1..594e873 100644 --- a/limit/aimd_test.go +++ b/limit/aimd_test.go @@ -20,7 +20,7 @@ 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()) }) @@ -28,7 +28,7 @@ func TestAIMDLimit(t *testing.T) { 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) @@ -39,7 +39,7 @@ 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()) }) @@ -47,7 +47,7 @@ func TestAIMDLimit(t *testing.T) { 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()) }) } From 8446a1c935a33ba8a90c858c8ee572e630c51e78 Mon Sep 17 00:00:00 2001 From: Cody Lee Date: Tue, 10 Dec 2019 11:04:18 -0600 Subject: [PATCH 2/2] fix vet --- limit/aimd.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/limit/aimd.go b/limit/aimd.go index da12f9c..975df4b 100644 --- a/limit/aimd.go +++ b/limit/aimd.go @@ -28,7 +28,7 @@ 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.