|
| 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 | +} |
0 commit comments