...
Tawesoft Logo

Package grace

import "tawesoft.co.uk/go/grace"
Overview
Index
Subdirectories

Overview ▾

Package grace implements a simple way to start multiple long-lived processes (e.g. goroutines) with cancellation, signal handling and graceful shutdown.

Examples

Start HTTP servers on multiple ports with graceful shutdown

https://www.tawesoft.co.uk/go/doc/grace/examples/multiportserver/

Package Information

License: MIT (see LICENSE.txt)

Stable: candidate

For more information, documentation, source code, examples, support, links, etc. please see https://www.tawesoft.co.uk/go and https://www.tawesoft.co.uk/go/grace

func Run

func Run(ctx context.Context, processes []Process, signals []os.Signal) (os.Signal, []error, error)

Run starts each process, waits for any signal in `notify` or until ctx is cancelled, then cancels each process. It blocks until all processes have been stopped with Stop() and all process Start() functions mark themselves as completely done().

The first return value is the os.Signal received.

The second return value is a list of errors for processes that returned errors when being cancelled.

The third return value returns any startup error.

The second return value may be non-empty even when the third return value is non-nil when there is both a startup error and an error stopping any previously started processes e.g. if process one starts but process two fails, then process one needs to be cancelled but may also run into an error cancelling.

type Process

Process defines a long-lived cancellable process

type Process interface {
    // Start launches a process. This shouldn't block, so launch goroutines or
    // other programs as necessary. Call done() when that goroutine returns
    // or that process has terminated. If Start returns an error, then done()
    // must not be called.
    Start(done func()) error

    // Stop stops a process. Derive from the context as necessary e.g.
    // use context.WithTimeout
    Stop(context.Context) error
}

Subdirectories

Name Synopsis
examples
multiportserver Start HTTP servers on multiple ports with graceful shutdown