-
Notifications
You must be signed in to change notification settings - Fork 66
/
trylock.go
68 lines (59 loc) · 1.49 KB
/
trylock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package utils
import (
"context"
"time"
"golang.org/x/sync/semaphore"
)
// TryLock is a lock supporting both `Lock` and `TryLock` by wrapping a weighted semaphore.
type TryLock struct {
lock *semaphore.Weighted
ctx context.Context
}
// NewTryLock creates a lock based on a weighted semaphore.
func NewTryLock(ctx ...context.Context) *TryLock {
var c context.Context
switch len(ctx) {
case 0:
c = context.TODO()
case 1:
c = ctx[0]
default:
panic("multiple context not allowed")
}
return &TryLock{lock: semaphore.NewWeighted(1), ctx: c}
}
// Lock acquires the lock blocking until resource is available
func (l *TryLock) Lock() error {
if err := l.lock.Acquire(l.ctx, 1); err != nil {
return err
}
return nil
}
// TryLock tries to acquire the resource and returns true if successful.
func (l *TryLock) TryLock() bool {
return l.lock.TryAcquire(1)
}
// TryLockSpinning tries to acquire the resource for some time and returns true if successful.
func (l *TryLock) TryLockSpinning(spinTime time.Duration) bool {
end := time.Now().Add(spinTime)
waitTime := 200 * time.Microsecond
for {
if l.TryLock() {
return true
}
delta := time.Until(end)
if waitTime > delta {
time.Sleep(delta)
return l.TryLock()
}
time.Sleep(waitTime)
waitTime = 11 * waitTime / 10
}
}
// Unlock releases the resource
func (l *TryLock) Unlock() {
l.lock.Release(1)
}