supervisor

package module
v0.0.0-...-7292f5c Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2017 License: MIT Imports: 3 Imported by: 1

README

supervisor

note

Use package retry instead, which has a simpler and more strict semantics. This package contains code smells that are remnants of it's history in trying to implement supervisor trees in Erlang.

A try in implementing supervision trees (as in Erlang; look into git history), that ended up in a function!

See documents.

Get it via:

$ go get -u gopkg.in/dc0d/supervisor.v1

Or for development version:

$ go get -u github.com/dc0d/supervisor

Documentation

Overview

Package supervisor provides supervision utilities for running a function, and retry it if there is an error or panic, for a certain number of times (or infinitely), periodically, at the specified periods.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Supervise

func Supervise(
	action func() error,
	options ...Option)

Supervise a helper method, runs in sync, use as "go Supervise(...)" if should run concurrently, takes care of restarts (in case of panic or error), can be used as a SimpleOneForOne supervisor, an intensity < 0 means restart forever. It is sync because in most cases we do not need to escape the scope of current goroutine.

Example
// if should run concurrently then use "go Supervise(...)"
Supervise(func() error {
	fmt.Println("done")
	return nil
})
Output:

done

Types

type Option

type Option func(supervisorConf) supervisorConf

Option represents a supervision option

func Intensity

func Intensity(intensity int) Option

Intensity of -1 means run worker forever.

Example
Supervise(func() error {
	fmt.Println(1)
	return errors.Errorf("FAILED")
},
	Intensity(3),
	Period(time.Millisecond*50))
Output:

1
1
1

func OnError

func OnError(onError func(error)) Option

OnError will get called in case of an error

Example (Error)
Supervise(func() error {
	return errors.Errorf("FAILED")
},
	Intensity(3),
	Period(time.Millisecond*50),
	OnError(func(err error) { fmt.Println(err) }))
Output:

FAILED
FAILED
FAILED
Example (Panic)
Supervise(func() error {
	panic(errors.Errorf("FAILED"))
},
	Intensity(3),
	Period(time.Millisecond*50),
	OnError(func(err error) { fmt.Println(err) }))
Output:

FAILED
FAILED
FAILED

func Period

func Period(period time.Duration) Option

Period of time to sleep between restarts

Example
startedAt := time.Now()
Supervise(func() error {
	return errors.Errorf("FAILED")
},
	Intensity(3),
	Period(time.Millisecond*50),
	OnError(func(err error) { fmt.Println(time.Since(startedAt).Round(time.Millisecond * 5)) }))
Output:

0s
50ms
100ms

Jump to

Keyboard shortcuts

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