semaphore: github.com/kamilsk/semaphore Index | Files | Directories

package semaphore

import "github.com/kamilsk/semaphore"

Package semaphore provides an implementation of Semaphore pattern with timeout of lock/unlock operations based on channels.

Index

Package Files

channel.go context.go default.go semaphore.go

func Capacity Uses

func Capacity() int

Capacity returns a capacity of the default semaphore.

func IsEmpty Uses

func IsEmpty(err error) bool

IsEmpty checks if passed error is related to call Release on empty semaphore.

func IsTimeout Uses

func IsTimeout(err error) bool

IsTimeout checks if passed error is related to call Acquire on full semaphore.

func Multiplex Uses

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 Uses

func Occupied() int

Occupied returns a current number of occupied slots of the default semaphore.

func Release Uses

func Release() error

Release releases the previously occupied slot of the default semaphore.

func Signal Uses

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 Uses

func WithContext(parent context.Context, deadline <-chan struct{}) context.Context

WithContext returns Context with cancellation based on empty struct channel.

func WithDeadline Uses

func WithDeadline(deadline time.Time) <-chan struct{}

WithDeadline returns empty struct channel above on `time.Timer` channel. TODO can be leaky, https://github.com/kamilsk/semaphore/issues/133

func WithSignal Uses

func WithSignal(s os.Signal) <-chan struct{}

WithSignal returns empty struct channel above on `os.Signal` channel. TODO can be leaky, https://github.com/kamilsk/semaphore/issues/133

func WithTimeout Uses

func WithTimeout(timeout time.Duration) <-chan struct{}

WithTimeout returns empty struct channel above on `time.Timer` channel. TODO can be leaky, https://github.com/kamilsk/semaphore/issues/133

type HealthChecker Uses

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 Uses

type ReleaseFunc func()

ReleaseFunc tells a semaphore to release the previously occupied slot and ignore an error if it occurs.

func Acquire Uses

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.

func (ReleaseFunc) Release Uses

func (f ReleaseFunc) Release() error

Release calls f().

type Releaser Uses

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 Uses

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.

func New Uses

func New(capacity int) Semaphore

New constructs a new thread-safe Semaphore with the given capacity.

Directories

PathSynopsis
cmd/semaphore

Package semaphore imports 7 packages (graph) and is imported by 1 packages. Updated 2019-02-17. Refresh now. Tools for package owners.