• Stars
    star
    16
  • Rank 1,311,288 (Top 26 %)
  • Language
    Go
  • License
    BSD 2-Clause "Sim...
  • Created almost 5 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A concurrent toolkit to help execute funcs concurrently in an efficient and safe way. It supports specifying the overall timeout to avoid blocking.

Introduction

Build Status codecov Go Report Card GoDoc

conexec is a concurrent toolkit to help execute functions concurrently in an efficient and safe way. It supports specifying the overall timeout to avoid blocking.

How to use

Generally it can be set as a singleton to save memory. There are some example to use it.

Normal Actuator

Actuator is a base struct to execute functions concurrently.

	opt := &Options{TimeOut:DurationPtr(time.Millisecond*50)}
	c := NewActuator(opt)
	
	err := c.Exec(
		func() error {
			fmt.Println(1)
			time.Sleep(time.Second * 2)
			return nil
		},
		func() error {
			fmt.Println(2)
			return nil
		},
		func() error {
			time.Sleep(time.Second * 1)
			fmt.Println(3)
			return nil
		},
	)
	
	if err != nil {
		// ...do sth
	}

Pooled Actuator

Pooled actuator uses the goroutine pool to execute functions. In some times it is a more efficient way.

	opt := &Options{TimeOut:DurationPtr(time.Millisecond*50)}
	c := NewPooledActuator(5, opt)
	
	err := c.Exec(...)
	
	if err != nil {
		// ...do sth
	}

Use custom goroutine pool

	c := NewPooledActuator(5).WithPool(pool)

Simply exec using goroutine

	done := Exec(...)

	if !done {
		// ... do sth 
	}