Go to file
Ben Tyler 4afe952a37 Add option for 'StartupHook' (#35)
* Add ServeWithOptions

This adds support for options to be added to 'Serve' and the app struct.
Options are implemented following the 'functional options' pattern
(https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis and
https://commandcenter.blogspot.co.uk/2014/01/self-referential-functions-and-design.html).

Future options can be added by creating an exported func that returns a closure
modifying the app struct, like the following:

    func HaltAndCatchFire(literallyCatchFire bool) option {
        return func(a *app) {
            a.haltAndCatchFire = literallyCatchFire
        }
    }

then in user code:

    gracehttp.ServeWithOptions(
        []*http.Server{ &myServer },
        gracehttp.HaltAndCatchFire(true),
    )

* Add 'StartupHook' option

This option attaches a callback to the application. This callback is triggered
directly before the new process is started during a graceful restart. This
allows the old process to release its hold on any resources that the new
process will need.

For example:

    gracehttp.ServeWithOptions(
        []*http.Server{ &myServer },
        gracehttp.StartupHook(func () error {
            // release port that new process will need to start up successfully
            return nil
        }
    )

* Rename 'StartupHook' to 'PreStartProcess'

This better indicates the timing of the callback by using terms already present
in the codebase. As part of the rename, the related constants in the tests were
fixed to follow the naming convention.
2017-02-18 14:52:39 -08:00
gracedemo internal sync, license update, 1.2 on travis etc 2014-04-02 14:59:39 -07:00
gracehttp Add option for 'StartupHook' (#35) 2017-02-18 14:52:39 -08:00
gracenet fixed a word spelling mistake: pacakge -> package 2015-08-04 14:57:06 +08:00
.travis.yml use go-1.7 on travis 2016-09-26 16:11:18 -07:00
license update to Additional Grant of Patent Rights Version 2 2015-06-12 18:29:25 +00:00
patents update to Additional Grant of Patent Rights Version 2 2015-06-12 18:29:25 +00:00
readme.md fix readme godoc link 2015-06-15 20:35:34 +00:00

grace Build Status

Package grace provides a library that makes it easy to build socket based servers that can be gracefully terminated & restarted (that is, without dropping any connections).

It provides a convenient API for HTTP servers including support for TLS, especially if you need to listen on multiple ports (for example a secondary internal only admin server). Additionally it is implemented using the same API as systemd providing socket activation compatibility to also provide lazy activation of the server.

Usage

Demo HTTP Server with graceful termination and restart: https://github.com/facebookgo/grace/blob/master/gracedemo/demo.go

  1. Install the demo application

     go get github.com/facebookgo/grace/gracedemo
    
  2. Start it in the first terminal

     gracedemo
    

    This will output something like:

     2013/03/25 19:07:33 Serving [::]:48567, [::]:48568, [::]:48569 with pid 14642.
    
  3. In a second terminal start a slow HTTP request

     curl 'http://localhost:48567/sleep/?duration=20s'
    
  4. In a third terminal trigger a graceful server restart (using the pid from your output):

     kill -USR2 14642
    
  5. Trigger another shorter request that finishes before the earlier request:

     curl 'http://localhost:48567/sleep/?duration=0s'
    

If done quickly enough, this shows the second quick request will be served by the new process (as indicated by the PID) while the slow first request will be served by the first server. It shows how the active connection was gracefully served before the server was shutdown. It is also showing that at one point both the new as well as the old server was running at the same time.

Documentation

http.Server graceful termination and restart: https://godoc.org/github.com/facebookgo/grace/gracehttp

net.Listener graceful termination and restart: https://godoc.org/github.com/facebookgo/grace/gracenet