Skip to content

Commit 5d79d2f

Browse files
authored
Reintroduce pkg/util/limiter/rate_limiter.go, in place of dskit/limiter (#4615)
Signed-off-by: Arve Knudsen <[email protected]>
1 parent 66df887 commit 5d79d2f

File tree

6 files changed

+132
-4
lines changed

6 files changed

+132
-4
lines changed

pkg/distributor/distributor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111

1212
"github.com/go-kit/log"
1313
"github.com/go-kit/log/level"
14-
"github.com/grafana/dskit/limiter"
1514
"github.com/grafana/dskit/services"
1615
"github.com/opentracing/opentracing-go"
1716
"github.com/pkg/errors"
@@ -34,6 +33,7 @@ import (
3433
"github.com/cortexproject/cortex/pkg/tenant"
3534
"github.com/cortexproject/cortex/pkg/util"
3635
"github.com/cortexproject/cortex/pkg/util/extract"
36+
"github.com/cortexproject/cortex/pkg/util/limiter"
3737
util_log "github.com/cortexproject/cortex/pkg/util/log"
3838
util_math "github.com/cortexproject/cortex/pkg/util/math"
3939
"github.com/cortexproject/cortex/pkg/util/validation"

pkg/distributor/ingestion_rate_strategy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package distributor
22

33
import (
4-
"github.com/grafana/dskit/limiter"
54
"golang.org/x/time/rate"
65

6+
"github.com/cortexproject/cortex/pkg/util/limiter"
77
"github.com/cortexproject/cortex/pkg/util/validation"
88
)
99

pkg/distributor/ingestion_rate_strategy_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ package distributor
33
import (
44
"testing"
55

6-
"github.com/grafana/dskit/limiter"
76
"github.com/stretchr/testify/assert"
87
"github.com/stretchr/testify/mock"
98
"github.com/stretchr/testify/require"
109
"golang.org/x/time/rate"
1110

11+
"github.com/cortexproject/cortex/pkg/util/limiter"
1212
"github.com/cortexproject/cortex/pkg/util/validation"
1313
)
1414

pkg/util/limiter/rate_limiter_test.go

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package limiter
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/stretchr/testify/assert"
8+
"golang.org/x/time/rate"
9+
)
10+
11+
func TestRateLimiter_RecheckPeriod(t *testing.T) {
12+
strategy := &increasingLimitStrategy{}
13+
limiter := NewRateLimiter(strategy, 10*time.Second)
14+
now := time.Now()
15+
16+
// Since the strategy increases the limit and burst value each time
17+
// the strategy functions are called, we do assert if the recheck
18+
// period is honored increasing the input time
19+
assert.Equal(t, float64(1), limiter.Limit(now, "test"))
20+
assert.Equal(t, 1, limiter.Burst(now, "test"))
21+
22+
assert.Equal(t, float64(1), limiter.Limit(now.Add(9*time.Second), "test"))
23+
assert.Equal(t, 1, limiter.Burst(now.Add(9*time.Second), "test"))
24+
25+
assert.Equal(t, float64(2), limiter.Limit(now.Add(10*time.Second), "test"))
26+
assert.Equal(t, 2, limiter.Burst(now.Add(10*time.Second), "test"))
27+
28+
assert.Equal(t, float64(2), limiter.Limit(now.Add(19*time.Second), "test"))
29+
assert.Equal(t, 2, limiter.Burst(now.Add(19*time.Second), "test"))
30+
31+
assert.Equal(t, float64(3), limiter.Limit(now.Add(20*time.Second), "test"))
32+
assert.Equal(t, 3, limiter.Burst(now.Add(20*time.Second), "test"))
33+
}
34+
35+
func TestRateLimiter_AllowN(t *testing.T) {
36+
strategy := &staticLimitStrategy{tenants: map[string]struct {
37+
limit float64
38+
burst int
39+
}{
40+
"tenant-1": {limit: 10, burst: 20},
41+
"tenant-2": {limit: 20, burst: 40},
42+
}}
43+
44+
limiter := NewRateLimiter(strategy, 10*time.Second)
45+
now := time.Now()
46+
47+
// Tenant #1
48+
assert.Equal(t, true, limiter.AllowN(now, "tenant-1", 8))
49+
assert.Equal(t, true, limiter.AllowN(now, "tenant-1", 10))
50+
assert.Equal(t, false, limiter.AllowN(now, "tenant-1", 3))
51+
assert.Equal(t, true, limiter.AllowN(now, "tenant-1", 2))
52+
53+
assert.Equal(t, true, limiter.AllowN(now.Add(time.Second), "tenant-1", 8))
54+
assert.Equal(t, false, limiter.AllowN(now.Add(time.Second), "tenant-1", 3))
55+
assert.Equal(t, true, limiter.AllowN(now.Add(time.Second), "tenant-1", 2))
56+
57+
// Tenant #2
58+
assert.Equal(t, true, limiter.AllowN(now, "tenant-2", 18))
59+
assert.Equal(t, true, limiter.AllowN(now, "tenant-2", 20))
60+
assert.Equal(t, false, limiter.AllowN(now, "tenant-2", 3))
61+
assert.Equal(t, true, limiter.AllowN(now, "tenant-2", 2))
62+
63+
assert.Equal(t, true, limiter.AllowN(now.Add(time.Second), "tenant-2", 18))
64+
assert.Equal(t, false, limiter.AllowN(now.Add(time.Second), "tenant-2", 3))
65+
assert.Equal(t, true, limiter.AllowN(now.Add(time.Second), "tenant-2", 2))
66+
}
67+
68+
func BenchmarkRateLimiter_CustomMultiTenant(b *testing.B) {
69+
strategy := &increasingLimitStrategy{}
70+
limiter := NewRateLimiter(strategy, 10*time.Second)
71+
now := time.Now()
72+
73+
b.ResetTimer()
74+
75+
for i := 0; i < b.N; i++ {
76+
limiter.AllowN(now, "test", 1)
77+
}
78+
}
79+
80+
func BenchmarkRateLimiter_OriginalSingleTenant(b *testing.B) {
81+
limiter := rate.NewLimiter(rate.Limit(1), 1)
82+
now := time.Now()
83+
84+
b.ResetTimer()
85+
86+
for i := 0; i < b.N; i++ {
87+
limiter.AllowN(now, 1)
88+
}
89+
}
90+
91+
type increasingLimitStrategy struct {
92+
limit float64
93+
burst int
94+
}
95+
96+
func (s *increasingLimitStrategy) Limit(tenantID string) float64 {
97+
s.limit++
98+
return s.limit
99+
}
100+
101+
func (s *increasingLimitStrategy) Burst(tenantID string) int {
102+
s.burst++
103+
return s.burst
104+
}
105+
106+
type staticLimitStrategy struct {
107+
tenants map[string]struct {
108+
limit float64
109+
burst int
110+
}
111+
}
112+
113+
func (s *staticLimitStrategy) Limit(tenantID string) float64 {
114+
tenant, ok := s.tenants[tenantID]
115+
if !ok {
116+
return 0
117+
}
118+
119+
return tenant.limit
120+
}
121+
122+
func (s *staticLimitStrategy) Burst(tenantID string) int {
123+
tenant, ok := s.tenants[tenantID]
124+
if !ok {
125+
return 0
126+
}
127+
128+
return tenant.burst
129+
}

vendor/modules.txt

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)