Skip to content
count connections to permit daemons to stop safely
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore
.travis.yml
LICENSE
README.md
httpdshutdown.go
httpdshutdown_test.go

README.md

License BSD Go Report Card GoDoc Build Status

httpdshutdown

Count connections to permit daemons to stop safely, and run hooks at shutdown time.

NOTE

As of early 2017, the Go standard library includes some of this functionality in the standard net/http library. You might want to check that first.

EXAMPLE

package main

import (
    "log"
	"net"
	"net/http"
	"github.com/bradclawsie/httpdshutdown"
	"os"
	"os/signal"
	"time"
)

func sampleShutdownHook1() error {
	log.Println("shutdown hook 1 called")
	return nil
}

func sampleShutdownHook2() error {
	log.Println("shutdown hook 2 called")
	return nil
}

func main() {
	log.Printf("launching with pid:%d\n", os.Getpid())
	watcher, watcher_err := httpdshutdown.NewWatcher(2000, sampleShutdownHook1, sampleShutdownHook2)
	if watcher == nil || watcher_err != nil {
		panic("could not construct watcher")
	}

	// Launch the signal handler and exit logic in a goroutine since the http daemon
	// issued later will run in the foreground.
	go func() {
		sigs := make(chan os.Signal, 1)
		exitcode := make(chan int, 1)
		signal.Notify(sigs)
		go watcher.SigHandle(sigs, exitcode)
		code := <-exitcode
		log.Printf("exit with code:%d", code)
		os.Exit(code)
	}()

	srv := &http.Server{
		Addr: ":8080",
		ReadTimeout:  3 * time.Second,
		WriteTimeout: 3 * time.Second,
		ConnState: func(conn net.Conn, newState http.ConnState) {
			log.Printf("(1) NEW CONN STATE:%v\n", newState)
			watcher.RecordConnState(newState)
			return
		},
	}

	log.Fatal(srv.ListenAndServe())
}

You can’t perform that action at this time.