Skip to content

Commit a1eea5c

Browse files
committed
feat: ErrLockReleased
1 parent 97a4c23 commit a1eea5c

File tree

3 files changed

+8
-6
lines changed

3 files changed

+8
-6
lines changed

modules/globallock/globallock.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package globallock
55

66
import (
77
"context"
8+
"fmt"
89
)
910

1011
type Locker interface {
@@ -13,3 +14,6 @@ type Locker interface {
1314
}
1415

1516
type ReleaseFunc func() context.Context
17+
18+
// ErrLockReleased is used as context cause when a lock is released
19+
var ErrLockReleased = fmt.Errorf("lock released")

modules/globallock/memory_locker.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package globallock
55

66
import (
77
"context"
8-
"fmt"
98
"sync"
109
"time"
1110
)
@@ -29,7 +28,7 @@ func (l *memoryLocker) Lock(ctx context.Context, key string) (context.Context, R
2928
return ctx, func() context.Context {
3029
releaseOnce.Do(func() {
3130
l.locks.Delete(key)
32-
cancel(fmt.Errorf("release"))
31+
cancel(ErrLockReleased)
3332
})
3433
return originalCtx
3534
}, nil
@@ -48,7 +47,7 @@ func (l *memoryLocker) Lock(ctx context.Context, key string) (context.Context, R
4847
return ctx, func() context.Context {
4948
releaseOnce.Do(func() {
5049
l.locks.Delete(key)
51-
cancel(fmt.Errorf("release"))
50+
cancel(ErrLockReleased)
5251
})
5352
return originalCtx
5453
}, nil
@@ -65,7 +64,7 @@ func (l *memoryLocker) TryLock(ctx context.Context, key string) (bool, context.C
6564
releaseOnce := sync.Once{}
6665
return true, ctx, func() context.Context {
6766
releaseOnce.Do(func() {
68-
cancel(fmt.Errorf("release"))
67+
cancel(ErrLockReleased)
6968
l.locks.Delete(key)
7069
})
7170
return originalCtx

modules/globallock/redis_locker.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package globallock
66
import (
77
"context"
88
"errors"
9-
"fmt"
109
"sync"
1110
"time"
1211

@@ -96,7 +95,7 @@ func (l *redisLocker) lock(ctx context.Context, key string, tries int) (context.
9695
// Do not call mutex.UnlockContext(ctx) here, or it will fail to release when ctx has timed out.
9796
_, _ = mutex.Unlock()
9897

99-
cancel(fmt.Errorf("release"))
98+
cancel(ErrLockReleased)
10099
})
101100
return originalCtx
102101
}, nil

0 commit comments

Comments
 (0)