Code Monkey home page Code Monkey logo

go-graceful-shutdown's Introduction

go-graceful-shutdown

This project demonstrates a pattern for graceful server shutdown in Go. It was inspired by this article: https://rudderstack.com/blog/implementing-graceful-shutdown-in-go

The sample server maintains:

  • a connection to postgres database
  • an HTTP webserver
  • a few long-running services using PG connection

Upon startup:

  • hooks on signals: SIGINT and SIGTERM to initiate a shutdown flow
  • creates a global context that will be cancelled in case of shutdown
  • the server connects to postgres database "test"
  • starts HTTP webserver
  • spawns a few goroutines that represent long-running tasks
  • main thread remains blocked until one of the signals caught

Once one of the signals caught:

  • it cancels the global context
  • all child components will finish executing because of ctx.Done()
  • the main thread unblocks receiving the error (if any)
  • database is closing as the last step of shutdown

Edge cases:

  • if a termination signal received upon startup sequence, it cancels the global context, and then newly creating components should start and stop pretty much immediately.
  • if a termination signal received during shutdown sequence, it will be ignored.
  • because SIGKILL cannot be handled, killing the server process will result in inconsistent state.

How to build and run

  • have a postgres instance up and running on your machine
  • create an empty DB "test"
  • go run .
  • try terminating the app by Ctrl+C (or by sending SIGINT or SIGTERM from terminal)

go-graceful-shutdown's People

Contributors

andrei-toptal 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.