awg

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

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

Go to latest
Published: May 3, 2017 License: Apache-2.0 Imports: 5 Imported by: 0

README

Advanced wait group

AWG - advanced version of wait group

Added features:

  • execution timeout
  • cancel and deadline with context
  • ability to return errors
  • panic handling from goroutines

Usage:

#!go
 	wg := awg.AdvancedWaitGroup{}
	
	// Add function one
	wg.Add(func() error {
		//Logic
		return nil
	})

	// Add function two
	wg.Add(func() error {
		//Another Logic
		return nil
	})
	
	wg.Start()

Getting errors (or one error):
#!go
	wg := awg.AdvancedWaitGroup{}
	
	// Add function one
	wg.Add(func() error {
		//Logic
		return nil
	})

	// Add function two
	wg.Add(func() error {
		//Another Logic
		return nil
	})
	
        // Taking one error make sense if you use *.SetStopOnError(true)* option - see below
        var err error
	err = wg.SetStopOnError(true).Start().GetLastError()


	// Taking all errors
	var errs []error
	errs = wg.Start().GetAllErrors()

You can add slice of functions to exec in parallel threads:
#!go

	wg := awg.AdvancedWaitGroup{}

	functions := []awg.WaitgroupFunc{
		func() error {
			//Logic
			return nil
		},
		func() error {
			//Another Logic
			return nil
		},
	}
	
	// Add function one
	wg.AddSlice(functions)

	wg.Start()
You can set timeout for execution:
#!go

	wg := awg.AdvancedWaitGroup{}
	
	// Add function one
	wg.Add(func() error {
		//Logic
		return nil
	})

	wg.SetTimeout(time.Minute).Start()

You can break execution after first error:
#!go

	wg := awg.AdvancedWaitGroup{}
	
	// Add function one
	wg.Add(func() error {
		//Logic
		return nil
	})

	// Add function two
	wg.Add(func() error {
		//Another Logic
		return nil
	})

	wg.SetStopOnError(true).Start()
You can reset state by .Reset() function

Documentation

Index

Constants

View Source
const (
	// StatusIdle means that WG did not run yet
	StatusIdle int = iota
	// StatusSuccess means successful execution of all tasks
	StatusSuccess
	// StatusTimeout means that job was broken by timeout
	StatusTimeout
	// StatusError means that job was broken by error in one task (if stopOnError is true)
	StatusError
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AdvancedWaitGroup

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

AdvancedWaitGroup enhanced wait group struct

func (*AdvancedWaitGroup) Add

Add adds new task in waitgroup

func (*AdvancedWaitGroup) AddSlice

AddSlice adds new tasks in waitgroup

func (*AdvancedWaitGroup) CheckStatus

func (wg *AdvancedWaitGroup) CheckStatus(status int) bool

CheckStatus return result of status compare

func (*AdvancedWaitGroup) GetAllErrors

func (wg *AdvancedWaitGroup) GetAllErrors() []error

GetAllErrors returns all errors that caught by execution process

func (*AdvancedWaitGroup) GetCapacity

func (wg *AdvancedWaitGroup) GetCapacity() int

GetCapacity defines tasks channel capacity

func (*AdvancedWaitGroup) GetLastError

func (wg *AdvancedWaitGroup) GetLastError() error

GetLastError returns last error that caught by execution process

func (*AdvancedWaitGroup) Reset

func (wg *AdvancedWaitGroup) Reset()

Reset performs cleanup task queue and reset state

func (*AdvancedWaitGroup) SetCapacity

func (wg *AdvancedWaitGroup) SetCapacity(c int) *AdvancedWaitGroup

SetCapacity defines tasks channel capacity

func (*AdvancedWaitGroup) SetStopOnError

func (wg *AdvancedWaitGroup) SetStopOnError(b bool) *AdvancedWaitGroup

SetStopOnError make wiatgroup stops if any task returns error

func (*AdvancedWaitGroup) SetTimeout

func (wg *AdvancedWaitGroup) SetTimeout(t time.Duration) *AdvancedWaitGroup

SetTimeout defines timeout for all tasks

func (*AdvancedWaitGroup) Start

func (wg *AdvancedWaitGroup) Start() *AdvancedWaitGroup

Start runs tasks in separate goroutines

func (*AdvancedWaitGroup) Status

func (wg *AdvancedWaitGroup) Status() int

Status return result state string

func (*AdvancedWaitGroup) WithContext

func (wg *AdvancedWaitGroup) WithContext(ctx context.Context) *AdvancedWaitGroup

WithContext make wiatgroup work with context timeout and Done

type ErrorTimeout

type ErrorTimeout time.Duration

ErrorTimeout error on timeout

func (ErrorTimeout) Error

func (e ErrorTimeout) Error() string

Error implementation

type WaitgroupFunc

type WaitgroupFunc func() error

WaitgroupFunc func

Jump to

Keyboard shortcuts

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