exitplan

package module
v1.0.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 4, 2021 License: Apache-2.0 Imports: 8 Imported by: 0

README

Exit Plan

Go Report Card GoDoc

Simple go package to help with the shutdown process within a go program by integrating with SIGINT and SIGTERM. The purpose is to be used with a container orchestration software like kubernetes where heath checks are done on the pod.

This can be used with or without the use of an HTTP server serving the heath check data.

Demo and Details

This repo has the example in it already using the cobra package.

Clone down the repo and build it.

Upon running executing exitplan test, an HTTP server on port 8855 will be running.

Browse to http://localhost:8855/readyz to check the status!


Now that the server is running, and the page is responding with "ok", you can now press COMMAND+C / CTRL+C in the terminal window to trigger a SIGTERM.

After you trigger the SIGTERM, two actions take place. First a flag is set to indicate it's going to terminate (which changes the response for /readyz), then a grace period is waiting to complete.

After the grade period has passed it kicks into action again with a timeout for the deadline of the program to exit. Before reaching the deadline, goroutines are being made to call the shutdown methods that have been registered.

Lastly before the program exists it makes one last synchronous call to the FinalCallback.

Usage

package main

import (
	"context"
	"fmt"
	"net/http"
	"time"

	"github.com/gorilla/mux"
	"github.com/Kashoo/exitplan"
)

func main() {

	m := mux.NewRouter()

	srv := &http.Server{
		Handler:      m,
		Addr:         "127.0.0.1:8855",
		WriteTimeout: 5 * time.Second,
		ReadTimeout:  5 * time.Second,
	}

	plan := exitplan.NewPlan()
	plan.Add("http", srv.Shutdown)

	// Register a Request Handler on "/readyz" for the status.
	m.HandleFunc("/readyz", plan.HandlerFunc).Methods(http.MethodGet)

	go srv.ListenAndServe()

	fmt.Println("Server Running")
	fmt.Println("Goto http://localhost:8855/readyz")

	plan.Finally(func (ctx context.Context) error {
		// Do some final cool stuff before death
		fmt.Println("final callback made")
		return nil
	})
	plan.Wait()

}

Inspired By

Gracefully Shutdown your Go Application by Alfian Dhimas

Graceful shutdown with Go http servers and Kubernetes rolling updates by Wayne Ashley Berry

Stackoverflow: Testing graceful shutdown on an HTTP server during a Kubernetes rollout

terminus Graceful shutdown and Kubernetes readiness / liveness checks for any Node.js HTTP applications by GoDaddy Dev Team

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ExecutionPlan

type ExecutionPlan struct {
	Signals     []os.Signal
	Timeout     time.Duration
	GradePeriod time.Duration
	// contains filtered or unexported fields
}

func NewPlan

func NewPlan() *ExecutionPlan

NewPlan will create a new ExecutionPlan with a default

GradePeriod of 5 seconds and Timeout of 25 seconds

func NewPlanWithTimer

func NewPlanWithTimer(gradePeriod, timeout time.Duration) *ExecutionPlan

func (*ExecutionPlan) Add

func (p *ExecutionPlan) Add(name string, handler ExitOperation) *ExecutionPlan

func (*ExecutionPlan) AddMany

func (p *ExecutionPlan) AddMany(many map[string]ExitOperation) *ExecutionPlan

func (*ExecutionPlan) Finally

func (p *ExecutionPlan) Finally(handler ExitOperation) *ExecutionPlan

func (*ExecutionPlan) HandlerFunc

func (p *ExecutionPlan) HandlerFunc(w http.ResponseWriter, r *http.Request)

HandlerFunc is used on the HTTP Server Side to support a RESTful way of ready state. See https://kubernetes.io/docs/reference/using-api/health-checks/ for more information

func (*ExecutionPlan) IsTerminating

func (p *ExecutionPlan) IsTerminating() bool

func (*ExecutionPlan) Wait

func (p *ExecutionPlan) Wait()

func (*ExecutionPlan) WaitContext

func (p *ExecutionPlan) WaitContext(ctx context.Context)

WaitContext will wait until the program gets an exit signal and all handlers have succeeded. If used on the main thread, this will allow it to die

func (*ExecutionPlan) WaitWithChan

func (p *ExecutionPlan) WaitWithChan(ctx context.Context) <-chan struct{}

type ExitOperation

type ExitOperation func(ctx context.Context) error

ExitOperation is a clean up function on shutting down

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL