semaphore

package
v0.0.0-...-6800482 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 13, 2015 License: MIT Imports: 2 Imported by: 0

README

semaphore

Build Status GoDoc

The semaphore resiliency pattern for golang.

Creating a semaphore takes two parameters:

  • ticket count (how many tickets to give out at once)
  • timeout (how long to wait for a ticket if none are currently available)
sem := semaphore.New(3, 1*time.Second)

if err := sem.Acquire(); err != nil {
	// could not acquire semaphore
	return err
}
defer sem.Release()

Documentation

Overview

Package semaphore implements the semaphore resiliency pattern for Go.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrNoTickets = errors.New("could not aquire semaphore ticket")

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

Functions

This section is empty.

Types

type Semaphore

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

Semaphore implements the semaphore resiliency pattern

Example
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
	}()
}
Output:

func New

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

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

func (*Semaphore) Acquire

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) Release

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL