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 ¶
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 ¶
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 ¶
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 ¶
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