/
semaphore.go
169 lines (138 loc) · 3.23 KB
/
semaphore.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package sync
import (
"fmt"
"sync"
"time"
sema "golang.org/x/sync/semaphore"
)
type prioritySemaphore struct {
name string
limit int
pending *priorityQueue
running map[string]bool
semaphore *sema.Weighted
lock *sync.Mutex
}
var _ Semaphore = &prioritySemaphore{}
func newSemaphore(name string, limit int) *prioritySemaphore {
return &prioritySemaphore{
name: name,
limit: limit,
pending: &priorityQueue{itemByKey: make(map[string]*item)},
semaphore: sema.NewWeighted(int64(limit)),
running: make(map[string]bool),
lock: &sync.Mutex{},
}
}
func (s *prioritySemaphore) getName() string {
return s.name
}
func (s *prioritySemaphore) getLimit() int {
return s.limit
}
func (s *prioritySemaphore) getCurrentPending() []string {
keys := []string{}
for _, item := range s.pending.items {
keys = append(keys, item.key)
}
return keys
}
func (s *prioritySemaphore) getCurrentRunning() []string {
keys := []string{}
for k := range s.running {
keys = append(keys, k)
}
return keys
}
func (s *prioritySemaphore) resize(n int) bool {
s.lock.Lock()
defer s.lock.Unlock()
cur := len(s.running)
// downward case, acquired n locks
if cur > n {
cur = n
}
semaphore := sema.NewWeighted(int64(n))
status := semaphore.TryAcquire(int64(cur))
if status {
s.semaphore = semaphore
s.limit = n
}
return status
}
func (s *prioritySemaphore) removeFromQueue(key string) {
s.lock.Lock()
defer s.lock.Unlock()
s.pending.remove(key)
}
func (s *prioritySemaphore) acquireLatest() string {
s.lock.Lock()
defer s.lock.Unlock()
if s.pending.Len() == 0 {
return ""
}
ready := s.pending.peek()
if s.semaphore.TryAcquire(1) {
_ = s.pending.pop()
s.running[ready.key] = true
return ready.key
}
return ""
}
func (s *prioritySemaphore) release(key string) bool {
s.lock.Lock()
defer s.lock.Unlock()
if _, ok := s.running[key]; ok {
delete(s.running, key)
// When semaphore resized downward
// Remove the excess holders from map once the done.
if len(s.running) >= s.limit {
return true
}
s.semaphore.Release(1)
}
return true
}
func (s *prioritySemaphore) addToQueue(key string, creationTime time.Time) bool {
s.lock.Lock()
defer s.lock.Unlock()
if _, ok := s.running[key]; ok {
return false
}
if s.pending.isPending(key) {
return false
}
s.pending.add(key, creationTime.UnixNano())
return true
}
func (s *prioritySemaphore) tryAcquire(key string) (bool, string) {
s.lock.Lock()
defer s.lock.Unlock()
if _, ok := s.running[key]; ok {
return true, ""
}
waitingMsg := fmt.Sprintf("Waiting for %s lock. Available queue status: %d/%d", s.name, s.limit-len(s.running), s.limit)
// Check whether requested key is in front of priority queue.
// If it is in front position, it will allow to acquire lock.
// If it is not a front key, it needs to wait for its turn.
var nextKey string
if s.pending.Len() > 0 {
item := s.pending.peek()
nextKey = fmt.Sprintf("%v", item.key)
if key != nextKey {
return false, waitingMsg
}
}
if s.acquire(nextKey) {
s.pending.pop()
return true, ""
}
return false, waitingMsg
}
func (s *prioritySemaphore) acquire(key string) bool {
if s.semaphore.TryAcquire(1) {
s.running[key] = true
return true
}
return false
}