mirror of
https://github.com/christianselig/apollo-backend
synced 2024-11-15 00:17:42 +00:00
36 lines
655 B
Go
36 lines
655 B
Go
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)
|
|
|
|
result, err := l.distributedLock.client.EvalSha(ctx, l.distributedLock.sha, []string{l.key, ch}, l.uid).Result()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if result == int64(0) {
|
|
return ErrLockExpired
|
|
}
|
|
|
|
return nil
|
|
}
|