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.
Please note that I am deprecating this package in favour of grace.
Grace has a simplified API and is split across different modules to reduce the number of forced dependencies.
go get github.com/tomwright/lifetime
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()
A service is a single service within your application that can be started and stopped.
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
orsyscall.SIGTERM
signal is received. lifetime.Shutdown
is called.
An immediate shutdown uses os.Exit
to immediately stop the application.
This will occur when:
- Multiple
syscall.SIGINT
orsyscall.SIGTERM
signals are received. - A
syscall.SIGKILL
signal is received.
Some services are provided for you to use, but you can easily create your own services by implementing the lifetime.Service
interface.
// 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)
// 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)