-
Notifications
You must be signed in to change notification settings - Fork 0
/
semaphore.go
97 lines (85 loc) · 2.9 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
// semaphore.go is for a counting semaphore.
// Semaphore, like FreeList, is a slightly higher level implementation than native channerics
// and may represent library bloat, so it may be removed in the future.
package channerics
import (
"context"
"sync"
)
// Semaphore represents semaphore turnstile semantics implemented using a buffered channel.
// This is traditionally used for finite concurrency limits, such as n concurrent db connections.
type Semaphore struct {
ch chan struct{}
safe_closer sync.Once
}
// TODO: see sync/semaphore weighted-semaphore implementation. This
// implementation likely not needed; the stdlib weight version likely has this covered.
// NewSemaphore returns a context-sensitive semaphore built on a buffered chan.
// If n == 0, use sync.Mutex or sync.RWMutex instead.
// Also note that an idiomatic local semaphore can be written simply:
// go func() {
// sem := make(chan struct{}, 10)
// defer close(sem)
// for {
// data <- workChan
// select {
// case ch <- struct{}:
// case <-context.Done():
// return
// }
// ... do work
// work(data)
// <-ch
// }
// }
//
func NewSemaphore(n int) *Semaphore {
return &Semaphore{
ch: make(chan struct{}, n),
safe_closer: sync.Once{},
}
}
// Take 'decrements' the semaphore, blocking until the semaphore is available.
// Taken indicates that take succeeded without context cancellation; if true,
// then Release must be called later, e.g. using 'defer sem.Release()'.
func (sem *Semaphore) Take(ctx context.Context) (taken bool) {
select {
case sem.ch <- struct{}{}:
taken = true
case <-ctx.Done():
}
return
}
// UnsafeCount returns an immediately expired view of the semaphore count,
// since the true count may change afterward at any point.
// Thus the only use-cases for this function are (1) determining when the semaphore
// is 'probably' free, but possibly blocking is still permissible, or (2) you coordinate
// your go-routines using another mechanism to not alter it until you've use it.
func (sem *Semaphore) UnsafeCount() int {
return cap(sem.ch) - len(sem.ch)
}
// MaybeTake attempts to take (decrement) if it would not block, otherwise returns immediately.
// MaybeTake does not take a context since it is non-blocking.
// Taken indicates that take succeeded without context cancellation; if true,
// then Release must be called later, e.g. using 'defer sem.Release()'.
func (sem *Semaphore) MaybeTake() (taken bool) {
select {
case sem.ch <- struct{}{}:
taken = true
default:
}
return
}
// Release increments the semaphore without blocking and must be called after calling
// Take(), its counterpart.
func (sem *Semaphore) Release() {
select {
case <-sem.ch:
default:
}
}
// Close closes the semaphore's channel, after which other methods will panic if called.
// Close may be called repeatedly from multiple go routines.
func (sem *Semaphore) Close() {
sem.safe_closer.Do(func() { close(sem.ch) })
}