shutdown

package module
v0.0.0-...-ed8dbe7 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2016 License: MIT Imports: 6 Imported by: 3

README

old version

This package has been updated to a new version. If you are starting a new project, use the new package:

Version 2 mainly contains minor adjustments to the API to make it a bit easier to use.

This package remains here to maintain compatibility.

shutdown

Shutdown management library for Go

This package helps you manage shutdown code centrally, and provides functionality to execute code when a controlled shutdown occurs.

This will enable you to save data, notify other services that your application is shutting down.

GoDoc Build Status

concept

Managing shutdowns can be very tricky, often leading to races, crashes and strange behavior. This package will help you manage the shutdown process and will attempt to fix some of the common problems when dealing with shutting down.

The shutdown package allow you to block shutdown while certain parts of your code is running. This is helpful to ensure that operations are not interrupted.

The second part of the shutdown process is notifying goroutines in a select loop and calling functions in your code that handles various shutdown procedures, like closing databases, notifying other servers, deleting temporary files, etc.

The second part of the process has three stages, which will enable you to do your shutdown in stages. This will enable you to rely on some parts, like logging, to work in the first two stages. There is no rules for what you should put in which stage, but things executing in stage one can safely rely on stage two not being executed yet.

All operations have timeouts. This is to fix another big issue with shutdowns; applications that hang on shutdown. The timeout is for each stage of the shutdown process, and can be adjusted to your application needs. If a timeout is exceeded the next shutdown stage will be initiated regardless.

Finally, you can always cancel a notifier, which will remove it from the shutdown queue.

usage

First get the libary with go get -u github.com/klauspost/shutdown, and add it as an import to your code with import github.com/klauspost/shutdown.

The next thing you probably want to do is to register Ctrl+c and system terminate. This will make all shutdown handlers run when any of these are sent to your program:

	shutdown.OnSignal(0, os.Interrupt, syscall.SIGTERM)

If you don't like the default timeout duration of 5 seconds, you can change it by calling the SetTimeout function:

  shutdown.SetTimeout(time.Second * 1)

Now the maximum delay for shutdown is 4 seconds. The timeout is applied to each of the stages and that is also the maximum time to wait for the shutdown to begin. If you need to adjust a single stage, use SetTimeoutN function.

Next you can register functions to run when shutdown runs:

  logFile := os.Create("log.txt")
  
  // Execute the function in the first stage of the shutdown process
  _ = shutdown.FirstFunc(func(interface{}){
    logFile.Close()
  }, nil)
  
  // Execute this function in the second part of the shutdown process
  _ = shutdown.SecondFunc(func(interface{}){
    _ = os.Delete("log.txt")
  }, nil)

As noted there are three stages. All functions in one stage are executed in parallel, but the package will wait for all functions in one stage to have finished before moving on to the next one. So your code cannot rely on any particular order of execution inside a single stage, but you are guaranteed that the First stage is finished before any functions from stage two are executed.

You can send a parameter to your function, which is delivered as an interface{}. This way you can re-use the same function for similar tasks. See simple-func.go in the examples folder.

This example above uses functions that are called, but you can also request channels that are notified on shutdown. This allows you do have shutdown handling in blocked select statements like this:

  go func() {
    // Get a stage 1 notification
    finish := shutdown.First()
    select {
      case n:= <-finish:
        log.Println("Closing")
        close(n)
        return
  }

It is important that you close the channel you receive. This is your way of signalling that you are done. If you do not close the channel you get shutdown will wait until the timeout has expired before proceeding to the next stage.

If you for some reason don't need a notifier anymore you can cancel it. When a notifier has been cancelled it will no longer receive notifications, and the shutdown code will no longer wait for it on exit.

  go func() {
    // Get a stage 1 notification
    finish := shutdown.First()
    select {
      case n:= <-finish:
        close(n)
        return
      case <-otherchan:
        finish.Cancel()
        return
  }

Functions are cancelled the same way by cancelling the returned notifier. Be aware that if shutdown has been initiated you can no longer cancel notifiers, so you may need to aquire a shutdown lock (see below).

The final thing you can do is to lock shutdown in parts of your code you do not want to be interrupted by a shutdown, or if the code relies on resources that are destroyed as part of the shutdown process.

A simple example can be seen in this http handler:

	http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
		// Acquire a lock. 
		// While this is held server will not shut down (except after timeout)
		if !shutdown.Lock() {
			// Shutdown has started, return that the service is unavailable
			w.WriteHeader(http.StatusServiceUnavailable)
		} 
		defer shutdown.Unlock()
		io.WriteString(w, "Server running")
	})

