goroutines

package module
v0.0.0-...-828fd4c Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2017 License: MIT Imports: 3 Imported by: 0

README

goroutines

This package provides utilities to perform common tasks on goroutines - waiting for a goroutine to start, timeouts, do somethting before or after a goroutine function inside the same goroutine and recover from panic. It has a simple fluent API (see tests).

For example, here we wait for the goroutine to start, also perform some action afterward in a deffered manner, so even if the goroutine panics, it would get done. Also we recover from panic and handle the error:

err := New().
	EnsureStarted().
	Recover(func(e interface{}) {
		// error (panic) handling ...
	}).
	After(func() {
		result += `3`
	}, true).
	Go(func() {
		result += `2`
		panic(`ERR`)
	})

if err != nil {
    // ...
}

Other patterns also can be implemented using these basic abstractions, such as a simple supervisor:

func simpleSupervisor(intensity int, fn func()) {
	if intensity <= 0 {
		return
	}
	intensity--
	New().
		Recover(func(e interface{}) {
			time.Sleep(time.Millisecond * 10)
			go simpleSupervisor(intensity, fn)
		}).
		Go(fn)
}

Here simpleSupervisor(...) will restart a goroutine for a function, in case of a panic(...), number of intensity times. And we can use it as:

go simpleSupervisor(9, func() {
	// ...
})

This restarts the function at most, nine times and then stops. If we needed the function to run forever, we could simply drop intensity, or handle a value of -1.

Documentation

Overview

Package goroutines provides utilities to perform common tasks on goroutines

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrTimeout is a timeout error
	ErrTimeout error = _error(`TIMEOUT`)
)

Functions

This section is empty.

Types

type Go

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

Go provides a fluent way to prepare & start a goroutine

func New

func New() Go

New creates an instange of Go struct

func (Go) AddToGroup

func (x Go) AddToGroup(wg *sync.WaitGroup) Go

AddToGroup registers the goroutine in a sync.WaitGroup by adding necessary code (Add/Done)

func (Go) After

func (x Go) After(after func(), deferred ...bool) Go

After will get called after the goroutine func, it can be deferred

func (Go) Before

func (x Go) Before(before func()) Go

Before will be called before the goroutine func at the begining of the same goroutine

func (Go) EnsureStarted

func (x Go) EnsureStarted() Go

EnsureStarted instructs Go to start a goroutine and wait for it to start, and after goroutine started, it returns.

func (Go) Go

func (x Go) Go(f func()) error

Go is final call in the fluent chain

func (Go) Recover

func (x Go) Recover(recoverFunc func(interface{})) Go

Recover recovers from panic and returns error, or returns the provided error

func (Go) Timeout

func (x Go) Timeout(timeout time.Duration) Go

Timeout sets a timeout and waits for f to complete (in a goroutine) or times out (returning ErrTimeout). A negative value for timeout, means waiting infinitely.

func (Go) WithContext

func (x Go) WithContext(ctx context.Context, f func(context.Context)) error

WithContext is same as Go(...) but also accepts a context and if a timeout larger than zero is set, creates a child context and cencels it on timeout

Jump to

Keyboard shortcuts

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