async

package module
v2.1.1+incompatible Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2018 License: MIT Imports: 3 Imported by: 1

README

async

Build Status Go Report Card GoDoc

A collection of methods for running functions concurrently.

Installation

go get -u github.com/eleniums/async

Example

Create some tasks to run:

foo := func() error {
    // do something
    return nil
}

bar := func() error {
    // do something else
    return nil
}

Run the tasks concurrently:

errc := async.Run(foo, bar)
err := async.Wait(errc)
if err != nil {
    log.Fatalf("task returned an error: %v", err)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HandleError

func HandleError(errc <-chan error, handler func(error))

HandleError sets a handler function to be called anytime an error is received on the given channel.

func Run

func Run(tasks ...Task) <-chan error

Run will execute the given tasks concurrently and return any errors.

func RunForever

func RunForever(ctx context.Context, concurrent int, task Task) <-chan error

RunForever will execute the given task repeatedly on a set number of goroutines and return any errors. Context can be used to cancel execution of additional tasks.

func RunLimited

func RunLimited(ctx context.Context, concurrent int, count int, task Task) <-chan error

RunLimited will execute the given task a set number of times on a set number of goroutines and return any errors. Total times the task will be executed is equal to concurrent multiplied by count. Context can be used to cancel execution of additional tasks.

func Wait

func Wait(errc <-chan error) error

Wait until channel is closed or error is received.

Types

type Task

type Task func() error

Task is a function that can be run concurrently.

type TaskPool

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

TaskPool limits the number of concurrent tasks being processed to a given max.

func NewTaskPool

func NewTaskPool(max int) *TaskPool

NewTaskPool creates a new task pool that will limit concurrent tasks to max.

func (*TaskPool) Run

func (p *TaskPool) Run(ctx context.Context, task Task) <-chan error

Run will block until there is available capacity and then execute the given task. Cancelling the context will stop the task from being started.

func (*TaskPool) Wait

func (p *TaskPool) Wait() error

Wait until all tasks have finished processing.

Jump to

Keyboard shortcuts

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