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 ¶
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 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.