We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 8fcdb60 commit d16bec7Copy full SHA for d16bec7
semaphore/semaphore.go
@@ -94,6 +94,24 @@ func (s *Weighted) TryAcquire(n int64) bool {
94
return success
95
}
96
97
+// TryAcquireAll acquires all free weight from the semaphore without blocking.
98
+// Returns the weight acquired. Returns 0 and leaves the semaphore unchanged if
99
+// no weight was available.
100
+func (s *Weighted) TryAcquireAll() int64 {
101
+ s.mu.Lock()
102
+ defer s.mu.Unlock()
103
+
104
+ // When somebody waits for a token there are obviously not enough tokens
105
+ // for waiters already waiting. Consequently, there are no free tokens.
106
+ if s.waiters.Len() > 0 {
107
+ return 0
108
+ }
109
110
+ free := s.size - s.cur
111
+ s.cur = s.size
112
+ return free
113
+}
114
115
// Release releases the semaphore with a weight of n.
116
func (s *Weighted) Release(n int64) {
117
s.mu.Lock()
0 commit comments