apollo-backend/internal/distributedlock/lock.go

37 lines
655 B
Go
Raw Permalink Normal View History

2023-04-01 15:57:28 +00:00
package distributedlock
import (
"context"
"fmt"
)
type Lock struct {
distributedLock *DistributedLock
key string
uid string
}
func NewLock(distributedLock *DistributedLock, key string, uid string) *Lock {
return &Lock{
distributedLock: distributedLock,
key: key,
uid: uid,
}
}
func (l *Lock) Release(ctx context.Context) error {
ch := fmt.Sprintf(lockTopicFormat, l.key)
2023-04-01 16:07:48 +00:00
result, err := l.distributedLock.client.EvalSha(ctx, l.distributedLock.sha, []string{l.key, ch}, l.uid).Result()
2023-04-01 15:57:28 +00:00
if err != nil {
return err
}
if result == int64(0) {
return ErrLockExpired
}
return nil
}