multierrgroup

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

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

Go to latest
Published: Jul 11, 2018 License: MIT Imports: 2 Imported by: 0

README

multierrgroup

Build Status Go Documentation

multierrgroup combines sync.WaitGroup and hashicorp's multierror.Error to create a wait group that collects all reported errors.

Installation and Docs

Install using go get github.com/jribe/multierrgroup.

Full documentation is available at http://godoc.org/github.com/jribe/multierrgroup

Usage

multierrgroup is used just like sync.WaitGroup, except the Done method takes error as a parameter and the Wait method returns a multierror.Error or nil.

Doing work in separate goroutines

If all the calls to Done provide a nil error, Wait returns nil

package main

import (
    "github.com/jribe/multierrgroup"
    "log"
)

func main() {
    wg := &multierrgroup.WaitGroup{}

    wg.Add(2)
    go func() {
        log.Println("goroutine 1!")
        wg.Done(nil)
    }()
    go func() {
        log.Println("goroutine 2!")
        wg.Done(nil)
    }()

    if err := wg.Wait(); err != nil {
        log.Fatal(err)
    }
}

Counting random failures

Wait returns *multierror.Error instead of error, so you can directly access the list of errors.

package main

import (
    "fmt"
    "github.com/jribe/multierrgroup"
    "log"
    "math/rand"
)

func RandomlyFail(i int) error {
    number := rand.New(rand.NewSource(int64(i))).Intn(2)
    if number != 0 {
        return fmt.Errorf("%d failed", number)
    }
    return nil
}

func main() {
    wg := &multierrgroup.WaitGroup{}

    for i := 0; i < 300; i++ {
        wg.Add(1)
        go func(i int) {
            err := RandomlyFail(i)
            wg.Done(err)
        }(i)
    }

    err := wg.Wait()
    log.Printf("there were %d errors!", len(err.Errors))
    log.Print(err)
}

Documentation

Overview

Example
wg := &WaitGroup{}

wg.Add(2)
go func() {
	log.Println("goroutine 1!")
	wg.Done(nil)
}()
go func() {
	log.Println("goroutine 2!")
	wg.Done(nil)
}()

if err := wg.Wait(); err != nil {
	log.Fatal(err)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type WaitGroup

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

WaitGroup waits for a collection of goroutines to finish. The main goroutine calls Add to set the number of goroutines to wait for. Then each of the goroutines runs and calls Done when finished. At the same time, Wait can be used to block until all goroutines have finished.

A WaitGroup must not be copied after first use.

WaitGroup is distinct from sync.WaitGroup because it collects errors returned by the goroutines in a multierror.Error.

func (*WaitGroup) Add

func (g *WaitGroup) Add(delta int)

Add adds delta, which may be negative, to the WaitGroup counter. If the counter becomes zero, all goroutines blocked on Wait are released. If the counter goes negative, Add panics.

Note that calls with a positive delta that occur when the counter is zero must happen before a Wait. Calls with a negative delta, or calls with a positive delta that start when the counter is greater than zero, may happen at any time. Typically this means the calls to Add should execute before the statement creating the goroutine or other event to be waited for. A WaitGroup should not be reused to wait for several independent sets of events because the collected errors can not be reset

func (*WaitGroup) Done

func (g *WaitGroup) Done(err error)

Done decrements the WaitGroup counter by one and adds err to the collected errors.

func (*WaitGroup) Wait

func (g *WaitGroup) Wait() *multierror.Error

Wait blocks until the WaitGroup counter is zero and returns a *multierror.Error wrapping all of the errors returned by the goroutines. If all of the goroutines returned nil errors, Wait returns nil.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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