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

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

default.go draft.go interface.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 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.

type BreakCloser Uses

type BreakCloser interface {
    Breaker
    // Close closes the Done channel and releases resources associated with it.
    Close()
}

A BreakCloser carries a cancellation signal to break an action execution and can release resources associated with it.

It is a subset of github.com/kamilsk/breaker.Breaker.

type Breaker Uses

type Breaker interface {
    // Done returns a channel that's closed when a cancellation signal occurred.
    Done() <-chan struct{}
}

A Breaker carries a cancellation signal to break an action execution.

It is a subset of context.Context and github.com/kamilsk/breaker.Breaker.

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. Deprecated: will be replaced by Interface.

type Interface Uses

type Interface interface {
    Releaser

    Acquire(BreakCloser, ...uint32) (Releaser, error)
    Try(Breaker, ...uint32) (Releaser, error)
    Signal(Breaker) <-chan Releaser

    Peek() uint32
    Size(uint) uint32
}

Interface defines the functionality of the Semaphore pattern.

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 resources associated with the Releaser.
    // If no places were occupied, then it returns an appropriate error.
    Release() error
}

A Releaser provides a possibility to release resources that it holds.

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. Deprecated: will be replaced by Interface.

func New Uses

func New(capacity int) Semaphore

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

Package semaphore imports 3 packages (graph). Updated 2019-04-23. Refresh now. Tools for package owners.