semaphore

package module
v0.0.0-...-cb737ff Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2018 License: Apache-2.0 Imports: 3 Imported by: 24

README

semaphore

Semaphore in Go

Build Status GoDoc Go Report Card

Usage

Initiate

import "github.com/abiosoft/semaphore"
...
sem := semaphore.New(5) // new semaphore with 5 permits

Acquire

sem.Acquire() // one
sem.AcquireMany(n) // multiple
sem.AcquireWithin(n, time.Second * 5) // timeout after 5 sec
sem.AcquireContext(ctx, n) // acquire with context

Release

sem.Release() // one
sem.ReleaseMany(n) // multiple
documentation

http://godoc.org/github.com/abiosoft/semaphore

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Semaphore

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

Semaphore is an implementation of semaphore.

func New

func New(permits int) *Semaphore

New creates a new Semaphore with specified number of permits.

func (*Semaphore) Acquire

func (s *Semaphore) Acquire()

Acquire acquires one permit. If it is not available, the goroutine will block until it is available.

func (*Semaphore) AcquireContext

func (s *Semaphore) AcquireContext(ctx context.Context, n int) bool

AcquireContext is similar to AcquireMany() but takes a context. Returns true if successful or false if the context is done first.

func (*Semaphore) AcquireMany

func (s *Semaphore) AcquireMany(n int)

AcquireMany is similar to Acquire() but for many permits.

The number of permits acquired is at most the number of permits in the semaphore. i.e. if n = 5 and s was created with New(2), at most 2 permits will be acquired.

func (*Semaphore) AcquireWithin

func (s *Semaphore) AcquireWithin(n int, d time.Duration) bool

AcquireWithin is similar to AcquireMany() but cancels if duration elapses before getting the permits. Returns true if successful and false if timeout occurs.

func (*Semaphore) AvailablePermits

func (s *Semaphore) AvailablePermits() int

AvailablePermits gives number of available unacquired permits.

func (*Semaphore) DrainPermits

func (s *Semaphore) DrainPermits() int

DrainPermits acquires all available permits and return the number of permits acquired.

func (*Semaphore) Release

func (s *Semaphore) Release()

Release releases one permit.

func (*Semaphore) ReleaseMany

func (s *Semaphore) ReleaseMany(n int)

ReleaseMany releases n permits.

The number of permits released is at most the number of permits in the semaphore. i.e. if n = 5 and s was created with New(2), at most 2 permits will be released.

Jump to

Keyboard shortcuts

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