If shutdown is started, either by a signal or by another goroutine, it will wait until the lock is released. It is important always to release the lock, if shutdown.Lock() returns true. Otherwise the server will have to wait until the timeout has passed before it starts shutting down, which may not be what you want.

Finally you can call shutdown.Exit(exitcode) to call all exit handlers and exit your application. This will wait for all locks to be released and notify all shutdown handlers and exit with the given exit code. If you want to do the exit yourself you can call the shutdown.Shutdown(), whihc does the same, but doesn't exit. Beware that you don't hold a lock when you call Exit/Shutdown.

Also there are some things to be mindful of:

  • Notifiers can be created inside shutdown code, but only for stages following the current. So stage 1 notifiers can create stage 2 notifiers, but if they create a stage 1 notifier this will never be called.
  • Timeout can be changed once shutdown has been initiated, but it will only affect the following stages.
  • Notifiers returned from a function (eg. FirstFunc) can be used for selects. They will be notified, but the shutdown manager will not wait for them to finish, so using them for this is not recommended.
  • If a panic occurs inside a shutdown function call in your code, the panic will be recovered and ignored and the shutdown will proceed. A message is printed to log. If you want to handle panics, you must do it in your code.
  • When shutdown is initiated, it cannot be stopped.

When you design with this do take care that this library is for controlled shutdown of your application. If you application crashes no shutdown handlers are run, so panics will still be fatal. You can of course still call the Shutdown() function if you recover a panic, but the library does nothing like this automatically.

why 3 stages?

By limiting the design to "only" three stages enable you to clearly make design choices, and force you to run as many things as possible in parallel. With this you can write simple design docs. Lets look at a webserver example:

  • Preshutdown: Finish accepted requests, refuse new ones.
  • Stage 1: Notify clients, flush data to database, notify upstream servers we are offline.
  • Stage 2: Flush database bulk writers, messages, close databases. (no database writes)
  • Stage 3: Flush/close log/metrics writer. (no log writes)

My intention is that this makes the shutdown process easier to manage, and encourage more concurrency, because you don't create a long daisy-chain of events, and doesn't force you to look through all your code to insert a single event correctly.

Don't think of the 3-stages as something that must do all stages of your shutdown. A single function call can of course (and is intended to) contain several "substages". Shutting down the database can easily be several stages, but you only register a single stage in the shutdown manager. The important part is that nothing else in the same stage can use the database.

examples

There are examples in the examples folder.

license

This code is published under an MIT license. See LICENSE file for more information.

Documentation

Overview

Package shutdown provides management of your shutdown process.

The package will enable you to get notifications for your application and handle the shutdown process.

See more information about the how to use it in the README.md file

Package home: https://github.com/klauspost/shutdown

This package has been updated to a new version. If you are starting a new project, use the new package:

* Package home: https://github.com/klauspost/shutdown2 * Godoc: https://godoc.org/github.com/klauspost/shutdown2

Version 2 mainly contains minor adjustments to the API to make it a bit easier to use.

This package remains here to maintain compatibility.

Index

Examples

Constants

This section is empty.

Variables

View Source
var Logger = log.New(os.Stderr, "[shutdown]: ", log.LstdFlags)

