LAD-1276 add period and burst for rateLimiter

This commit is contained in:
Пучков Андрей Павлович committed 2026-10-09 14:49:27 +03:00
1 parent d1c78cecf1
commit 3ff40fda83
4 files changed
+68 -12

No files matched your search

+2
View File
@@ -7,6 +7,8 @@ import (
type RateLimiterRequest struct {
Key string `validate:"required"`
MaxRps int `validate:"required"`
PeriodInSec int
Burst int
}
type RateLimiterInMemRequest struct {
+16 -2
View File
@@ -48,8 +48,22 @@ func NewRateLimiter(logger log.Logger, cli goredislib.UniversalClient, cfg conf.
return limiter
}
func (r *RateLimiter) Limit(ctx context.Context, key string, maxRps int) (*domain.RateLimiterResponse, error) {
res, err := r.cli.Allow(ctx, makeKey(r.prefix, key), redis_rate.PerSecond(maxRps))
func (r *RateLimiter) Limit(ctx context.Context, req domain.RateLimiterRequest) (*domain.RateLimiterResponse, error) {
period := time.Second
if req.PeriodInSec > 0 {
period = time.Duration(req.PeriodInSec) * time.Second
}
burst := req.Burst
if burst <= 0 {
burst = req.MaxRps
}
limit := redis_rate.Limit{
Rate: req.MaxRps,
Period: period,
Burst: burst,
}
res, err := r.cli.Allow(ctx, makeKey(r.prefix, req.Key), limit)
if err != nil {
return nil, errors.WithMessage(err, "allow request")
}
+2 -2
View File
@@ -7,7 +7,7 @@ import (
)
type limiterRepo interface {
Limit(ctx context.Context, key string, maxRps int) (*domain.RateLimiterResponse, error)
Limit(ctx context.Context, req domain.RateLimiterRequest) (*domain.RateLimiterResponse, error)
LimitInMem(ctx context.Context, key string, maxRps float64, infiniteKey bool) (*domain.RateLimiterInMemResponse, error)
}
@@ -20,7 +20,7 @@ func NewRateLimiter(repo limiterRepo) rateLimiter {
}
func (s rateLimiter) Limit(ctx context.Context, req domain.RateLimiterRequest) (*domain.RateLimiterResponse, error) {
return s.repo.Limit(ctx, req.Key, req.MaxRps) // nolint:wrapcheck
return s.repo.Limit(ctx, req) // nolint:wrapcheck
}
func (s rateLimiter) LimitInMem(ctx context.Context, req domain.RateLimiterInMemRequest) (*domain.RateLimiterInMemResponse, error) {
+48 -8
View File
@@ -4,11 +4,12 @@ import (
"testing"
"time"
"github.com/txix-open/isp-kit/test"
"github.com/txix-open/isp-kit/test/fake"
"isp-lock-service/conf"
"isp-lock-service/domain"
"isp-lock-service/repository"
"github.com/txix-open/isp-kit/test"
"github.com/txix-open/isp-kit/test/fake"
)
func TestRateLimiter(t *testing.T) {
@@ -30,7 +31,7 @@ func TestRateLimiter(t *testing.T) {
ctx = t.Context()
)
for i := range maxRps {
resp, err := r.Limit(ctx, key, maxRps)
resp, err := r.Limit(ctx, domain.RateLimiterRequest{Key: key, MaxRps: maxRps})
required.NoError(err)
exp := &domain.RateLimiterResponse{
Allow: true,
@@ -40,7 +41,7 @@ func TestRateLimiter(t *testing.T) {
required.EqualValues(exp, resp)
}
resp, err := r.Limit(ctx, key, maxRps)
resp, err := r.Limit(ctx, domain.RateLimiterRequest{Key: key, MaxRps: maxRps})
required.NoError(err)
exp := &domain.RateLimiterResponse{
Allow: false,
@@ -51,7 +52,7 @@ func TestRateLimiter(t *testing.T) {
required.EqualValues(exp, resp)
time.Sleep(time.Second)
resp, err = r.Limit(ctx, key, maxRps)
resp, err = r.Limit(ctx, domain.RateLimiterRequest{Key: key, MaxRps: maxRps})
required.NoError(err)
exp = &domain.RateLimiterResponse{
Allow: true,
@@ -62,7 +63,7 @@ func TestRateLimiter(t *testing.T) {
for range 1000 {
key := fake.It[string]()
resp, err := r.Limit(ctx, key, 1)
resp, err := r.Limit(ctx, domain.RateLimiterRequest{Key: key, MaxRps: 1})
required.NoError(err)
exp := &domain.RateLimiterResponse{
Allow: true,
@@ -73,6 +74,45 @@ func TestRateLimiter(t *testing.T) {
}
}
func TestRateLimiterWithPeriod(t *testing.T) {
t.Parallel()
tst, required := test.New(t)
redis := NewRedis(tst)
r := repository.NewRateLimiter(tst.Logger(), redis, conf.Remote{
Redis: conf.Redis{Prefix: "test"},
InMemLimiter: conf.InMemLimiter{
ClearPeriodInSec: 10,
LastUseThresholdInSec: 10,
},
})
tst.T().Cleanup(r.Close)
var (
req = domain.RateLimiterRequest{
Key: fake.It[string](),
MaxRps: 1,
PeriodInSec: 60,
Burst: 1,
}
ctx = t.Context()
)
resp, err := r.Limit(ctx, req)
required.NoError(err)
required.True(resp.Allow)
resp, err = r.Limit(ctx, req)
required.NoError(err)
required.False(resp.Allow)
required.Greater(resp.RetryAfter, 59*time.Second)
firstRetryAfter := resp.RetryAfter
resp, err = r.Limit(ctx, req)
required.NoError(err)
required.False(resp.Allow)
required.LessOrEqual(resp.RetryAfter, firstRetryAfter)
}
func TestRateLimiterInMem(t *testing.T) {
t.Parallel()
tst, required := test.New(t)
@@ -186,7 +226,7 @@ func BenchmarkRateLimiter(b *testing.B) {
b.Run("Limit", func(b *testing.B) {
for range b.N {
_, err := r.Limit(ctx, key, maxRps)
_, err := r.Limit(ctx, domain.RateLimiterRequest{Key: key, MaxRps: maxRps})
if err != nil {
b.Fatalf("unexpected error: %v", err)
}
@@ -227,7 +267,7 @@ func BenchmarkRateLimiter2(b *testing.B) {
for i := range 100 {
maxRps := i + 1
for _, key := range keys {
_, err := r.Limit(ctx, key, maxRps)
_, err := r.Limit(ctx, domain.RateLimiterRequest{Key: key, MaxRps: maxRps})
if err != nil {
b.Fatalf("unexpected error for maxRps %d and key %s: %v", maxRps, key, err)
}