Giter Site home page Giter Site logo

tomwright / lifetime Goto Github PK

View Code? Open in Web Editor NEW
18.0 3.0 1.0 15 KB

A basic go package to handle the lifetime / lifecycle of an application providing easy to use graceful shutdowns.

Go 100.00%
lifetime shutdown graceful-shutdown graceful gracefully services service lifecycle lifecycle-management lifecycle-manager

lifetime's Introduction

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.

Deprecated

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.

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)

lifetime's People

Contributors

tomwright avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

syllogy

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.