go-service/routines/routines.go

171 lines
4.4 KiB
Go
Raw Permalink Normal View History

2024-10-30 16:09:43 -06:00
// Package routines provides utilities for managing long-running goroutines.
package routines
import "io"
import "fmt"
import "log"
import "time"
import "sync"
import "errors"
2024-10-30 16:09:43 -06:00
import "context"
2024-10-30 16:09:43 -06:00
2024-10-30 16:09:43 -06:00
// Func is a routine created from a run function.
type Func func (context.Context) error
2024-10-30 16:09:43 -06:00
2024-10-30 16:09:43 -06:00
// Run runs the routine.
func (fun Func) Run (ctx context.Context) error {
return fun(ctx)
2024-10-30 16:09:43 -06:00
}
// Routine is an object that can be run and stopped.
type Routine interface {
// Run is a long-running function that does not return until it is
2024-10-30 16:09:43 -06:00
// 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
2024-10-30 16:09:43 -06:00
}
// InitRoutine is a routine that has to be initialized before it can be run.
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
}
2024-10-30 16:09:43 -06:00
// Manager is a system capable of managing multiple routines, and restarting
// them if they fail.
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.
2024-10-30 16:09:43 -06:00
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
loggerLock sync.Mutex
2024-10-30 16:09:43 -06:00
2024-10-30 16:09:43 -06:00
ctx context.Context
failed map[Routine] struct { }
failedLock sync.Mutex
2024-10-30 16:09:43 -06:00
}
// 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.
2024-10-30 16:09:43 -06:00
// Run returns only when all routines have exited.
func (this *Manager) Run (ctx context.Context) error {
ctx, done := context.WithCancel(ctx)
this.ctx = ctx
this.failed = make(map[Routine] struct { })
2024-10-30 16:09:43 -06:00
2024-10-30 16:09:43 -06:00
var waitGroup sync.WaitGroup
2024-10-30 16:09:43 -06:00
for _, routine := range this.Routines {
2024-10-30 16:09:43 -06:00
if routine != nil {
waitGroup.Add(1)
go this.initRoutine(routine, &waitGroup)
}
}
waitGroup.Wait()
for _, routine := range this.Routines {
if _, failed := this.failed[routine]; !failed {
2024-10-30 16:09:43 -06:00
waitGroup.Add(1)
2024-10-30 16:09:43 -06:00
go this.runRoutine(routine, &waitGroup)
2024-10-30 16:09:43 -06:00
}
}
this.failed = nil
2024-10-30 16:09:43 -06:00
waitGroup.Wait()
2024-10-30 16:09:43 -06:00
done()
return ctx.Err()
2024-10-30 16:09:43 -06:00
}
// Append adds one or more routines to the Routines slice. This has no effect if
// the manager is already running.
2024-10-30 16:09:43 -06:00
func (this *Manager) Append (routines ...Routine) {
this.Routines = append(this.Routines, routines...)
2024-10-30 16:09:43 -06:00
}
2024-10-30 16:09:43 -06:00
func (this *Manager) log (message ...any) {
this.loggerLock.Lock()
defer this.loggerLock.Unlock()
2024-10-30 16:09:43 -06:00
if this.Logger == nil {
2024-10-30 16:09:43 -06:00
log.Println(message...)
} else {
2024-10-30 16:09:43 -06:00
fmt.Fprintln(this.Logger, message...)
2024-10-30 16:09:43 -06:00
}
}
func (this *Manager) initRoutine (routine Routine, group *sync.WaitGroup) {
defer group.Done()
if initRoutine, ok := routine.(InitRoutine); ok {
err := initRoutine.Init(this.ctx)
if err != nil {
this.log("XXX routine failed to initialize:", err)
this.failedLock.Lock()
defer this.failedLock.Unlock()
this.failed[routine] = struct { } { }
}
}
}
2024-10-30 16:09:43 -06:00
func (this *Manager) runRoutine (routine Routine, group *sync.WaitGroup) {
2024-10-30 16:09:43 -06:00
defer group.Done()
restartDeadline := this.RestartDeadline
if restartDeadline == 0 {
restartDeadline = 32 * time.Second
}
2024-10-30 16:09:43 -06:00
for {
lastStart := time.Now()
2024-10-30 16:09:43 -06:00
err := panicWrap(routine.Run, this.ctx)
if ctxErr := this.ctx.Err(); ctxErr != nil {
if err == nil || errors.Is(err, context.Canceled) {
2024-10-30 16:09:43 -06:00
this.log("(i) stopped routine")
2024-10-30 16:09:43 -06:00
} else {
2024-10-30 16:09:43 -06:00
this.log("!!! stopped routine, with error:", err)
2024-10-30 16:09:43 -06:00
}
break
}
if err == nil {
2024-10-30 16:09:43 -06:00
this.log("(i) routine exited")
2024-10-30 16:09:43 -06:00
break
} else {
2024-10-30 16:09:43 -06:00
this.log("XXX routine failed:", err)
2024-10-30 16:09:43 -06:00
}
if time.Since(lastStart) < restartDeadline {
2024-10-30 16:09:43 -06:00
this.log("!!! not restarting routine, failed too soon")
2024-10-30 16:09:43 -06:00
break
} else {
2024-10-30 16:09:43 -06:00
this.log("(i) routine is being restarted")
2024-10-30 16:09:43 -06:00
}
}
}
2024-10-30 16:09:43 -06:00
func panicWrap (f func (context.Context) error, ctx context.Context) (err error) {
2024-10-30 16:09:43 -06:00
defer func () {
if pan := recover(); pan != nil {
err = errors.New(fmt.Sprint(pan))
}
} ()
2024-10-30 16:09:43 -06:00
err = f(ctx)
2024-10-30 16:09:43 -06:00
return
}