2016-04-15 15:09:17 +03:00
|
|
|
# Semaphore Pattern
|
|
|
|
A semaphore is a synchronization pattern/primitive that imposes mutual exclusion on a limited number of resources.
|
2015-12-23 16:45:48 +03:00
|
|
|
|
2016-04-15 15:09:17 +03:00
|
|
|
## Implementation
|
|
|
|
|
|
|
|
```go
|
|
|
|
package semaphore
|
2015-12-23 16:45:48 +03:00
|
|
|
|
|
|
|
var (
|
|
|
|
ErrNoTickets = errors.New("semaphore: could not aquire semaphore")
|
|
|
|
ErrIllegalRelease = errors.New("semaphore: can't release the semaphore without acquiring it first")
|
|
|
|
)
|
|
|
|
|
2016-04-15 15:09:17 +03:00
|
|
|
// Interface contains the behavior of a semaphore that can be acquired and/or released.
|
2015-12-23 16:45:48 +03:00
|
|
|
type Interface interface {
|
|
|
|
Acquire() error
|
|
|
|
Release() error
|
|
|
|
}
|
|
|
|
|
2016-01-04 07:15:29 +03:00
|
|
|
type implementation struct {
|
2015-12-23 16:45:48 +03:00
|
|
|
sem chan struct{}
|
|
|
|
timeout time.Duration
|
|
|
|
}
|
|
|
|
|
2016-01-04 07:15:29 +03:00
|
|
|
func (s *implementation) Acquire() error {
|
2015-12-23 16:45:48 +03:00
|
|
|
select {
|
|
|
|
case s.sem <- struct{}{}:
|
|
|
|
return nil
|
|
|
|
case <-time.After(s.timeout):
|
|
|
|
return ErrNoTickets
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-04 07:15:29 +03:00
|
|
|
func (s *implementation) Release() error {
|
|
|
|
select {
|
|
|
|
case _ = <-s.sem:
|
|
|
|
return nil
|
|
|
|
case <-time.After(s.timeout):
|
2015-12-23 16:45:48 +03:00
|
|
|
return ErrIllegalRelease
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2016-01-04 07:15:29 +03:00
|
|
|
|
|
|
|
func New(tickets int, timeout time.Duration) Interface {
|
|
|
|
return &implementation{
|
|
|
|
sem: make(chan struct{}, tickets),
|
|
|
|
timeout: timeout,
|
|
|
|
}
|
|
|
|
}
|
2016-04-15 15:09:17 +03:00
|
|
|
```
|
|
|
|
|
|
|
|
## Usage
|
|
|
|
### Semaphore with Timeouts
|
|
|
|
|
|
|
|
```go
|
|
|
|
tickets, 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)
|
|
|
|
|
|
|
|
```go
|
|
|
|
tickets, 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)
|
|
|
|
}
|
|
|
|
```
|