go-resiliency: github.com/eapache/go-resiliency/semaphore Index | Examples | Files

package semaphore

import "github.com/eapache/go-resiliency/semaphore"

Package semaphore implements the semaphore resiliency pattern for Go.

Index

Examples

Package Files

semaphore.go

Variables

var ErrNoTickets = errors.New("could not acquire semaphore ticket")

ErrNoTickets is the error returned by Acquire when it could not acquire a ticket from the semaphore within the configured timeout.

type Semaphore Uses

type Semaphore struct {
    // contains filtered or unexported fields
}

Semaphore implements the semaphore resiliency pattern

Code:

sem := New(3, 1*time.Second)

for i := 0; i < 10; i++ {
    go func() {
        if err := sem.Acquire(); err != nil {
            return //could not acquire semaphore
        }
        defer sem.Release()

        // do something semaphore-guarded
    }()
}

func New Uses

func New(tickets int, timeout time.Duration) *Semaphore

New constructs a new Semaphore with the given ticket-count and timeout.

func (*Semaphore) Acquire Uses

func (s *Semaphore) Acquire() error

Acquire tries to acquire a ticket from the semaphore. If it can, it returns nil. If it cannot after "timeout" amount of time, it returns ErrNoTickets. It is safe to call Acquire concurrently on a single Semaphore.

func (*Semaphore) IsEmpty Uses

func (s *Semaphore) IsEmpty() bool

IsEmpty will return true if no tickets are being held at that instant. It is safe to call concurrently with Acquire and Release, though do note that the result may then be unpredictable.

func (*Semaphore) Release Uses

func (s *Semaphore) Release()

Release releases an acquired ticket back to the semaphore. It is safe to call Release concurrently on a single Semaphore. It is an error to call Release on a Semaphore from which you have not first acquired a ticket.

Package semaphore imports 2 packages (graph). Updated 2018-04-13. Refresh now. Tools for package owners. This is an inactive package (no imports and no commits in at least two years).