2020-04-28 17:48:25 +03:00
|
|
|
package semaphore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2020-05-21 12:45:23 +03:00
|
|
|
//error info
|
2020-04-28 17:48:25 +03:00
|
|
|
var (
|
2020-05-21 12:45:23 +03:00
|
|
|
ErrNoTickets = errors.New("could not acquire semaphore")
|
|
|
|
ErrIllegalRelease = errors.New("can't release the semaphore without acquiring it first")
|
2020-04-28 17:48:25 +03:00
|
|
|
)
|
|
|
|
|
2020-05-21 12:45:23 +03:00
|
|
|
// ISemaphore contains the behavior of a semaphore that can be acquired and/or released.
|
|
|
|
type ISemaphore interface {
|
2020-04-28 17:48:25 +03:00
|
|
|
Acquire() error
|
|
|
|
Release() error
|
|
|
|
}
|
|
|
|
|
2020-05-21 12:45:23 +03:00
|
|
|
type semp struct {
|
2020-04-28 17:48:25 +03:00
|
|
|
sem chan struct{}
|
|
|
|
timeout time.Duration
|
|
|
|
}
|
|
|
|
|
2020-05-21 12:45:23 +03:00
|
|
|
func (s *semp) Acquire() error {
|
2020-04-28 17:48:25 +03:00
|
|
|
select {
|
|
|
|
case s.sem <- struct{}{}:
|
|
|
|
return nil
|
|
|
|
case <-time.After(s.timeout):
|
|
|
|
return ErrNoTickets
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-21 12:45:23 +03:00
|
|
|
func (s *semp) Release() error {
|
2020-04-28 17:48:25 +03:00
|
|
|
select {
|
2020-05-21 12:45:23 +03:00
|
|
|
case <-s.sem:
|
2020-04-28 17:48:25 +03:00
|
|
|
return nil
|
|
|
|
case <-time.After(s.timeout):
|
|
|
|
return ErrIllegalRelease
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-05-21 12:45:23 +03:00
|
|
|
//New return a new Semaphore
|
|
|
|
func New(tickets int, timeout time.Duration) ISemaphore {
|
|
|
|
return &semp{
|
2020-04-28 17:48:25 +03:00
|
|
|
sem: make(chan struct{}, tickets),
|
|
|
|
timeout: timeout,
|
|
|
|
}
|
|
|
|
}
|