Skip to content

Commit 4f6fb1a

Browse files
Merge pull request #22 from platinummonkey/backport-upstream-changes
backport Netflix/concurrency-limits#153
2 parents 766b864 + 8446a1c commit 4f6fb1a

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

limit/aimd.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
type AIMDLimit struct {
1313
name string
1414
limit int
15+
increaseBy int
1516
backOffRatio float64
1617

1718
listeners []core.LimitChangeListener
@@ -27,25 +28,30 @@ func NewDefaultAIMLimit(
2728
registry core.MetricRegistry,
2829
tags ...string,
2930
) *AIMDLimit {
30-
return NewAIMDLimit(name, 10, 0.9, registry, tags...)
31+
return NewAIMDLimit(name, 10, 0.9, 1, registry, tags...)
3132
}
3233

3334
// NewAIMDLimit will create a new AIMDLimit.
3435
func NewAIMDLimit(
3536
name string,
3637
initialLimit int,
3738
backOffRatio float64,
39+
increaseBy int,
3840
registry core.MetricRegistry,
3941
tags ...string,
4042
) *AIMDLimit {
4143
if registry == nil {
4244
registry = core.EmptyMetricRegistryInstance
4345
}
46+
if increaseBy <= 0 {
47+
increaseBy = 1
48+
}
4449

4550
l := &AIMDLimit{
4651
name: name,
4752
limit: initialLimit,
4853
backOffRatio: backOffRatio,
54+
increaseBy: increaseBy,
4955
listeners: make([]core.LimitChangeListener, 0),
5056
registry: registry,
5157
}
@@ -85,7 +91,7 @@ func (l *AIMDLimit) OnSample(startTime int64, rtt int64, inFlight int, didDrop b
8591
l.limit = int(math.Max(1, math.Min(float64(l.limit-1), float64(int(float64(l.limit)*l.backOffRatio)))))
8692
l.notifyListeners(l.limit)
8793
} else if inFlight >= l.limit {
88-
l.limit++
94+
l.limit += l.increaseBy
8995
l.notifyListeners(l.limit)
9096
}
9197
return

limit/aimd_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ func TestAIMDLimit(t *testing.T) {
2020
t.Run("Default", func(t2 *testing.T) {
2121
t2.Parallel()
2222
asrt := assert.New(t2)
23-
l := NewAIMDLimit("test", 10, 0.9, nil)
23+
l := NewAIMDLimit("test", 10, 0.9, 1, nil)
2424
asrt.Equal(10, l.EstimatedLimit())
2525
asrt.Equal(0.9, l.BackOffRatio())
2626
})
2727

2828
t.Run("IncreaseOnSuccess", func(t2 *testing.T) {
2929
t2.Parallel()
3030
asrt := assert.New(t2)
31-
l := NewAIMDLimit("test", 10, 0.9, nil)
31+
l := NewAIMDLimit("test", 10, 0.9, 1, nil)
3232
listener := testNotifyListener{changes: make([]int, 0)}
3333
l.NotifyOnChange(listener.updater())
3434
l.OnSample(-1, (time.Millisecond * 1).Nanoseconds(), 10, false)
@@ -39,15 +39,15 @@ func TestAIMDLimit(t *testing.T) {
3939
t.Run("DecreaseOnDrops", func(t2 *testing.T) {
4040
t2.Parallel()
4141
asrt := assert.New(t2)
42-
l := NewAIMDLimit("test", 10, 0.9, nil)
42+
l := NewAIMDLimit("test", 10, 0.9, 1, nil)
4343
l.OnSample(-1, 1, 1, true)
4444
asrt.Equal(9, l.EstimatedLimit())
4545
})
4646

4747
t.Run("String", func(t2 *testing.T) {
4848
t2.Parallel()
4949
asrt := assert.New(t2)
50-
l := NewAIMDLimit("test", 10, 0.9, nil)
50+
l := NewAIMDLimit("test", 10, 0.9, 1, nil)
5151
asrt.Equal("AIMDLimit{limit=10, backOffRatio=0.9000}", l.String())
5252
})
5353
}

0 commit comments

Comments
 (0)