alloter

package module
v1.2.4 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2021 License: MIT Imports: 6 Imported by: 0

README

Introduction

GoDoc

Alloter is a goroutine's concurrent toolkit to help execute functions concurrently in an efficient and safe way.

It was inspired by a Node.js package's function, bluebird .map()

  • It supports concurrency limits.
  • It supports recovery goroutine's panic.
  • It supports specifying the overall timeout to avoid blocking.
  • It supports the use of goroutines pool(invoke ants/v2).
  • It supports context passing; listen ctx.Done(), will return.
  • It supports ending other tasks when an error occurs.

init Alloter

// simple concurrency
func NewAlloter() *Alloter

// concurrency control
func NewCtrlAlloter(workerNum int) *Alloter

// goroutines pool from ants/v2
func NewPooledAlloter(workerNum int) *Alloter

exec Alloter

type Task func() error

func (c *Alloter) Exec(tasks *[]Task) error

func (c *Alloter) ExecWithContext(ctx context.Context, tasks *[]Task) error

Demo!!!

    func (that *Controller) TestGoRunLock(ctx echo.Context) {
        userIds := []string{
            "uuid_1",
            "uuid_2",
        }
        tasks := []alloter.Task{}
        users := []third_parts.User{}
        mux := sync.Mutex{}
        for _, uid := range userIds {
            func (uid string) {
                tasks = append(tasks, func() error {
                user, resErr := third_parts.GetUserById(uid)
                if resErr != nil {
                    return resErr
                }
                mux.Lock()
                users = append(users, user)
                mux.Unlock()
                return nil
                })
            }(uid)
        }
        p := alloter.NewCtrlAlloter(1)
        err = p.ExecWithContext(ctx.Request().Context(), &tasks)
        if err != nil {
            ctx.JSON(500, err.Error())
            return
        }
        ctx.JSON(200, users)
        return
    }

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrorUsingAlloter = fmt.Errorf("ErrorUsingActuator")

Functions

This section is empty.

Types

type Alloter

type Alloter struct{}

func NewAlloter

func NewAlloter() *Alloter

func (*Alloter) Exec

func (c *Alloter) Exec(tasks *[]Task) error

func (*Alloter) ExecWithContext

func (c *Alloter) ExecWithContext(ctx context.Context, tasks *[]Task) error

type BaseActuator added in v1.2.2

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

type CtrlAlloter added in v1.2.2

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

func NewCtrlAlloter added in v1.2.2

func NewCtrlAlloter(workerNum int) *CtrlAlloter

func (*CtrlAlloter) Exec added in v1.2.2

func (c *CtrlAlloter) Exec(tasks *[]Task) error

func (*CtrlAlloter) ExecWithContext added in v1.2.2

func (c *CtrlAlloter) ExecWithContext(ctx context.Context, tasks *[]Task) error

type GoroutinePool

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

type PooledAlloter

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

func NewPooledAlloter

func NewPooledAlloter(workerNum int) *PooledAlloter

func (*PooledAlloter) Exec

func (c *PooledAlloter) Exec(tasks *[]Task) error

Exec is used to run tasks concurrently

func (*PooledAlloter) ExecWithContext

func (c *PooledAlloter) ExecWithContext(ctx context.Context, tasks *[]Task) error

func (*PooledAlloter) Release

func (c *PooledAlloter) Release()

func (*PooledAlloter) WithPool

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

WithPool will support for using custom goroutine pool

type Task

type Task func() error

type TimedAlloter

type TimedAlloter interface {
	BaseActuator
}

Jump to

Keyboard shortcuts

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