Giter Club home page Giter Club logo

awg's Introduction

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

awg's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.