Semaphore:负责协调各个线程,以保证它们能够正确、合理地使用公共资源。
Semaphore Pattern
A semaphore is a synchronization pattern/primitive that imposes mutual exclusion on a limited number of resources.
信号量是在有限数量的资源上强制互斥的同步模式/原语。
Implementation1
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
45package semaphore
var (
ErrNoTickets = errors.New("semaphore: could not aquire semaphore")
ErrIllegalRelease = errors.New("semaphore: can't release the semaphore without acquiring it first")
)
// Interface contains the behavior of a semaphore that can be acquired and/or released.
// 接口包含可以获取和/或释放的信号量的行为。
type Interface interface {
Acquire() error
Release() error
}
type implementation struct {
sem chan struct{}
timeout time.Duration
}
func (s *implementation) Acquire() error {
select {
case s.sem <- struct{}{}:
return nil
case <-time.After(s.timeout):
return ErrNoTickets
}
}
func (s *implementation) Release() error {
select {
case _ = <-s.sem:
return nil
case <-time.After(s.timeout):
return ErrIllegalRelease
}
return nil
}
func New(tickets int, timeout time.Duration) Interface {
return &implementation{
sem: make(chan struct{}, tickets),
timeout: timeout,
}
}
Usage
Semaphore with Timeouts
信号量超时1
2
3
4
5
6
7
8
9
10
11
12tickets, timeout := 1, 3*time.Second
s := semaphore.New(tickets, timeout)
if err := s.Acquire(); err != nil {
panic(err)
}
// Do important work 做重要工作
if err := s.Release(); err != nil {
panic(err)
}
Semaphore without Timeouts (Non-Blocking)
信号量没有超时(非阻塞)1
2
3
4
5
6
7
8
9
10
11tickets, timeout := 0, 0
s := semaphore.New(tickets, timeout)
if err := s.Acquire(); err != nil {
if err != semaphore.ErrNoTickets {
panic(err)
}
// No tickets left, can't work :( 没票了,不工作
os.Exit(1)
}