forked from AdguardTeam/dnsproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sema.go
58 lines (46 loc) · 1.32 KB
/
sema.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
package proxy
import (
"fmt"
)
// semaphore is the semaphore interface. acquire will block until the
// resource can be acquired. release never blocks.
type semaphore interface {
acquire()
release()
}
// noopSemaphore is a semaphore that has no limit.
type noopSemaphore struct{}
// acquire implements the semaphore interface for noopSemaphore.
func (noopSemaphore) acquire() {}
// release implements the semaphore interface for noopSemaphore.
func (noopSemaphore) release() {}
// newNoopSemaphore returns a new noopSemaphore.
func newNoopSemaphore() (s semaphore) { return noopSemaphore{} }
// sig is an alias for struct{} to type less.
type sig = struct{}
// chanSemaphore is a channel-based semaphore.
type chanSemaphore struct {
c chan sig
}
// acquire implements the semaphore interface for *chanSemaphore.
func (c *chanSemaphore) acquire() {
c.c <- sig{}
}
// release implements the semaphore interface for *chanSemaphore.
func (c *chanSemaphore) release() {
select {
case <-c.c:
default:
}
}
// newChanSemaphore returns a new chanSemaphore with the provided
// maximum resource number. maxRes must be greater than zero.
func newChanSemaphore(maxRes int) (s semaphore, err error) {
if maxRes < 1 {
return nil, fmt.Errorf("bad maxRes: %d", maxRes)
}
s = &chanSemaphore{
c: make(chan sig, maxRes),
}
return s, nil
}