routines

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2024 License: GPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package routines provides utilities for managing long-running goroutines.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Func

type Func func(context.Context) error

Func is a routine created from a run function.

func (Func) Run

func (fun Func) Run(ctx context.Context) error

Run runs the routine.

type InitRoutine

type InitRoutine interface {
	Routine

	// Init is an initialization function that is called before any routines
	// are run, including this one. Routines must not depend on eachother in
	// this function.
	Init(context.Context) error
}

InitRoutine is a routine that has to be initialized before it can be run.

type Manager

type Manager struct {
	// Routines specifies a list of routines to manage. These are started
	// when Run() is called.
	Routines []Routine

	// RestartDeadline specifies the amount of time a routine has to be
	// running before failing to be restarted. This is to prevent routines
	// that immediately fail from just being restarted over and over again.
	// Defaults to 32 seconds if not set.
	RestartDeadline time.Duration

	// Logger, if non-nil, is where log messages will be written to. If it
	// is nil, messages will be written to the standard logger. To disable
	// logging altogether, this can be set to io.Discard.
	Logger io.Writer
	// contains filtered or unexported fields
}

Manager is a system capable of managing multiple routines, and restarting them if they fail.

func (*Manager) Append

func (this *Manager) Append(routines ...Routine)

Append adds one or more routines to the Routines slice. This has no effect if the manager is already running.

func (*Manager) Run

func (this *Manager) Run(ctx context.Context) error

Run spawns all routines in the Routines slice. If a routine exits with an error and it was running for longer than RestartDeadline, it is restarted. Run returns only when all routines have exited.

type Routine

type Routine interface {
	// Run is a long-running function that does not return until it is
	// finished, or its context is cancelled. If the context is cancelled,
	// the function must perform necessary cleanup/shutdown operations and
	// exit. An error is returned if the routine exited due to an error.
	Run(context.Context) error
}

Routine is an object that can be run and stopped.

Jump to

Keyboard shortcuts

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