diff --git a/controller/locker.go b/controller/locker.go index 85736bb..d204599 100644 --- a/controller/locker.go +++ b/controller/locker.go @@ -9,10 +9,8 @@ import ( ) type LockerService interface { - // All(ctx context.Context) ([]domain.Locker, error) - // Get(ctx context.Context, id int) (*domain.Locker, error) Lock(ctx context.Context, req domain.Request) (*domain.LockResponse, error) - UnLock(ctx context.Context, req domain.Request) (domain.LockResponse, error) + UnLock(ctx context.Context, req domain.Request) (*domain.LockResponse, error) } type Locker struct { @@ -20,12 +18,32 @@ type Locker struct { logger log.Logger } +// Lock +// @Tags locker +// @Summary выставляем лок на строку +// @Description `key` - строка для лока +// @Description Возвращаем ключ для разблокировки +// @Accept json +// @Produce json +// @Success 200 {object} domain.LockResponse +// @Failure 500 {object} +// @Router /api/isp-lock-service/lock [POST] func (l Locker) Lock(ctx context.Context, req domain.Request) (*domain.LockResponse, error) { l.logger.Debug(ctx, "call ctrl.Lock") return l.s.Lock(ctx, req) } -func (l Locker) UnLock(ctx context.Context, req domain.Request) (domain.LockResponse, error) { +// UnLock +// @Tags locker +// @Summary снимаем лок со строки +// @Description `key` - строка для лока +// @Description `locKKey` - ключ для разблокировки, полученный из Lock +// @Accept json +// @Produce json +// @Success 200 {object} domain.LockResponse +// @Failure 500 {object} {} +// @Router /api/isp-lock-service/lock [POST] +func (l Locker) UnLock(ctx context.Context, req domain.Request) (*domain.LockResponse, error) { l.logger.Debug(ctx, "call ctrl.UnLock") return l.s.UnLock(ctx, req) } diff --git a/domain/locker.go b/domain/locker.go index f51689c..c2572cf 100644 --- a/domain/locker.go +++ b/domain/locker.go @@ -3,7 +3,7 @@ package domain import "time" type Request struct { - Key string + Key string `json:"Key" valid:"required"` LockKey string TTL time.Duration } diff --git a/rc/rs.go b/rc/rc.go similarity index 88% rename from rc/rs.go rename to rc/rc.go index 48ed618..a7d99fa 100644 --- a/rc/rs.go +++ b/rc/rc.go @@ -22,6 +22,18 @@ type RC struct { l *log.Adapter } +func NewRCWithClient(cfg conf.Remote, l *log.Adapter, cli *goredislib.Client) (*RC, error) { + r := RC{ + timeOut: cfg.Redis.DefaultTimeOut * time.Second, + prefix: cfg.Redis.Prefix, + l: l, + } + + r.cli = redsync.New(goredis.NewPool(cli)) + + return &r, nil +} + func NewRC(cfg conf.Remote, l *log.Adapter) (*RC, error) { r := RC{ timeOut: cfg.Redis.DefaultTimeOut * time.Second, diff --git a/repository/locker.go b/repository/locker.go index e7817c2..cb97e6e 100644 --- a/repository/locker.go +++ b/repository/locker.go @@ -25,14 +25,14 @@ func (l Locker) Lock(ctx context.Context, req domain.Request) (*domain.LockRespo return &domain.LockResponse{LockKey: val}, nil } -func (l Locker) UnLock(ctx context.Context, req domain.Request) (domain.LockResponse, error) { +func (l Locker) UnLock(ctx context.Context, req domain.Request) (*domain.LockResponse, error) { l.logger.Debug(ctx, "call repo.UnLock") _, err := l.rc.UnLock(req.Key, req.LockKey) if err != nil { l.logger.Error(ctx, err) - return domain.LockResponse{}, err + return &domain.LockResponse{}, err } - return domain.LockResponse{}, nil + return &domain.LockResponse{}, nil } func NewLocker(logger log.Logger, rc *rc.RC) Locker { diff --git a/service/locker.go b/service/locker.go index ea04658..7f59146 100644 --- a/service/locker.go +++ b/service/locker.go @@ -9,10 +9,8 @@ import ( ) type LockerRepo interface { - // All(ctx context.Context) ([]entity.Locker, error) - // Get(ctx context.Context, id int) (*entity.Locker, error) Lock(ctx context.Context, req domain.Request) (*domain.LockResponse, error) - UnLock(ctx context.Context, req domain.Request) (domain.LockResponse, error) + UnLock(ctx context.Context, req domain.Request) (*domain.LockResponse, error) } type Locker struct { @@ -25,7 +23,7 @@ func (l Locker) Lock(ctx context.Context, req domain.Request) (*domain.LockRespo return l.repo.Lock(ctx, req) } -func (l Locker) UnLock(ctx context.Context, req domain.Request) (domain.LockResponse, error) { +func (l Locker) UnLock(ctx context.Context, req domain.Request) (*domain.LockResponse, error) { l.logger.Debug(ctx, "call srv.UnLock") return l.repo.UnLock(ctx, req) }