Skip to content

Commit b5984b6

Browse files
committed
fix(locks): typing and local lock context cancelation
1 parent b43083c commit b5984b6

2 files changed

Lines changed: 24 additions & 4 deletions

File tree

pkg/lock/etcd.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ type EtcdRWLock struct {
5252
// w -> /chall-manager/<key>/w
5353
}
5454

55+
var _ RWLock = (*EtcdRWLock)(nil)
56+
5557
func NewEtcdRWLock(ctx context.Context, key string) (RWLock, error) {
5658
s, gen, err := global.GetEtcdManager().GetSession(ctx)
5759
if err != nil {

pkg/lock/local.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ type LocalLock struct {
1414
mx *sync.RWMutex
1515
}
1616

17+
var _ RWLock = (*LocalLock)(nil)
18+
1719
func NewLocalRWLock(key string) (RWLock, error) {
1820
lock, _ := localLocks.LoadOrStore(key, &LocalLock{
1921
key: key,
@@ -26,22 +28,38 @@ func (lock *LocalLock) Key() string {
2628
return lock.key
2729
}
2830

29-
func (lock *LocalLock) RLock(_ context.Context) error {
31+
func (lock *LocalLock) IsCanceled(err error) bool {
32+
return err == context.Canceled
33+
}
34+
35+
func (lock *LocalLock) RLock(ctx context.Context) error {
36+
if err := ctx.Err(); err != nil {
37+
return err
38+
}
3039
lock.mx.RLock()
3140
return nil
3241
}
3342

34-
func (lock *LocalLock) RUnlock(_ context.Context) error {
43+
func (lock *LocalLock) RUnlock(ctx context.Context) error {
44+
if err := ctx.Err(); err != nil {
45+
return err
46+
}
3547
lock.mx.RUnlock()
3648
return nil
3749
}
3850

39-
func (lock *LocalLock) RWLock(_ context.Context) error {
51+
func (lock *LocalLock) RWLock(ctx context.Context) error {
52+
if err := ctx.Err(); err != nil {
53+
return err
54+
}
4055
lock.mx.Lock()
4156
return nil
4257
}
4358

44-
func (lock *LocalLock) RWUnlock(_ context.Context) error {
59+
func (lock *LocalLock) RWUnlock(ctx context.Context) error {
60+
if err := ctx.Err(); err != nil {
61+
return err
62+
}
4563
lock.mx.Unlock()
4664
return nil
4765
}

0 commit comments

Comments
 (0)