go4.org: go4.org/syncutil Index | Files | Directories

package syncutil

import "go4.org/syncutil"

Package syncutil provides various synchronization utilities.

Index

Package Files

gate.go group.go once.go sem.go syncutil.go

type Gate Uses

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

A Gate limits concurrency.

func NewGate Uses

func NewGate(max int) *Gate

NewGate returns a new gate that will only permit max operations at once.

func (*Gate) Done Uses

func (g *Gate) Done()

Done finishes an operation.

func (*Gate) Start Uses

func (g *Gate) Start()

Start starts an operation, blocking until the gate has room.

type Group Uses

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

A Group is like a sync.WaitGroup and coordinates doing multiple things at once. Its zero value is ready to use.

func (*Group) Err Uses

func (g *Group) Err() error

Err waits for all previous calls to Go to complete and returns the first non-nil error, or nil.

func (*Group) Errs Uses

func (g *Group) Errs() []error

Errs waits for all previous calls to Go to complete and returns all non-nil errors.

func (*Group) Go Uses

func (g *Group) Go(fn func() error)

Go runs fn in its own goroutine, but does not wait for it to complete. Call Err or Errs to wait for all the goroutines to complete.

func (*Group) Wait Uses

func (g *Group) Wait()

Wait waits for all the previous calls to Go to complete.

type Once Uses

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

A Once will perform a successful action exactly once.

Unlike a sync.Once, this Once's func returns an error and is re-armed on failure.

func (*Once) Do Uses

func (o *Once) Do(f func() error) error

Do calls the function f if and only if Do has not been invoked without error for this instance of Once. In other words, given

var once Once

if once.Do(f) is called multiple times, only the first call will invoke f, even if f has a different value in each invocation unless f returns an error. A new instance of Once is required for each function to execute.

Do is intended for initialization that must be run exactly once. Since f is niladic, it may be necessary to use a function literal to capture the arguments to a function to be invoked by Do:

err := config.once.Do(func() error { return config.init(filename) })

type Sem Uses

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

Sem implements a semaphore that can have multiple units acquired/released at a time.

func NewSem Uses

func NewSem(max int64) *Sem

NewSem creates a semaphore with max units available for acquisition.

func (*Sem) Acquire Uses

func (s *Sem) Acquire(n int64) error

Acquire will deduct n units from the semaphore. If the deduction would result in the available units falling below zero, the call will block until another go routine returns units via a call to Release. If more units are requested than the semaphore is configured to hold, error will be non-nil.

func (*Sem) Release Uses

func (s *Sem) Release(n int64)

Release will return n units to the semaphore and notify any currently blocking Acquire calls.

Directories

PathSynopsis
singleflightPackage singleflight provides a duplicate function call suppression mechanism.
syncdebugPackage syncdebug contains facilities for debugging synchronization problems.

Package syncutil imports 4 packages (graph) and is imported by 128 packages. Updated 2018-01-04. Refresh now. Tools for package owners.