MSP-3362 implement rate limiter
This commit is contained in:
1 parent
e3f8a4e8bf
commit
19211955b2
4 files changed
+101
No files matched your search
@@ -0,0 +1,23 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"isp-lock-service/domain"
|
||||
)
|
||||
|
||||
type limiterService interface {
|
||||
Limit(ctx context.Context, req domain.RateLimiterRequest) (*domain.RateLimiterResponse, error)
|
||||
}
|
||||
|
||||
type RateLimiter struct {
|
||||
svc limiterService
|
||||
}
|
||||
|
||||
func NewRateLimiter(svc limiterService) RateLimiter {
|
||||
return RateLimiter{svc: svc}
|
||||
}
|
||||
|
||||
func (c RateLimiter) Limit(ctx context.Context, req domain.RateLimiterRequest) (*domain.RateLimiterResponse, error) {
|
||||
return c.svc.Limit(ctx, req)
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type RateLimiterRequest struct {
|
||||
Key string `validate:"required"`
|
||||
MaxRps int `validate:"required"`
|
||||
}
|
||||
|
||||
type RateLimiterResponse struct {
|
||||
Allow bool
|
||||
Remaining int
|
||||
RetryAfter time.Duration
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/go-redis/redis_rate/v10"
|
||||
"github.com/pkg/errors"
|
||||
goredislib "github.com/redis/go-redis/v9"
|
||||
"github.com/txix-open/isp-kit/log"
|
||||
"isp-lock-service/conf"
|
||||
"isp-lock-service/domain"
|
||||
)
|
||||
|
||||
type rateLimiter struct {
|
||||
logger log.Logger
|
||||
cli *redis_rate.Limiter
|
||||
prefix string
|
||||
}
|
||||
|
||||
func NewRateLimiter(logger log.Logger, cli goredislib.UniversalClient, cfg conf.Redis) rateLimiter {
|
||||
return rateLimiter{
|
||||
logger: logger,
|
||||
cli: redis_rate.NewLimiter(cli),
|
||||
prefix: cfg.Prefix + "::rate-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))
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "allow request")
|
||||
}
|
||||
|
||||
return &domain.RateLimiterResponse{
|
||||
Allow: res.Allowed > 0,
|
||||
Remaining: res.Remaining,
|
||||
RetryAfter: res.RetryAfter,
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"isp-lock-service/domain"
|
||||
)
|
||||
|
||||
type limiterRepo interface {
|
||||
Limit(ctx context.Context, key string, maxRps int) (*domain.RateLimiterResponse, error)
|
||||
}
|
||||
|
||||
type rateLimiter struct {
|
||||
repo limiterRepo
|
||||
}
|
||||
|
||||
func NewRateLimiter(repo limiterRepo) rateLimiter {
|
||||
return rateLimiter{repo: repo}
|
||||
}
|
||||
|
||||
func (s rateLimiter) Limit(ctx context.Context, req domain.RateLimiterRequest) (*domain.RateLimiterResponse, error) {
|
||||
return s.repo.Limit(ctx, req.Key, req.MaxRps) // nolint:wrapcheck
|
||||
}
|
||||
Reference in new issue
Block a user