Logger used for output. This can be exchanged with your own.

View Source
var Preshutdown = Stage{0} // Indicates stage when waiting for locks to be released.
View Source
var Stage1 = Stage{1} // Indicates first stage of timeouts.
View Source
var Stage2 = Stage{2} // Indicates second stage of timeouts.
View Source
var Stage3 = Stage{3} // Indicates third stage of timeouts.

Functions

func Exit

func Exit(code int)

Exit performs shutdown operations and exits with the given exit code.

func Lock

func Lock() bool

Lock will signal that you have a function running, that you do not want to be interrupted by a shutdown.

If the function returns false shutdown has already been initiated, and you did not get a lock. You should therefore not call Unlock.

If the function returned true, you must call Unlock() once to release the lock.

You should not hold a lock when you start a shutdown.

Example

Note that the same effect of this example can also be achieved using the WrapHandlerFunc helper.

http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
	// Get a lock while we have the lock, the server will not shut down.
	if Lock() {
		defer Unlock()
	} else {
		// We are currently shutting down, return http.StatusServiceUnavailable
		w.WriteHeader(http.StatusServiceUnavailable)
		return
	}
	// ...
})
http.ListenAndServe(":8080", nil)
Output:

func OnSignal

func OnSignal(exitCode int, sig ...os.Signal)

OnSignal will start the shutdown when any of the given signals arrive

A good shutdown default is

shutdown.OnSignal(0, os.Interrupt, syscall.SIGTERM)

which will do shutdown on Ctrl+C and when the program is terminated.

func SetTimeout

func SetTimeout(d time.Duration)

SetTimeout sets maximum delay to wait for each stage to finish. When the timeout has expired for a stage the next stage will be initiated.

func SetTimeoutN

func SetTimeoutN(s Stage, d time.Duration)

SetTimeoutN set maximum delay to wait for a specific stage to finish. When the timeout expired for a stage the next stage will be initiated. The stage can be obtained by using the exported variables called 'Stage1, etc.

Example

Change timeout for a single stage

// Set timout for all stages
SetTimeout(time.Second)

// But give second stage more time
SetTimeoutN(Stage2, time.Second*10)
Output:

func Shutdown

func Shutdown()

Shutdown will signal all notifiers in three stages. It will first check that all locks have been released - see Lock()

func Started

func Started() bool

Started returns true if shutdown has been started. Note that shutdown can have been started before you check the value.

func Unlock

func Unlock()

Unlock will release a shutdown lock. This may only be called if you have previously called Lock and it has returned true

func Wait

func Wait()

Wait will wait until shutdown has finished. This can be used to keep a main function from exiting until shutdown has been called, either by a goroutine or a signal.

Example

This is an example, that could be your main function.

We wait for jobs to finish in another goroutine, from where we initialize the shutdown.

This is of course not a real-world problem, but there are many cases where you would want to initialize shutdown from other places than your main function, and where you would still like it to be able to do some final cleanup.

x := make([]struct{}, 10)
var wg sync.WaitGroup

wg.Add(len(x))
for i := range x {
	go func(i int) {
		time.Sleep(time.Millisecond * time.Duration(i))
		wg.Done()
	}(i)
}

// ignore this reset, for test purposes only
reset()

// Wait for the jobs above to finish
go func() {
	wg.Wait()
	fmt.Println("jobs done")
	Shutdown()
}()

// Since this is main, we wait for a shutdown to occur before
// exiting.
Wait()
fmt.Println("exiting main")

// Note than the output will always be in this order.
Output:

jobs done
exiting main

func WrapHandler

func WrapHandler(h http.Handler) http.Handler

WrapHandler will return an http Handler That will lock shutdown until all have completed and will return http.StatusServiceUnavailable if shutdown has been initiated.

Example

This example creates a fileserver and wraps the handler, so all request will finish before shutdown is started.

