2026-10-12 12:27:14: merge [main:43ce9babe5667b4aa643ec333e1b8e988c1a8980];

This commit is contained in:
txix committed 2026-10-12 12:27:14 +03:00
1 parent 17985317d2
commit 4b9d03c833
21 files changed
+595 -243

No files matched your search

+31
View File
@@ -2,14 +2,19 @@ package controller
import (
"context"
"errors"
"net/http"
"isp-lock-service/domain"
"github.com/go-redsync/redsync/v4"
"github.com/txix-open/isp-kit/grpc/apierrors"
"github.com/txix-open/isp-kit/log"
)
type LockerService interface {
Lock(ctx context.Context, req domain.LockRequest) (*domain.LockResponse, error)
TryLock(ctx context.Context, req domain.LockRequest) (*domain.LockResponse, error)
UnLock(ctx context.Context, req domain.UnLockRequest) (*domain.LockResponse, error)
}
@@ -39,6 +44,32 @@ func (l Locker) Lock(ctx context.Context, req domain.LockRequest) (*domain.LockR
return l.s.Lock(ctx, req)
}
// TryLock
// @Tags locker
// @Summary однократная попытка взять лок без ретраев
// @Description Пытаемся взять лок один раз. В отличие от Lock, не делает повторных попыток. При успехе возвращаем ключ для разблокировки.
// @Description При получение ошибки, что лок уже занят, возращаем ошибку с кодом 423.
// @Param key query string true "строка для лока"
// @Param ttlInSec query int true "число секунд после которых блокировка снимется автоматически"
// @Accept json
// @Produce json
// @Success 200 {object} domain.LockResponse
// @Failure 400 {object} apierrors.Error "lock already taken"
// @Router /api/isp-lock-service/try_lock [POST]
func (l Locker) TryLock(ctx context.Context, req domain.LockRequest) (*domain.LockResponse, error) {
var errTaken *redsync.ErrTaken
resp, err := l.s.TryLock(ctx, req)
switch {
case errors.As(err, &errTaken):
return nil, apierrors.NewBusinessError(http.StatusLocked, err.Error(), err)
case err != nil:
return nil, err
}
return resp, nil
}
// UnLock
// @Tags locker
// @Summary снимаем лок со строки