• Stars
    star
    107
  • Rank 323,587 (Top 7 %)
  • Language
    Go
  • License
    MIT License
  • Created over 7 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Seamless restart / zero-downtime deploy for Go servers

Go Seamless Restart

godoc license

Package seamless implements a seamless restart strategy for daemons monitored by a service supervisor expecting non-forking daemons like daemontools, runit, systemd etc.

The seamless strategy is to fully rely on the service supervisor to restart the daemon, while providing to the daemon the full control of the restart process. To achieve this, seamless duplicates the daemon at startup in order to establish a supervisor -> launcher -> daemon relationship. The launcher is the first generation of the daemon hijacked by seamless to act as a circuit breaker between the supervisor and the supervised process.

This way, when the supervisor sends a TERM signal to stop the daemon, the launcher intercepts the signal and send an USR2 signal to its child (the actual daemon). In the daemon, seamless intercepts the USR2 signals to initiate the first stage of the seamless restart.

During the first stage, the daemon prepare itself to welcome a new version of itself by creating a PID file (see below) and by for instance closing file descriptors. At this point, the daemon is still supposed to accept requests. Once read, seamless make it send a CHLD signal back to the launcher (its parent). Upon reception, the launcher, immediately die, cutting to link between the supervisor and the daemon, making the supervisor attempting a restart of the daemon while current daemon is still running, detached and unsupervised.

Once the supervisor restarted the daemon, the daemon can start serving traffic in place of the old (still running) daemon by rebinding sockets using SO_REUSEPORT for instance (see different strategies in examples/). This is the second stage of the seamless restart. When ready, the new daemon calls seamless.Started which will look for a PID file, and if found, will send a TERM signal to the old daemon using the PID found in this file.

When the old daemon receives this TERM signal, the third and last stage of the seamless restart is engaged. The OnShutdown function is called so the daemon can gracefully shutdown using Go 1.8 http graceful Shutdown method for instance. This stage can last as long as you decide. When done, the old process can exit in order to conclude the seamless restart.

Seamless does not try to implement the actual graceful shutdown or to manage sockets migration. This task is left to the caller. See the examples directory for different implementations.

Usage

Here is an example of seamless restart of an HTTP server using Go 1.8 provided graceful shutdown feature + the SO_REUSEPORT sockopt.

package main

import (
	"context"
	"flag"
	"fmt"
	"log"
	"net/http"
	"os"
	"time"

	reuseport "github.com/kavu/go_reuseport"
	"github.com/rs/seamless"
)

var (
	listen          = flag.String("listen", "localhost:8080", "Listen address")
	pidFile         = flag.String("pid-file", "/tmp/reuseport.pid", "Seemless restart PID file")
	gracefulTimeout = flag.Duration("graceful-timeout", 60*time.Second, "Maximum duration to wait for in-flight requests")
)

func init() {
	flag.Parse()
	seamless.Init(*pidFile)
}

func main() {
	// Use github.com/kavu/go_reuseport waiting for
	// https://github.com/golang/go/issues/9661 to be fixed.
	//
	// The idea of SO_REUSEPORT flag is that two processes can listen on the
	// same host:port. Using the capability, the new daemon can listen while
	// the old daemon is still bound, allowing seemless transition from one
	// process to the other.
	l, err := reuseport.Listen("tcp", *listen)
	if err != nil {
		log.Fatal(err)
	}

	s := &http.Server{
		Addr: *listen,
		Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			if d := r.URL.Query().Get("delay"); d != "" {
				if delay, err := time.ParseDuration(d); err == nil {
					time.Sleep(delay)
				}
			}
			w.WriteHeader(http.StatusOK)
			fmt.Fprintf(w, "Server pid: %d\n", os.Getpid())
		}),
	}

	// Implement the graceful shutdown that will be triggered once the new process
	// successfully rebound the socket.
	seamless.OnShutdown(func() {
		ctx, cancel := context.WithTimeout(context.Background(), *gracefulTimeout)
		defer cancel()
		if err := s.Shutdown(ctx); err != nil {
			log.Print("Graceful shutdown timeout, force closing")
			s.Close()
		}
	})

	go func() {
		// Give the server a second to start
		time.Sleep(time.Second)
		if err == nil {
			// Signal seamless that the daemon is started and the socket is
			// bound successfully. If a pid file is found, seamless will send
			// a signal to the old process to start its graceful shutdown
			// sequence.
			seamless.Started()
		}
	}()
	err = s.Serve(l)
	if err != nil && err != http.ErrServerClosed {
		log.Fatal(err)
	}

	// Once graceful shutdown is initiated, the Serve method is return with a
	// http.ErrServerClosed error. We must not exit until the graceful shutdown
	// is completed. The seamless.Wait method blocks until the OnShutdown callback
	// has returned.
	seamless.Wait()
}

Lets test this using daemontools. We first create the service directory:

mkdir -p service
cat <<EOF > service/run
#!/bin/sh
exec ./reuseport
EOF
chmod 755 service/run
go build -o service/reuseport examples/reuseport

Then in a separate terminal, run supervise on this service:

supervise ./service/

Then in two terminals, run two loops, one with fast quick request and another with artificially slow requests:

# term 1
while true; do curl http://localhost:8080 || break; done

# term 2
while true; do curl 'http://localhost:8080?delay=10s' || break; done

Then in yet another terminal, try to restart the service:

svc -t ./service/

