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)
lifetime's People
Forkers
syllogyRecommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.