supervisor

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package supervisor provides a supervised goroutine manager inspired by Elixir's OTP Supervisor. Workers are restarted automatically on failure according to the chosen Strategy.

A worker function signals a clean exit by returning nil; returning a non-nil error triggers a restart. Workers should honour context cancellation so the supervisor can shut down cleanly.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ChildSpec

type ChildSpec struct {
	Name  string
	Start func(ctx context.Context) error
}

ChildSpec defines a supervised worker. Start runs in its own goroutine. A nil return signals a clean exit (no restart). A non-nil return signals a crash and triggers a restart according to the supervisor's Strategy. Start must honour context cancellation to allow clean shutdown.

type Strategy

type Strategy int

Strategy controls how the supervisor responds to a child failure.

const (
	// OneForOne restarts only the child that failed, leaving others running.
	OneForOne Strategy = iota
	// OneForAll restarts all children when any one of them fails.
	OneForAll
)

type Supervisor

type Supervisor struct {
	// contains filtered or unexported fields
}

Supervisor manages a set of worker goroutines with automatic restart. All methods are safe to call from multiple goroutines. Methods must not be called after Stop.

func Start

func Start(strategy Strategy, specs []ChildSpec) *Supervisor

Start launches the supervisor and all children immediately.

Example
package main

import (
	"context"
	"fmt"

	"github.com/mikehelmick/go-functional/supervisor"
)

func main() {
	ready := make(chan struct{})
	specs := []supervisor.ChildSpec{
		{
			Name: "worker",
			Start: func(ctx context.Context) error {
				close(ready)
				<-ctx.Done()
				return nil
			},
		},
	}
	sup := supervisor.Start(supervisor.OneForOne, specs)
	<-ready
	sup.Stop()
	fmt.Println("stopped")
}
Output:
stopped

func (*Supervisor) Stop

func (s *Supervisor) Stop()

Stop cancels all children and blocks until the supervisor loop exits. Calling Stop more than once is safe; subsequent calls are no-ops.

Jump to

Keyboard shortcuts

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