Add feature for graceful shutdown of HTTP server

If the watchdog is sent SIGTERM from an external process then it
should stop accepting new connections and attempt to finish the
work in progress. This change makes use of the new ability in Go
1.9 and onwards to cancel a HTTP server gracefully.

The write_timeout duration is used as a grace period to allow all
in-flight requests to complete. The pattern is taken directly from
the offical example in the Golang documentation. [1]

Further tuning and testing may be needed for Windows containers which
have a different set of signals for closing work. This change aims
to cover the majority use-case for Linux containers.

The HTTP health-check is also invalidated by creating an and
expression with the existing lock file.

Tested with Kubernetes by deploying a custom watchdog and the
fprocess of `env`. Log message was observed when scaling down and
connections stopped being accepted on terminating replica.

Also corrects some typos from previous PR.

[1] https://golang.org/pkg/net/http/#Server.Shutdown

Signed-off-by: Alex Ellis (VMware) <alexellis2@gmail.com>
This commit is contained in:
Alex Ellis (VMware)
2018-03-20 09:45:31 +00:00
committed by Alex Ellis
parent 6921e4fc0f
commit de2c74fcdb
2 changed files with 60 additions and 19 deletions

View File

@ -11,6 +11,7 @@ import (
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"time"
@ -361,11 +362,13 @@ func TestHandler_StatusOKForGETAndNoBody(t *testing.T) {
}
}
func TestHealthHandler_SatusOK_LockFilePresent(t *testing.T) {
func TestHealthHandler_StatusOK_LockFilePresent(t *testing.T) {
rr := httptest.NewRecorder()
if lockFilePresent() == false {
if err := createLockFile(); err != nil {
present := lockFilePresent()
if present == false {
if _, err := createLockFile(); err != nil {
t.Fatal(err)
}
}
@ -402,7 +405,7 @@ func TestHealthHandler_StatusInternalServerError_LockFileNotPresent(t *testing.T
required := http.StatusInternalServerError
if status := rr.Code; status != required {
t.Errorf("handler retruned wrong status code: got %v, but wanted %v", status, required)
t.Errorf("handler returned wrong status code - got: %v, want: %v", status, required)
}
}
@ -426,3 +429,10 @@ func TestHealthHandler_SatusMethoNotAllowed_ForWriteableVerbs(t *testing.T) {
}
}
}
func removeLockFile() error {
path := filepath.Join(os.TempDir(), ".lock")
log.Printf("Removing lock-file : %s\n", path)
removeErr := os.Remove(path)
return removeErr
}