Documentation ¶
Overview ¶
Package goodroutine provide some generic maintenance utilities generally useful for server implementations. The goal of this package is to provide predictability of initialization, intervals, retries, and panic recovery to maintenance functions that would otherwise just run bare in goroutines. It also includes often needed aspects of server's health.
Features include: - interval-based goroutine that safely runs a function - threshold based up / down healthcheck
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type FileChangeRoutine ¶
type FileChangeRoutine struct { OnFileChange func(file string, stat os.FileInfo, err error) IntervalRoutine // contains filtered or unexported fields }
FileChangeRoutine implements an interval routine that calls a function on file change. A file change is detected when the OS reported file ModTime or size has changed. Some important notes: - the error interval is only triggered by an error returned by the function, not by file stat error - the first run of Stats on file does not trigger the function (not considered a change) - file stat error on a file only triggers a change once
func NewFileChangeRoutine ¶
func NewFileChangeRoutine(f func() error, runInterval time.Duration, retryInterval time.Duration) *FileChangeRoutine
NewFileChangeRoutine creates a new FileChangeRoutine, which takes care of running f(). Parameters are equivalent to IntervalRoutine.
func (*FileChangeRoutine) AddFiles ¶
func (fcr *FileChangeRoutine) AddFiles(files ...string)
AddFiles adds files to watch for updates. Parameter is a list of file paths, empty path are ignored. This function must be called prior to calling Start()
type HealthChecker ¶
type HealthChecker struct { // OnUp is called when state changes to up, numDowns is number of prior downs OnUp func(numUps int, numDowns int) // OnDown is called when state changes to down, numUps is number of prior ups, lastErr is last error recorded OnDown func(numUps int, numDowns int, lastErr error) // NoRecover if set to true, panics are not recovered NoRecover bool // FastStart if set to true, threshold fully apply from start FastStart bool // contains filtered or unexported fields }
HealthChecker implements a health check, using a threshold for up / down logic. It can be combined with an IntervalRoutine to implement a health check goroutine.
func NewHealthChecker ¶
func NewHealthChecker(runner Runner, defaultState bool, thresholdUp int, thresholdDown int) *HealthChecker
NewHealthChecker creates a new HealthChecker. f is the function to run to obtain the health. defaultState is the default up / down state before any run occurs. thresholdUp defines the number of non-error runs before going from down to up. thresholdDown defines the number of error runs before going from up to down. By default it uses fast start, which means the 1st run will reset the state. A typical usage is a defaultState of false, thresholdUp of 3 and thresholdDown of 5.
func (*HealthChecker) IntervalRun ¶
func (hrt *HealthChecker) IntervalRun() error
IntervalRun implements the Runner interface
func (*HealthChecker) IsUp ¶
func (hrt *HealthChecker) IsUp() bool
IsUp returns the current state, up (true) or down (false)
func (*HealthChecker) LastErr ¶
func (hrt *HealthChecker) LastErr() error
LastErr returns the last error
func (*HealthChecker) Reset ¶
func (hrt *HealthChecker) Reset(newState bool)
Reset sets the healthcheck to the given state, resetting all other aspects.
type IntervalRoutine ¶
type IntervalRoutine struct { // PanicRecoverDisabled if set to true, panics are not recovered PanicRecoverDisabled bool // RetryBackoffDisabled if set to true, retry interval does not increase exponentially RetryBackoffDisabled bool OnPanic func(recovered interface{}) // contains filtered or unexported fields }
IntervalRoutine implements a management goroutine. It provides a safe way to run a function, at interval, from a single goroutine.
func NewIntervalRoutine ¶
func NewIntervalRoutine(runner Runner, runInterval time.Duration, retryInterval time.Duration) *IntervalRoutine
NewIntervalRoutine creates a new IntervalRoutine. Runs may be triggered in 3 ways: - normally at each run interval - at the retry interval, if the last run returned an error - if TriggerRun was called A typical usage is a runInterval of 5min, retryInterval of 30sec. By default the retry interval increases exponentially from retryInterval up to runInterval. retryInterval cannot be set higher than runInterval.
func (*IntervalRoutine) TriggerRun ¶
func (rrt *IntervalRoutine) TriggerRun()
TriggerRun triggers a run as soon as possible. Does nothing if a forced run is already scheduled.
type Runner ¶
type Runner interface {
IntervalRun() error
}
Runner implements a function that is run at interval
type RunnerFunc ¶
type RunnerFunc func() error
The RunnerFunc type is an adapter to allow the use of ordinary functions as Runner. If f is a function with the appropriate signature, RunnerFunc(f) is a Runner that calls f.
func (RunnerFunc) IntervalRun ¶
func (rf RunnerFunc) IntervalRun() error
IntervalRun implements the Runner interface