loop

package
v0.0.0-...-a2846b3 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2016 License: BSD-3-Clause Imports: 3 Imported by: 0

Documentation

Overview

The Tideland Go Library loop package is intended to support the developer implementing the typical Go idiom for concurrent applications running in a loop in the background and doing a select on one or more channels. Stopping those loops or getting aware of internal errors requires extra efforts. The loop package helps to control this kind of goroutines.

Index

Examples

Constants

View Source
const (
	Running = iota
	Stopping
	Stopped
)

Status of the loop.

Variables

This section is empty.

Functions

func PackageVersion

func PackageVersion() version.Version

PackageVersion returns the version of the version package.

Types

type Loop

type Loop interface {
	// Stop tells the loop to stop working without a passed error and
	// waits until it is done.
	Stop() error

	// Kill tells the loop to stop working due to the passed error.
	// Here only the first error will be stored for later evaluation.
	Kill(err error)

	// Wait blocks the caller until the loop ended and returns the error.
	Wait() (err error)

	// Error returns the current status and error of the loop.
	Error() (status int, err error)

	// ShallStop returns a channel signalling the loop to
	// stop working.
	ShallStop() <-chan struct{}

	// IsStopping returns a channel that can be used to wait until
	// the loop is stopping or to avoid deadlocks when communicating
	// with the loop.
	IsStopping() <-chan struct{}
}

Loop manages running loops in the background as goroutines.

func Go

func Go(lf LoopFunc) Loop

Go starts the loop function in the background. The loop can be stopped or killed. This leads to a signal out of the channel Loop.ShallStop(). The loop then has to end working returning a possible error. Wait() then waits until the loop ended and returns the error.

func GoRecoverable

func GoRecoverable(lf LoopFunc, rf RecoverFunc) Loop

GoRecoverable starts the loop function in the background. The loop can be stopped or killed. This leads to a signal out of the channel Loop.ShallStop(). The loop then has to end working returning a possible error. Wait() then waits until the loop ended and returns the error.

If the loop panics a Recovering is created and passed with all Recoverings before to the RecoverFunc. If it returns nil the loop will be started again. Otherwise the loop will be killed with that error.

type LoopFunc

type LoopFunc func(l Loop) error

LoopFunc is managed loop function.

Example
package main

import (
	"github.com/tideland/golib/loop"
)

func main() {
	printChan := make(chan string)
	loopFunc := func(l loop.Loop) error {
		for {
			select {
			case <-l.ShallStop():
				return nil
			case str := <-printChan:
				println(str)
			}
		}
	}
	loop.Go(loopFunc)
}
Output:

type RecoverFunc

type RecoverFunc func(rs Recoverings) (Recoverings, error)

RecoverFunc decides if a loop shall be started again or end with an error. It is also responsible to trim the list of revocerings if needed.

Example
package main

import (
	"errors"

	"github.com/tideland/golib/loop"
)

func main() {
	printChan := make(chan string)
	loopFunc := func(l loop.Loop) error {
		for {
			select {
			case <-l.ShallStop():
				return nil
			case str := <-printChan:
				println(str)
			}
		}
	}
	recoverFunc := func(rs loop.Recoverings) (loop.Recoverings, error) {
		if len(rs) >= 5 {
			return nil, errors.New("too many panics")
		}
		return rs, nil
	}
	loop.GoRecoverable(loopFunc, recoverFunc)
}
Output:

type Recovering

type Recovering struct {
	Time   time.Time
	Reason interface{}
}

Recovering stores time and reason of one of the recoverings.

type Recoverings

type Recoverings []*Recovering

Recoverings is a list of recoverings a loop already had.

func (Recoverings) First

func (rs Recoverings) First() *Recovering

Last returns the last recovering.

func (Recoverings) Frequency

func (rs Recoverings) Frequency(num int, dur time.Duration) bool

Frequency checks if a given number of restarts happened during a given duration.

func (Recoverings) Last

func (rs Recoverings) Last() *Recovering

Last returns the last recovering.

func (Recoverings) Len

func (rs Recoverings) Len() int

Len returns the length of the recoverings.

func (Recoverings) Trim

func (rs Recoverings) Trim(l int) Recoverings

Trim returns the last recoverings defined by l. This way the recover func can con control the length and take care that the list not grows too much.

Jump to

Keyboard shortcuts

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