Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f06c0abcaa |
No files matched your search
@@ -1,3 +1,5 @@
|
||||
# v1.7.0
|
||||
* Добавлена возможность использования inMemRateLimiter с RPS<1
|
||||
# v1.6.1
|
||||
* Обновлены зависимости
|
||||
# v1.6.0
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
|
||||
type limiterService interface {
|
||||
Limit(ctx context.Context, req domain.RateLimiterRequest) (*domain.RateLimiterResponse, error)
|
||||
LimitInMem(ctx context.Context, req domain.RateLimiterRequest) (*domain.InMemRateLimiterResponse, error)
|
||||
LimitInMem(ctx context.Context, req domain.RateLimiterInMemRequest) (*domain.RateLimiterInMemResponse, error)
|
||||
}
|
||||
|
||||
type RateLimiter struct {
|
||||
@@ -23,6 +23,6 @@ func (c RateLimiter) Limit(ctx context.Context, req domain.RateLimiterRequest) (
|
||||
return c.svc.Limit(ctx, req)
|
||||
}
|
||||
|
||||
func (c RateLimiter) LimitInMem(ctx context.Context, req domain.RateLimiterRequest) (*domain.InMemRateLimiterResponse, error) {
|
||||
func (c RateLimiter) LimitInMem(ctx context.Context, req domain.RateLimiterInMemRequest) (*domain.RateLimiterInMemResponse, error) {
|
||||
return c.svc.LimitInMem(ctx, req)
|
||||
}
|
||||
@@ -9,12 +9,17 @@ type RateLimiterRequest struct {
|
||||
MaxRps int `validate:"required"`
|
||||
}
|
||||
|
||||
type RateLimiterInMemRequest struct {
|
||||
Key string `validate:"required"`
|
||||
MaxRps float64 `validate:"required"`
|
||||
}
|
||||
|
||||
type RateLimiterResponse struct {
|
||||
Allow bool
|
||||
Remaining int
|
||||
RetryAfter time.Duration
|
||||
}
|
||||
|
||||
type InMemRateLimiterResponse struct {
|
||||
type RateLimiterInMemResponse struct {
|
||||
PassAfter time.Duration
|
||||
}
|
||||
@@ -60,14 +60,17 @@ func (r *RateLimiter) Limit(ctx context.Context, key string, maxRps int) (*domai
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (r *RateLimiter) LimitInMem(_ context.Context, key string, maxRps int) (*domain.InMemRateLimiterResponse, error) {
|
||||
key = fmt.Sprintf("%s:::%d", key, maxRps)
|
||||
func (r *RateLimiter) LimitInMem(_ context.Context, key string, maxRps float64) (*domain.RateLimiterInMemResponse, error) {
|
||||
key = fmt.Sprintf("%s:::%0.5f", key, maxRps)
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
|
||||
lim, ok := r.inMemLimiters[key]
|
||||
if !ok {
|
||||
lim = &limiter{Limiter: rate.NewLimiter(rate.Limit(maxRps), 1)}
|
||||
if maxRps < 1 {
|
||||
lim.ReserveN(time.Now(), 1)
|
||||
}
|
||||
r.inMemLimiters[key] = lim
|
||||
}
|
||||
lim.lastUse = time.Now()
|
||||
@@ -77,7 +80,7 @@ func (r *RateLimiter) LimitInMem(_ context.Context, key string, maxRps int) (*do
|
||||
return nil, errors.Errorf("can't reserve time; key='%s'", key)
|
||||
}
|
||||
|
||||
return &domain.InMemRateLimiterResponse{
|
||||
return &domain.RateLimiterInMemResponse{
|
||||
PassAfter: res.DelayFrom(lim.lastUse),
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
|
||||
type limiterRepo interface {
|
||||
Limit(ctx context.Context, key string, maxRps int) (*domain.RateLimiterResponse, error)
|
||||
LimitInMem(ctx context.Context, key string, maxRps int) (*domain.InMemRateLimiterResponse, error)
|
||||
LimitInMem(ctx context.Context, key string, maxRps float64) (*domain.RateLimiterInMemResponse, error)
|
||||
}
|
||||
|
||||
type rateLimiter struct {
|
||||
@@ -23,6 +23,6 @@ func (s rateLimiter) Limit(ctx context.Context, req domain.RateLimiterRequest) (
|
||||
return s.repo.Limit(ctx, req.Key, req.MaxRps) // nolint:wrapcheck
|
||||
}
|
||||
|
||||
func (s rateLimiter) LimitInMem(ctx context.Context, req domain.RateLimiterRequest) (*domain.InMemRateLimiterResponse, error) {
|
||||
func (s rateLimiter) LimitInMem(ctx context.Context, req domain.RateLimiterInMemRequest) (*domain.RateLimiterInMemResponse, error) {
|
||||
return s.repo.LimitInMem(ctx, req.Key, req.MaxRps) // nolint:wrapcheck
|
||||
}
|
||||
@@ -91,7 +91,7 @@ func TestRateLimiterInMem(t *testing.T) {
|
||||
ctx = t.Context()
|
||||
)
|
||||
for i := range 2 * maxRps {
|
||||
resp, err := r.LimitInMem(ctx, key, maxRps)
|
||||
resp, err := r.LimitInMem(ctx, key, float64(maxRps))
|
||||
required.NoError(err)
|
||||
expPassAfter := reqInterval * time.Duration(i)
|
||||
required.True(resp.PassAfter <= expPassAfter)
|
||||
@@ -105,6 +105,31 @@ func TestRateLimiterInMem(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRateLimiterInMemLowRPSCase(t *testing.T) {
|
||||
t.Parallel()
|
||||
tst, required := test.New(t)
|
||||
r := repository.NewRateLimiter(tst.Logger(), nil, conf.Remote{
|
||||
InMemLimiter: conf.InMemLimiter{
|
||||
ClearPeriodInSec: 10,
|
||||
LastUseThresholdInSec: 10,
|
||||
},
|
||||
})
|
||||
tst.T().Cleanup(r.Close)
|
||||
|
||||
var (
|
||||
maxRps = 0.5
|
||||
reqInterval = time.Duration(float64(time.Second) / maxRps)
|
||||
key = "key"
|
||||
ctx = t.Context()
|
||||
)
|
||||
for i := range 3 {
|
||||
resp, err := r.LimitInMem(ctx, key, maxRps)
|
||||
required.NoError(err)
|
||||
expPassAfter := reqInterval * time.Duration(i+1)
|
||||
required.True(resp.PassAfter <= expPassAfter)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkRateLimiter(b *testing.B) {
|
||||
tst, _ := test.New(new(testing.T))
|
||||
redis := NewRedis(tst)
|
||||
@@ -135,7 +160,7 @@ func BenchmarkRateLimiter(b *testing.B) {
|
||||
|
||||
b.Run("LimitInMem", func(b *testing.B) {
|
||||
for range b.N {
|
||||
_, err := r.LimitInMem(ctx, key, maxRps)
|
||||
_, err := r.LimitInMem(ctx, key, float64(maxRps))
|
||||
if err != nil {
|
||||
b.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
@@ -181,7 +206,7 @@ func BenchmarkRateLimiter2(b *testing.B) {
|
||||
for i := range 100 {
|
||||
maxRps := i + 1
|
||||
for _, key := range keys {
|
||||
_, err := r.LimitInMem(ctx, key, maxRps)
|
||||
_, err := r.LimitInMem(ctx, key, float64(maxRps))
|
||||
if err != nil {
|
||||
b.Fatalf("unexpected error for maxRps %d and key %s: %v", maxRps, key, err)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user