gate

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

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

Go to latest
Published: Aug 27, 2015 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package gate provides primitive to limit number of concurrent goroutine workers. Useful when sync.Locker or sync.WaitGroup is not enough.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add

func Add(n int)

Add adds n to default gate counter. Absolute value of n should be no more than runtime.NumCPU()

func Done

func Done()

Done decrements default gate counter

func Lock

func Lock()

Lock locks default gate with capacity defined by runtime.NumCPU()

func Unlock

func Unlock()

Unlock unlocks default gate

func Wait

func Wait()

Wait blocks until default gate is not locked

Types

type Gate

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

A Gate is a primitive intended to help in limiting concurrency in some scenarios. Think of it as a close sync.WaitGroup analog which has upper limit on its counter or a sync.Locker which allows up to max number of concurrent lockers to be held.

func New

func New(max int) *Gate

New returns new Gate with provided capacity. If capacity is non-positive, New would panic.

func (*Gate) Add

func (g *Gate) Add(n int)

Add implements similar semantic to sync.WaitGroup.Add. If Add is called with positive argument N, it essentially calls Lock N times; if N is negative, it calls Unlock N times. If absolute value of N is greater than Gate capacity, Add would panic. Add is safe for concurrent use, but should be used with care as deadlocks are possible.

func (*Gate) Done

func (g *Gate) Done()

Done semantic is the same as sync.WaitGroup.Done.

func (*Gate) Lock

func (g *Gate) Lock()

Lock implements sync.Locker interface. Gate capacity determines number of non-blocking Lock calls, when max number is reached, Lock would block until some other goroutine calls Unlock. Lock is safe for concurrent use.

func (*Gate) Unlock

func (g *Gate) Unlock()

Unlock implements sync.Locker interface. Unlock is safe for concurrent use.

func (*Gate) Wait

func (g *Gate) Wait()

Wait blocks until nothing holds a single Gate lock. Its semantic is the same as sync.WaitGroup.Wait.

Jump to

Keyboard shortcuts

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