You should see no refused connection on the first terminal and the ongoing slow request should not be interrupted on the other one.

License

All source code is licensed under the MIT License.

More Repositories

1

zerolog

Zero Allocation JSON Logger
Go
10,343
star
2

xid

xid is a globally unique id generator thought for the web
Go
3,874
star
3

curlie

The power of curl, the ease of use of httpie.
Go
2,808
star
4

cors

Go net/http configurable handler to handle CORS requests
Go
2,625
star
5

rest-layer

REST Layer, Go (golang) REST API framework
Go
1,257
star
6

SDSegmentedControl

A drop-in remplacement for UISegmentedControl that mimic iOS 6 AppStore tab controls
Objective-C
1,200
star
7

pushd

Blazing fast multi-protocol mobile and web push notification service
CoffeeScript
1,162
star
8

jplot

iTerm2 expvar/JSON monitoring tool
Go
1,140
star
9

SDAVAssetExportSession

AVAssetExportSession drop-in replacement with customizable audio&video settings
Objective-C
800
star
10

SDURLCache

URLCache subclass with on-disk cache support on iPhone/iPad
Objective-C
798
star
11

SafariTabSwitching

A SIMBL plugin for Safari 5.1 allowing tab switching by index (using Cmd-1, Cmd-2โ€ฆ)
Objective-C
472
star
12

jaggr

JSON Aggregation CLI
Go
460
star
13

SafariOmnibar

Safari plugin to add Chrome like omnibar in Safari
Objective-C
416
star
14

dnstrace

DNS resolution tracing tool
Go
270
star
15

dnscache

DNS lookup cache for Go
Go
267
star
16

node-netmask

Parse and lookup IP network blocks
CoffeeScript
253
star
17

xhandler

XHandler is a bridge between net/context and http.Handler
Go
233
star
18

xlog

xlog is a logger for net/context aware HTTP applications
Go
138
star
19

xmux

xmux is a httprouter fork on top of xhandler (net/context aware)
Go
98
star
20

vast

Golang VAST 3.0 library
Go
83
star
21

xstats

xstats is a generic client for service instrumentation
Go
82
star
22

gls

A graphical ls command for iTerm2 with icons
Swift
78
star
23

SDNetworkActivityIndicator

Handle show/hiding of the iOS network activity indicator
Objective-C
75
star
24

zkfarmer

ZkFarmer is a set of tools to easily manage distributed server farms using Apache ZooKeeper
Python
74
star
25

logbench

Golang logging library benchmarks
Go
69
star
26

dashplay

Easy dashboard screen management
HTML
67
star
27

SDAdvancedWebView

Add some handy features to you UIWebViews
Objective-C
49
star
28

eve-auth-jwt

Eve OAuth 2.0 JWT token validation authentication module
Python
46
star
29

domcheck

A Python library to validate the ownership of a domain using different strategies
Python
45
star
30

iris-ice

Iris keyboard build with custom case
43
star
31

scanman

ScanSnap manager for Raspberry Pi
Python
40
star
32

formjson

Go net/http handler to transparently manage posted JSON
Go
39
star
33

moquette

MQTT service dispatcher
Go
38
star
34

SDReachability

Easy to use and to embed Reachability library
Objective-C
36
star
35

golp

Go panic logger
Go
27
star
36

xaccess

Go http handler access logger
Go
20
star
37

tzsp

TaZmen Sniffer Protocol (TZSP) parser in Go
Go
20
star
38

rest-layer-mongo

REST Layer MongoDB resource storage handler
Go
18
star
39

audience-meter

Lightweight server to mesure audience of a live event
JavaScript
17
star
40

node-dnsstamp

DNS Stamp encoding/decoding library for node
TypeScript
15
star
41

net-server-mail

Extensible Perl implementation of the STMP protocol and its different evolutions (ie: ESMTP, LMTP)
Perl
15
star
42

vmap

Golang VMAP 1.0 library
Go
14
star
43

SDSRTParser

Objective-C SRT subtitle parser
Objective-C
13
star
44

mysql-genocide

Parallel operations on MySQL processlist
Perl
11
star
45

dnsdump

DNS Packet Dump
Go
10
star
46

pinba_http

Pinba HTTP Gateway
Python
8
star
47

gh-readme

Githup pages template for projects README
CSS
7
star
48

rest-layer-es

REST Layer ElasticSearch resource storage handler
Go
6
star
49

mydbd

A mysqli OO interface with PEAR::DB API compatibility
PHP
5
star
50

local-ip

Go
4
star
51

rest-layer-mem

REST Layer memory storage handler
Go
4
star
52

homebrew-tap

rs homebrew packages
Ruby
3
star
53

xlog-nsq

XLog to NSQ Output
Go
3
star
54

rest-layer-hystrix

REST Layer Hystrix storage handler wrapper
Go
3
star
55

rrdpoller

Easily query and perform threshold tests on RRD files data
Perl
3
star
56

rrdcollect-remote

Collect rrdcollect output from several hosts to update local RRD files
Perl
2
star
57

SiriDailymotion

AssistantExtensions plugin to integrate Dailymotion to Siri on Jailbroken iPhone 4s
Objective-C
2
star
58

proxy

A simple HTTP explicit forward proxy http.Handler
Go
2
star
59

gcs-oauth2-boto-env-plugin

Google Storage auth2 plugin with support for passing service key via environment
Python
2
star
60

rs.github.com

1
star
61

obfu

Go
1
star