Help us shape the future of Go! Take the 2026 Go Developer Survey

graceful

package
v0.0.12 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: MIT Imports: 7 Imported by: 2

Documentation

Overview

Package graceful provides a graceful start and shutdown insturmentation for go programs based on a simple Plugin interface.

It supports

  • graceful shutdown of plugins
  • timeout for plugins to shutdown
  • os.Signals to listen for (defaults to os.Interrupt and syscall.SIGTERM)

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrRunnerNotStarted     = errors.New("server not started")
	ErrRunnerAlreadyStarted = errors.New("server already started")
)

Functions

This section is empty.

Types

type Plugin

type Plugin interface {
	// Name returns the name of the plugin. This can be any identifier you like and
	// will only be used for logging and debugging purposes.
	Name() string

	// Start starts the plugin with the context provided for cancellation.
	// if the context is cancelled, the plugin should stop, cleanup and return.
	// The start method _should_ be blocking, until the context is cancelled.
	// If the plugin terminates early with an error, it will cause a shutdown
	// in the system.
	//
	// Errors during shutdown should be logged within your plugin. Errors
	// that occur during the shutdown process will be ignored by the server.
	//
	// Example:
	//   func (p *MyPlugin) Start(ctx context.Context) error {
	//     // do something
	//     <-ctx.Done()
	//     // cleanup / logging
	//     return nil
	//   }
	Start(ctx context.Context) error
}

Plugin defines the interface for a plugin that can be started and stopped via the Runner. The plugin should be blocking until the context is cancelled. Note that startup order for plugins is non-deterministic and should not be relied upon. If you need to will need to manage coordination between plugins.

func PluginFunc

func PluginFunc(name string, start func(ctx context.Context) error) Plugin

type Runner

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

Runner is the orchestrator of the plugins provided. It will start and cancel the plugins based on the context and os.Signals provided.

func NewRunner

func NewRunner(opts ...RunnerOptFunc) *Runner

func (*Runner) AddFunc added in v0.0.9

func (svr *Runner) AddFunc(name string, fn func(ctx context.Context) error)

AddFunc adds a function as a plugin to the server during construction. This is useful for quick plugin creation and for passing structs that only provide the Start method.

Example:

svr.AddFunc("my-plugin", func(ctx context.Context) error {
  // do something
  <-ctx.Done()
  // cleanup / logging
  return nil
}

func (*Runner) AddPlugin

func (svr *Runner) AddPlugin(p ...Plugin)

AddPlugin adds a plugin to the server during construction. This returns the server for chaining.

func (*Runner) Name

func (*Runner) Name() string

func (*Runner) Shutdown

func (svr *Runner) Shutdown()

Shutdown sends a signal to the server to stop all plugins and the server itself. This function returns immediately after the signal is sent.

func (*Runner) Start

func (svr *Runner) Start(ctx context.Context) error

Start start the server with a context provided for cancellation if the root context is cancelled, the server signal stops to all plugins registered.

Note that a new context is created with the provided signals defined when creating the server.

type RunnerOptFunc

type RunnerOptFunc func(*runnerOpts)

func WithPrintln

func WithPrintln(fn func(...any)) RunnerOptFunc

WithPrintln provides a function to print messages

func WithSignals

func WithSignals(signals ...os.Signal) RunnerOptFunc

WithSignals provides a list of signals to listen for when starting the server that will cancel the context

Defaults to

  • os.Interrupt
  • syscall.SIGTERM

Multiple calls to this option will override the previous

func WithTimeout

func WithTimeout(timeout time.Duration) RunnerOptFunc

WithTimeout provides a timeout for the server to wait for plugins to stop before shutting down.

Defaults to 5 seconds

Jump to

Keyboard shortcuts

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