semaphore

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2018 License: MIT Imports: 1 Imported by: 0

README

Semaphore

For Bounding resource use.

s := semaphore.NewSemaphore(10)

go func() {
    ctx := context.Background() // for cancellation
    if s.Obtain(ctx) {
        defer s.Release()
        // do whatever 
    }
}()

For weighted semaphore, see this implementation.

Documentation

Overview

Package semaphore provides a bounded resources implementation.

Index

Constants

This section is empty.

Variables

New is a func alias for NewSemaphore. The name NewSemaphore is reduncdant but kept for compatibility.

Functions

This section is empty.

Types

type Semaphore

type Semaphore interface {
	// Obtain puts one resource into the semaphore,
	// returns true if it succeeds;
	// otherwise it blocks until the context is cancelled.
	// Obtaining from a closed semaphore should return false.
	Obtain(context.Context) bool

	// Release takes one resource from the semaphore,
	// returns true if it succeeds.
	// It should never blocks.
	Release() bool

	// Capacity returns semaphore's max concurrent resources.
	Capacity() int

	// Count returns semaphore's current used resources.
	Count() int

	// Close closes the semaphore, stops obtaining resources
	// from it by making Obtain() return false ever since.
	Close()

	// Closed tells if the semaphore is closed.
	Closed() bool
}

Semaphore is a bounded resources abstraction. Ref: https://github.com/golang/go/wiki/BoundingResourceUse

func NewSemaphore

func NewSemaphore(n int) Semaphore

NewSemaphore returns an internal semaphore. This is the exported interface for using semaphore.

Jump to

Keyboard shortcuts

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