Categorygithub.com/tomwright/lifetime
modulepackage
1.0.1
Repository: https://github.com/tomwright/lifetime.git
Documentation: pkg.go.dev

# README

lifetime

Test Go Report Card Documentation

Lifetime is a basic package to help you manage the lifetime of an application with multiple routines running at once.

The main benefit of this module is that it allows you to easily manage graceful shutdowns without most of the boilerplate code that goes along with it.

Installation

go get github.com/tomwright/lifetime

Usage

Example usage can be found on godoc.

// Create and initialise the lifetime.
lt := lifetime.New(context.Background()).Init()

// Create HTTP server.
mux := http.NewServeMux()
mux.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
    writer.Write([]byte("Hello world"))
})
server := &http.Server{
    Addr:    ":80",
    Handler: mux,
}

// Create HTTP service, giving it the HTTP server.
service := lifetime.NewHTTPService(server)

// Start the service.
lt.Start(service)

go func() {
    // At some point in time call lt.Shutdown.
    // lt.Shutdown would also be executed when a shutdown signal is received.
    time.Sleep(time.Second * 5)
    lt.Shutdown()
}()

// Wait for all services to shutdown
lt.Wait()

Service

A service is a single service within your application that can be started and stopped.

Graceful shutdown

A graceful shutdown causes all of the Service.Stop funcs to be executed causing all services to begin their graceful shutdown.

You can use lifetime.Wait to wait for the services to be stopped.

A graceful shutdown will be triggered when:

  • A server Start func returns an error.
  • A syscall.SIGINT or syscall.SIGTERM signal is received.
  • lifetime.Shutdown is called.

Immediate shutdown

An immediate shutdown uses os.Exit to immediately stop the application.

This will occur when:

  • Multiple syscall.SIGINT or syscall.SIGTERM signals are received.
  • A syscall.SIGKILL signal is received.

Services

Some services are provided for you to use, but you can easily create your own services by implementing the lifetime.Service interface.

HTTP Server

// Create HTTP server.
mux := http.NewServeMux()
mux.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
    writer.Write([]byte("Hello world"))
})
server := &http.Server{
    Addr:    ":80",
    Handler: mux,
}

// Create HTTP service, giving it the HTTP server.
service := lifetime.NewHTTPService(server)

// Start the service.
lt.Start(service)

GRPC Server

// Create and register GRPC server.
var server *grpc.Server
// Register GRPC server implementation...

// Create GRPC service, giving it the GRPC server and a listen address.
service := lifetime.NewGRPCService(server, ":9000")

// Start the service.
lt.Start(service)

# Functions

New returns a new Lifetime instance that can be used to control the lifetime of an application.
NewGRPCService returns a service that will run listen and serve the given GRPC server.
NewHTTPService returns a service that will run listen and serve the given HTTP server.

# Variables

ErrImmediateShutdownSignalReceived is used when a shutdown signal is received for the second time.
ErrShutdownSignalReceived is used when a shutdown signal is received.

# Structs

Lifetime contains some basic functionality you can use to control the lifetime of an application.

# Interfaces

Service defines a single service in an application.