Git Product home page Git Product logo

go-graceful-shutdown's Introduction

Graceful shutdown decorator

Go Reference Pipeline Lint

A wrapper for your Go HTTP server so that it will finish responding to in-flight requests on interrupt signals before shutting down.

func main() {
  var (
    ctx        = context.Background()
    httpServer = &http.Server{Addr: ":8080", Handler: http.HandlerFunc(acceptancetests.SlowHandler)}
    server     = gracefulshutdown.NewServer(httpServer)
  )

  if err := server.ListenAndServe(ctx); err != nil {
    // this will typically happen if our responses aren't written before the ctx deadline, not much can be done
    log.Fatalf("uh oh, didnt shutdown gracefully, some responses may have been lost %v", err)
  }

  // hopefully, you'll always see this instead
  log.Println("shutdown gracefully! all responses were sent")
}

The problem

  • You're running a HTTP server, and deploying it many times per day
  • Sometimes, you might be deploying a new version of the code while it is trying to handle a request, and if you're not handling this gracefully you'll either:
    • Not get a response
    • Or the reverse-proxy in front of your service will complain about your service and return a 502

The solution

Graceful shutdown!

  • Listen to interrupt signals
  • Rather than killing the program straight away, instead call http.Server.Shutdown which will let requests, connections e.t.c drain before killing the server
  • This should mean in most cases, the server will finish the currently running requests before stopping

There are a few examples of this out there, I thought I'd roll my own, so I could understand it better, and structure it in a non-confusing way, hopefully.

Almost everything boils down to a decorator pattern in the end. You provide my library a *http.Server and it'll return you back a *gracefulshutdown.Server. Just call ListenAndServe, and it'll gracefully shutdown on an os signal.

Example usage and testing

See acceptancetests/withgracefulshutdown/main.go for an example

There are two binaries in this project with accompanying acceptance tests to verify the functionality that live inside /acceptancetests.

Both tests build the binaries, run them, fire a HTTP GET and then send an interrupt signal to tell the server to stop.

The two binaries allow us to test both scenarios

  1. A "slow" HTTP server with no graceful shutdown. For this we assert that we do get an error, because the server should shutdown immediately and any in-flight requests will fail.
  2. Another slow HTTP server with graceful shutdown. Same test again, but this time we assert we don't get an error as we expect to get a response before the server is terminated.

go-graceful-shutdown's People

Contributors

quii avatar

Stargazers

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

Watchers

 avatar  avatar

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.