Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a2f3e1c1b6 | ||
|
|
820afbd774 |
No files matched your search
@@ -1,3 +1,7 @@
|
|||||||
|
# v1.4.1
|
||||||
|
* добавлена обработка ошибки при закрытии клиента `redis`
|
||||||
|
# v1.4.0
|
||||||
|
* добавлен метод `daily_limit/get` для проверки возможности отправки запроса пользователем
|
||||||
# v1.3.0
|
# v1.3.0
|
||||||
* добавлены методы `/daily_limit/increment` и `/daily_limit/set`
|
* добавлены методы `/daily_limit/increment` и `/daily_limit/set`
|
||||||
* удалена неиспользуемая структура `domain.GrpcError`
|
* удалена неиспользуемая структура `domain.GrpcError`
|
||||||
|
|||||||
@@ -83,7 +83,12 @@ func (a *Assembly) Closers() []app.Closer {
|
|||||||
return nil
|
return nil
|
||||||
}),
|
}),
|
||||||
app.CloserFunc(func() error {
|
app.CloserFunc(func() error {
|
||||||
_ = a.redisCli.Close()
|
if a.redisCli != nil {
|
||||||
|
err := a.redisCli.Close()
|
||||||
|
if err != nil {
|
||||||
|
return errors.WithMessage(err, "shutdown redis client")
|
||||||
|
}
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
type dailyLimiterService interface {
|
type dailyLimiterService interface {
|
||||||
Increment(ctx context.Context, req domain.IncrementRequest) (*domain.IncrementResponse, error)
|
Increment(ctx context.Context, req domain.IncrementRequest) (*domain.IncrementResponse, error)
|
||||||
Set(ctx context.Context, req domain.SetRequest) error
|
Set(ctx context.Context, req domain.SetRequest) error
|
||||||
|
GetLimit(ctx context.Context, req domain.GetRequest) (*domain.GetResponse, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type DailyLimiter struct {
|
type DailyLimiter struct {
|
||||||
@@ -33,3 +34,12 @@ func (c DailyLimiter) Set(ctx context.Context, req domain.SetRequest) error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c DailyLimiter) Get(ctx context.Context, req domain.GetRequest) (*domain.GetResponse, error) {
|
||||||
|
resp, err := c.svc.GetLimit(ctx, req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, apierrors.NewInternalServiceError(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return resp, nil
|
||||||
|
}
|
||||||
@@ -18,3 +18,12 @@ type SetRequest struct {
|
|||||||
Value uint64 `validate:"required"`
|
Value uint64 `validate:"required"`
|
||||||
Today time.Time `validate:"required"`
|
Today time.Time `validate:"required"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type GetRequest struct {
|
||||||
|
Key string `validate:"required"`
|
||||||
|
Today time.Time `validate:"required"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type GetResponse struct {
|
||||||
|
Value uint64
|
||||||
|
}
|
||||||
@@ -49,3 +49,15 @@ func (r dailyLimiter) Set(ctx context.Context, key string, dailyLimit uint64) er
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r dailyLimiter) GetLimit(ctx context.Context, key string) (uint64, error) {
|
||||||
|
val, err := r.redisCli.Get(ctx, makeKey(r.keyPrefix, key)).Uint64()
|
||||||
|
if errors.Is(err, redis.Nil) {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return 0, errors.WithMessage(err, "redis cli get daily limit")
|
||||||
|
}
|
||||||
|
|
||||||
|
return val, nil
|
||||||
|
}
|
||||||
@@ -52,5 +52,10 @@ func endpointDescriptors(c Controllers) []cluster.EndpointDescriptor {
|
|||||||
Inner: true,
|
Inner: true,
|
||||||
UserAuthRequired: false,
|
UserAuthRequired: false,
|
||||||
Handler: c.DailyLimiter.Set,
|
Handler: c.DailyLimiter.Set,
|
||||||
|
}, {
|
||||||
|
Path: "isp-lock-service/daily_limit/get",
|
||||||
|
Inner: true,
|
||||||
|
UserAuthRequired: false,
|
||||||
|
Handler: c.DailyLimiter.Get,
|
||||||
}}
|
}}
|
||||||
}
|
}
|
||||||
@@ -12,6 +12,7 @@ import (
|
|||||||
type dailyLimiterRepo interface {
|
type dailyLimiterRepo interface {
|
||||||
Increment(ctx context.Context, key string) (uint64, error)
|
Increment(ctx context.Context, key string) (uint64, error)
|
||||||
Set(ctx context.Context, key string, dailyLimit uint64) error
|
Set(ctx context.Context, key string, dailyLimit uint64) error
|
||||||
|
GetLimit(ctx context.Context, key string) (uint64, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type dailyLimiter struct {
|
type dailyLimiter struct {
|
||||||
@@ -36,6 +37,15 @@ func (s dailyLimiter) Set(ctx context.Context, req domain.SetRequest) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s dailyLimiter) GetLimit(ctx context.Context, req domain.GetRequest) (*domain.GetResponse, error) {
|
||||||
|
v, err := s.repo.GetLimit(ctx, s.makeKey(req.Key, req.Today))
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.WithMessage(err, "repo get")
|
||||||
|
}
|
||||||
|
|
||||||
|
return &domain.GetResponse{Value: v}, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s dailyLimiter) makeKey(key string, today time.Time) string {
|
func (s dailyLimiter) makeKey(key string, today time.Time) string {
|
||||||
return fmt.Sprintf("%s:%s", key, today.Format(time.DateOnly))
|
return fmt.Sprintf("%s:%s", key, today.Format(time.DateOnly))
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user