If requests take too long to finish the shutdown will proceed and clients will be disconnected when the server shuts down. To modify the timeout use SetTimeoutN(Preshutdown, duration)

// Set a custom timeout, if the 5 second default doesn't fit your needs.
SetTimeoutN(Preshutdown, time.Second*30)
// Catch OS signals
OnSignal(0, os.Interrupt, syscall.SIGTERM)

// Create a fileserver handler
fh := http.FileServer(http.Dir("/examples"))

// Wrap the handler function
http.Handle("/", WrapHandler(fh))

// Start the server
http.ListenAndServe(":8080", nil)
Output:

func WrapHandlerFunc

func WrapHandlerFunc(h http.HandlerFunc) http.HandlerFunc

WrapHandlerFunc will return an http.HandlerFunc that will lock shutdown until all have completed. The handler will return http.StatusServiceUnavailable if shutdown has been initiated.

Example

This example creates a custom function handler and wraps the handler, so all request will finish before shutdown is started.

If requests take too long to finish (see the shutdown will proceed and clients will be disconnected when the server shuts down. To modify the timeout use SetTimeoutN(Preshutdown, duration)

// Set a custom timeout, if the 5 second default doesn't fit your needs.
SetTimeoutN(Preshutdown, time.Second*30)
// Catch OS signals
OnSignal(0, os.Interrupt, syscall.SIGTERM)

// Example handler function
fn := func(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
}

// Wrap the handler function
http.HandleFunc("/", WrapHandlerFunc(fn))

// Start the server
http.ListenAndServe(":8080", nil)
Output:

Types

type Notifier

type Notifier chan chan struct{}

Notifier is a channel, that will be sent a channel once the application shuts down. When you have performed your shutdown actions close the channel you are given.

Example

Get a notifier and perform our own code when we shutdown

shutdown := First()
select {
case n := <-shutdown:
	// Do shutdown code ...

	// Signal we are done
	close(n)
}
Output:

func First

func First() Notifier

First returns a notifier that will be called in the first stage of shutdowns

func FirstFunc

func FirstFunc(fn ShutdownFn, v interface{}) Notifier

FirstFunc executes a function in the first stage of the shutdown

func PreShutdown

func PreShutdown() Notifier

PreShutdown will return a Notifier that will be fired as soon as the shutdown is signalled, before locks are released. This allows to for instance send signals to upstream servers not to send more requests.

func PreShutdownFunc

func PreShutdownFunc(fn ShutdownFn, v interface{}) Notifier

PreShutdownFunc registers a function that will be called as soon as the shutdown is signalled, before locks are released. This allows to for instance send signals to upstream servers not to send more requests.

func Second

func Second() Notifier

Second returns a notifier that will be called in the second stage of shutdowns

func SecondFunc

func SecondFunc(fn ShutdownFn, v interface{}) Notifier

SecondFunc executes a function in the second stage of the shutdown

func Third

func Third() Notifier

Third returns a notifier that will be called in the third stage of shutdowns

func ThirdFunc

func ThirdFunc(fn ShutdownFn, v interface{}) Notifier

ThirdFunc executes a function in the third stage of the shutdown The returned Notifier is only really useful for cancelling the shutdown function

func (*Notifier) Cancel

func (s *Notifier) Cancel()

Cancel a Notifier. This will remove a notifier from the shutdown queue, and it will not be signalled when shutdown starts. If the shutdown has already started this will not have any effect.

type ShutdownFn

type ShutdownFn func(interface{})
Example

Get a notifier and perform our own function when we shutdown

_ = FirstFunc(func(i interface{}) {
	// This function is called on shutdown
	fmt.Println(i.(string))
}, "Example parameter")

// Will print the parameter when Shutdown() is called
Output:

type Stage

type Stage struct {
	// contains filtered or unexported fields
}

Valid values for this is exported as variables.

Jump to

Keyboard shortcuts

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