conexec

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2020 License: BSD-2-Clause Imports: 8 Imported by: 0

README

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 
	}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrorTimeOut = fmt.Errorf("TimeOut")

ErrorTimeOut is the error when executes tasks timeout

View Source
var (
	// ErrorUsingActuator is the error when goroutine pool has exception
	ErrorUsingActuator = fmt.Errorf("ErrorUsingActuator")
)

Functions

func DurationPtr

func DurationPtr(t time.Duration) *time.Duration

DurationPtr helps to make a duration ptr

func Exec

func Exec(tasks ...Task) bool

Exec simply runs the tasks concurrently True will be returned is all tasks complete successfully otherwise false will be returned

func ExecWithError

func ExecWithError(tasks ...Task) error

ExecWithError simply runs the tasks concurrently nil will be returned is all tasks complete successfully otherwise custom error will be returned

Types

type Actuator

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

Actuator is the base struct

func NewActuator

func NewActuator(opt ...*Options) *Actuator

NewActuator creates an Actuator instance

func (*Actuator) Exec

func (c *Actuator) Exec(tasks ...Task) error

Exec is used to run tasks concurrently

func (*Actuator) ExecWithContext

func (c *Actuator) ExecWithContext(ctx context.Context, tasks ...Task) error

ExecWithContext is used to run tasks concurrently Return nil when tasks are all completed successfully, or return error when some exception happen such as timeout

func (*Actuator) GetTimeout

func (c *Actuator) GetTimeout() *time.Duration

GetTimeout return the timeout set before

type BaseActuator

type BaseActuator interface {
	Exec(tasks ...Task) error
	ExecWithContext(ctx context.Context, tasks ...Task) error
}

BaseActuator is the actuator interface

type GoroutinePool

type GoroutinePool interface {
	Submit(f func()) error
	Release()
}

GoroutinePool is the base routine pool interface User can use custom goroutine pool by implementing this interface

type Options

type Options struct {
	TimeOut *time.Duration
}

Options use to init actuator

type PooledActuator

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

PooledActuator is a actuator which has a worker pool

func NewPooledActuator

func NewPooledActuator(workerNum int, opt ...*Options) *PooledActuator

NewPooledActuator creates an PooledActuator instance

func (*PooledActuator) Exec

func (c *PooledActuator) Exec(tasks ...Task) error

Exec is used to run tasks concurrently

func (*PooledActuator) ExecWithContext

func (c *PooledActuator) ExecWithContext(ctx context.Context, tasks ...Task) error

ExecWithContext uses goroutine pool to run tasks concurrently Return nil when tasks are all completed successfully, or return error when some exception happen such as timeout

func (*PooledActuator) GetTimeout

func (c *PooledActuator) GetTimeout() *time.Duration

GetTimeout return the timeout set before

func (*PooledActuator) Release

func (c *PooledActuator) Release()

Release will release the pool

func (*PooledActuator) WithPool

func (c *PooledActuator) WithPool(pool GoroutinePool) *PooledActuator

WithPool will support for using custom goroutine pool

type Task

type Task func() error

Task Type

type TimedActuator

type TimedActuator interface {
	BaseActuator
	GetTimeout() *time.Duration
	// contains filtered or unexported methods
}

TimedActuator is the actuator interface within timeout method

Jump to

Keyboard shortcuts

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