Documentation ¶
Overview ¶
Package semaphore provides an implementation of Semaphore pattern with timeout of lock/unlock operations based on channels.
Index ¶
- func Capacity() int
- func IsEmpty(err error) bool
- func IsTimeout(err error) bool
- func Multiplex(channels ...<-chan struct{}) <-chan struct{}
- func Occupied() int
- func Release() error
- func Signal(deadline <-chan struct{}) <-chan ReleaseFunc
- func WithContext(parent context.Context, deadline <-chan struct{}) context.Context
- func WithDeadline(deadline time.Time) <-chan struct{}
- func WithSignal(s os.Signal) <-chan struct{}
- func WithTimeout(timeout time.Duration) <-chan struct{}
- type HealthChecker
- type ReleaseFunc
- type Releaser
- type Semaphore
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Multiplex ¶
func Multiplex(channels ...<-chan struct{}) <-chan struct{}
Multiplex combines multiple empty struct channels into one. TODO can be leaky, https://github.com/kamilsk/semaphore/issues/133
func Occupied ¶
func Occupied() int
Occupied returns a current number of occupied slots of the default semaphore.
func Release ¶
func Release() error
Release releases the previously occupied slot of the default semaphore.
func Signal ¶
func Signal(deadline <-chan struct{}) <-chan ReleaseFunc
Signal returns a channel to send to it release function only if Acquire is successful. In any case, the channel will be closed.
func WithContext ¶
WithContext returns Context with cancellation based on empty struct channel.
func WithDeadline ¶
WithDeadline returns empty struct channel above on `time.Timer` channel. TODO can be leaky, https://github.com/kamilsk/semaphore/issues/133
func WithSignal ¶
WithSignal returns empty struct channel above on `os.Signal` channel. TODO can be leaky, https://github.com/kamilsk/semaphore/issues/133
func WithTimeout ¶
WithTimeout returns empty struct channel above on `time.Timer` channel. TODO can be leaky, https://github.com/kamilsk/semaphore/issues/133
Types ¶
type HealthChecker ¶
type HealthChecker interface { // Capacity returns a capacity of a semaphore. // It must be safe to call Capacity concurrently on a single semaphore. Capacity() int // Occupied returns a current number of occupied slots. // It must be safe to call Occupied concurrently on a single semaphore. Occupied() int }
HealthChecker defines helpful methods related with semaphore status.
type ReleaseFunc ¶
type ReleaseFunc func()
ReleaseFunc tells a semaphore to release the previously occupied slot and ignore an error if it occurs.
func Acquire ¶
func Acquire(deadline <-chan struct{}) (ReleaseFunc, error)
Acquire tries to reduce the number of available slots of the default semaphore for 1. The operation can be canceled using deadline channel. In this case, it returns an appropriate error.
type Releaser ¶
type Releaser interface { // Release releases the previously occupied slot. // If no places were occupied, then it returns an appropriate error. // It must be safe to call Release concurrently on a single semaphore. Release() error }
Releaser defines a method to release the previously occupied semaphore.
type Semaphore ¶
type Semaphore interface { HealthChecker Releaser // Acquire tries to reduce the number of available slots for 1. // The operation can be canceled using context. In this case, // it returns an appropriate error. // It must be safe to call Acquire concurrently on a single semaphore. Acquire(deadline <-chan struct{}) (ReleaseFunc, error) // Signal returns a channel to send to it release function // only if Acquire is successful. In any case, the channel will be closed. Signal(deadline <-chan struct{}) <-chan ReleaseFunc }
Semaphore provides the functionality of the same named pattern.