2014-04-03 02:53:15 +08:00
|
|
|
grace [![Build Status](https://secure.travis-ci.org/facebookgo/grace.png)](http://travis-ci.org/facebookgo/grace)
|
|
|
|
=====
|
2012-06-05 12:56:51 +08:00
|
|
|
|
|
|
|
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).
|
|
|
|
|
2013-09-01 10:59:33 +08:00
|
|
|
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
|
2013-03-26 05:46:34 +08:00
|
|
|
activation](http://0pointer.de/blog/projects/socket-activation.html)
|
|
|
|
compatibility to also provide lazy activation of the server.
|
|
|
|
|
2013-03-26 10:21:14 +08:00
|
|
|
|
|
|
|
Usage
|
|
|
|
-----
|
|
|
|
|
2012-06-05 12:56:51 +08:00
|
|
|
Demo HTTP Server with graceful termination and restart:
|
2014-04-03 02:52:43 +08:00
|
|
|
https://github.com/facebookgo/grace/blob/master/gracedemo/demo.go
|
2012-06-05 12:56:51 +08:00
|
|
|
|
2013-03-26 10:21:14 +08:00
|
|
|
1. Install the demo application
|
|
|
|
|
2014-04-03 02:52:43 +08:00
|
|
|
go get github.com/facebookgo/grace/gracedemo
|
2013-03-26 10:21:14 +08:00
|
|
|
|
|
|
|
1. 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.
|
|
|
|
|
|
|
|
1. In a second terminal start a slow HTTP request
|
|
|
|
|
|
|
|
curl 'http://localhost:48567/sleep/?duration=20s'
|
|
|
|
|
|
|
|
1. In a third terminal trigger a graceful server restart (using the pid from your output):
|
|
|
|
|
|
|
|
kill -USR2 14642
|
|
|
|
|
|
|
|
1. 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
|
|
|
|
-------------
|
|
|
|
|
2013-05-19 01:31:19 +08:00
|
|
|
`http.Server` graceful termination and restart:
|
2014-04-03 02:52:43 +08:00
|
|
|
http://godoc.org/github.com/facebookgo/grace/gracehttp
|
2012-06-05 12:56:51 +08:00
|
|
|
|
2013-05-19 01:31:19 +08:00
|
|
|
`net.Listener` graceful termination and restart:
|
2014-04-03 02:52:43 +08:00
|
|
|
http://godoc.org/github.com/facebookgo/grace
|