semaphore

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2022 License: Apache-2.0 Imports: 2 Imported by: 4

README

semaphore

This implements a semaphore in Go. Use this to manage the maximum amount of concurrency you want.

GoDoc

Usage

To create a new Semaphore with 5 workers:

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

Acquire one of the workers:

sem.Acquire() // one
// Acquire with a timeout, or cancelable context:
sem.AcquireContext(context.TODO())

Once you're done with the semaphore:

sem.Release() // Release one worker
sem.Drain() // Release them all
Complete documentation

See here: https://godoc.org/github.com/kevinburke/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 allows you to control the number of in-flight requests of a given service.

func New

func New(n int) *Semaphore

New creates a new Semaphore with specified number of concurrent workers.

func (*Semaphore) Acquire

func (s *Semaphore) Acquire()

Acquire blocks until a worker becomes available.

func (*Semaphore) AcquireContext

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

AcquireContext attempts to acquire a resource. AcquireContext returns false if we were unable to acquire a resource before the Context timed out or was canceled.

func (*Semaphore) Available

func (s *Semaphore) Available() int

Available gives number of unacquired resources.

func (*Semaphore) Drain

func (s *Semaphore) Drain()

Drain releases all resources that have been acquired by this semaphore.

func (*Semaphore) Len

func (s *Semaphore) Len() int

Len returns the total number of workers in this semaphore.

func (*Semaphore) Release

func (s *Semaphore) Release()

Release releases one worker. Release panics if no workers are available to be released.

Jump to

Keyboard shortcuts

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