mirror of
https://github.com/openfaas/faas.git
synced 2025-06-25 00:03:24 +00:00
Merge master into breakout_swarm
Signed-off-by: Alex Ellis <alexellis2@gmail.com>
This commit is contained in:
37
gateway/vendor/github.com/docker/distribution/health/api/api.go
generated
vendored
Normal file
37
gateway/vendor/github.com/docker/distribution/health/api/api.go
generated
vendored
Normal file
@ -0,0 +1,37 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/docker/distribution/health"
|
||||
)
|
||||
|
||||
var (
|
||||
updater = health.NewStatusUpdater()
|
||||
)
|
||||
|
||||
// DownHandler registers a manual_http_status that always returns an Error
|
||||
func DownHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == "POST" {
|
||||
updater.Update(errors.New("Manual Check"))
|
||||
} else {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}
|
||||
}
|
||||
|
||||
// UpHandler registers a manual_http_status that always returns nil
|
||||
func UpHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == "POST" {
|
||||
updater.Update(nil)
|
||||
} else {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}
|
||||
}
|
||||
|
||||
// init sets up the two endpoints to bring the service up and down
|
||||
func init() {
|
||||
health.Register("manual_http_status", updater)
|
||||
http.HandleFunc("/debug/health/down", DownHandler)
|
||||
http.HandleFunc("/debug/health/up", UpHandler)
|
||||
}
|
86
gateway/vendor/github.com/docker/distribution/health/api/api_test.go
generated
vendored
Normal file
86
gateway/vendor/github.com/docker/distribution/health/api/api_test.go
generated
vendored
Normal file
@ -0,0 +1,86 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/docker/distribution/health"
|
||||
)
|
||||
|
||||
// TestGETDownHandlerDoesNotChangeStatus ensures that calling the endpoint
|
||||
// /debug/health/down with METHOD GET returns a 404
|
||||
func TestGETDownHandlerDoesNotChangeStatus(t *testing.T) {
|
||||
recorder := httptest.NewRecorder()
|
||||
|
||||
req, err := http.NewRequest("GET", "https://fakeurl.com/debug/health/down", nil)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to create request.")
|
||||
}
|
||||
|
||||
DownHandler(recorder, req)
|
||||
|
||||
if recorder.Code != 404 {
|
||||
t.Errorf("Did not get a 404.")
|
||||
}
|
||||
}
|
||||
|
||||
// TestGETUpHandlerDoesNotChangeStatus ensures that calling the endpoint
|
||||
// /debug/health/down with METHOD GET returns a 404
|
||||
func TestGETUpHandlerDoesNotChangeStatus(t *testing.T) {
|
||||
recorder := httptest.NewRecorder()
|
||||
|
||||
req, err := http.NewRequest("GET", "https://fakeurl.com/debug/health/up", nil)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to create request.")
|
||||
}
|
||||
|
||||
DownHandler(recorder, req)
|
||||
|
||||
if recorder.Code != 404 {
|
||||
t.Errorf("Did not get a 404.")
|
||||
}
|
||||
}
|
||||
|
||||
// TestPOSTDownHandlerChangeStatus ensures the endpoint /debug/health/down changes
|
||||
// the status code of the response to 503
|
||||
// This test is order dependent, and should come before TestPOSTUpHandlerChangeStatus
|
||||
func TestPOSTDownHandlerChangeStatus(t *testing.T) {
|
||||
recorder := httptest.NewRecorder()
|
||||
|
||||
req, err := http.NewRequest("POST", "https://fakeurl.com/debug/health/down", nil)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to create request.")
|
||||
}
|
||||
|
||||
DownHandler(recorder, req)
|
||||
|
||||
if recorder.Code != 200 {
|
||||
t.Errorf("Did not get a 200.")
|
||||
}
|
||||
|
||||
if len(health.CheckStatus()) != 1 {
|
||||
t.Errorf("DownHandler didn't add an error check.")
|
||||
}
|
||||
}
|
||||
|
||||
// TestPOSTUpHandlerChangeStatus ensures the endpoint /debug/health/up changes
|
||||
// the status code of the response to 200
|
||||
func TestPOSTUpHandlerChangeStatus(t *testing.T) {
|
||||
recorder := httptest.NewRecorder()
|
||||
|
||||
req, err := http.NewRequest("POST", "https://fakeurl.com/debug/health/up", nil)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to create request.")
|
||||
}
|
||||
|
||||
UpHandler(recorder, req)
|
||||
|
||||
if recorder.Code != 200 {
|
||||
t.Errorf("Did not get a 200.")
|
||||
}
|
||||
|
||||
if len(health.CheckStatus()) != 0 {
|
||||
t.Errorf("UpHandler didn't remove the error check.")
|
||||
}
|
||||
}
|
62
gateway/vendor/github.com/docker/distribution/health/checks/checks.go
generated
vendored
Normal file
62
gateway/vendor/github.com/docker/distribution/health/checks/checks.go
generated
vendored
Normal file
@ -0,0 +1,62 @@
|
||||
package checks
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/docker/distribution/health"
|
||||
)
|
||||
|
||||
// FileChecker checks the existence of a file and returns an error
|
||||
// if the file exists.
|
||||
func FileChecker(f string) health.Checker {
|
||||
return health.CheckFunc(func() error {
|
||||
if _, err := os.Stat(f); err == nil {
|
||||
return errors.New("file exists")
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// HTTPChecker does a HEAD request and verifies that the HTTP status code
|
||||
// returned matches statusCode.
|
||||
func HTTPChecker(r string, statusCode int, timeout time.Duration, headers http.Header) health.Checker {
|
||||
return health.CheckFunc(func() error {
|
||||
client := http.Client{
|
||||
Timeout: timeout,
|
||||
}
|
||||
req, err := http.NewRequest("HEAD", r, nil)
|
||||
if err != nil {
|
||||
return errors.New("error creating request: " + r)
|
||||
}
|
||||
for headerName, headerValues := range headers {
|
||||
for _, headerValue := range headerValues {
|
||||
req.Header.Add(headerName, headerValue)
|
||||
}
|
||||
}
|
||||
response, err := client.Do(req)
|
||||
if err != nil {
|
||||
return errors.New("error while checking: " + r)
|
||||
}
|
||||
if response.StatusCode != statusCode {
|
||||
return errors.New("downstream service returned unexpected status: " + strconv.Itoa(response.StatusCode))
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// TCPChecker attempts to open a TCP connection.
|
||||
func TCPChecker(addr string, timeout time.Duration) health.Checker {
|
||||
return health.CheckFunc(func() error {
|
||||
conn, err := net.DialTimeout("tcp", addr, timeout)
|
||||
if err != nil {
|
||||
return errors.New("connection to " + addr + " failed")
|
||||
}
|
||||
conn.Close()
|
||||
return nil
|
||||
})
|
||||
}
|
25
gateway/vendor/github.com/docker/distribution/health/checks/checks_test.go
generated
vendored
Normal file
25
gateway/vendor/github.com/docker/distribution/health/checks/checks_test.go
generated
vendored
Normal file
@ -0,0 +1,25 @@
|
||||
package checks
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFileChecker(t *testing.T) {
|
||||
if err := FileChecker("/tmp").Check(); err == nil {
|
||||
t.Errorf("/tmp was expected as exists")
|
||||
}
|
||||
|
||||
if err := FileChecker("NoSuchFileFromMoon").Check(); err != nil {
|
||||
t.Errorf("NoSuchFileFromMoon was expected as not exists, error:%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHTTPChecker(t *testing.T) {
|
||||
if err := HTTPChecker("https://www.google.cybertron", 200, 0, nil).Check(); err == nil {
|
||||
t.Errorf("Google on Cybertron was expected as not exists")
|
||||
}
|
||||
|
||||
if err := HTTPChecker("https://www.google.pt", 200, 0, nil).Check(); err != nil {
|
||||
t.Errorf("Google at Portugal was expected as exists, error:%v", err)
|
||||
}
|
||||
}
|
130
gateway/vendor/github.com/docker/distribution/health/doc.go
generated
vendored
Normal file
130
gateway/vendor/github.com/docker/distribution/health/doc.go
generated
vendored
Normal file
@ -0,0 +1,130 @@
|
||||
// Package health provides a generic health checking framework.
|
||||
// The health package works expvar style. By importing the package the debug
|
||||
// server is getting a "/debug/health" endpoint that returns the current
|
||||
// status of the application.
|
||||
// If there are no errors, "/debug/health" will return an HTTP 200 status,
|
||||
// together with an empty JSON reply "{}". If there are any checks
|
||||
// with errors, the JSON reply will include all the failed checks, and the
|
||||
// response will be have an HTTP 503 status.
|
||||
//
|
||||
// A Check can either be run synchronously, or asynchronously. We recommend
|
||||
// that most checks are registered as an asynchronous check, so a call to the
|
||||
// "/debug/health" endpoint always returns immediately. This pattern is
|
||||
// particularly useful for checks that verify upstream connectivity or
|
||||
// database status, since they might take a long time to return/timeout.
|
||||
//
|
||||
// Installing
|
||||
//
|
||||
// To install health, just import it in your application:
|
||||
//
|
||||
// import "github.com/docker/distribution/health"
|
||||
//
|
||||
// You can also (optionally) import "health/api" that will add two convenience
|
||||
// endpoints: "/debug/health/down" and "/debug/health/up". These endpoints add
|
||||
// "manual" checks that allow the service to quickly be brought in/out of
|
||||
// rotation.
|
||||
//
|
||||
// import _ "github.com/docker/distribution/registry/health/api"
|
||||
//
|
||||
// # curl localhost:5001/debug/health
|
||||
// {}
|
||||
// # curl -X POST localhost:5001/debug/health/down
|
||||
// # curl localhost:5001/debug/health
|
||||
// {"manual_http_status":"Manual Check"}
|
||||
//
|
||||
// After importing these packages to your main application, you can start
|
||||
// registering checks.
|
||||
//
|
||||
// Registering Checks
|
||||
//
|
||||
// The recommended way of registering checks is using a periodic Check.
|
||||
// PeriodicChecks run on a certain schedule and asynchronously update the
|
||||
// status of the check. This allows CheckStatus to return without blocking
|
||||
// on an expensive check.
|
||||
//
|
||||
// A trivial example of a check that runs every 5 seconds and shuts down our
|
||||
// server if the current minute is even, could be added as follows:
|
||||
//
|
||||
// func currentMinuteEvenCheck() error {
|
||||
// m := time.Now().Minute()
|
||||
// if m%2 == 0 {
|
||||
// return errors.New("Current minute is even!")
|
||||
// }
|
||||
// return nil
|
||||
// }
|
||||
//
|
||||
// health.RegisterPeriodicFunc("minute_even", currentMinuteEvenCheck, time.Second*5)
|
||||
//
|
||||
// Alternatively, you can also make use of "RegisterPeriodicThresholdFunc" to
|
||||
// implement the exact same check, but add a threshold of failures after which
|
||||
// the check will be unhealthy. This is particularly useful for flaky Checks,
|
||||
// ensuring some stability of the service when handling them.
|
||||
//
|
||||
// health.RegisterPeriodicThresholdFunc("minute_even", currentMinuteEvenCheck, time.Second*5, 4)
|
||||
//
|
||||
// The lowest-level way to interact with the health package is calling
|
||||
// "Register" directly. Register allows you to pass in an arbitrary string and
|
||||
// something that implements "Checker" and runs your check. If your method
|
||||
// returns an error with nil, it is considered a healthy check, otherwise it
|
||||
// will make the health check endpoint "/debug/health" start returning a 503
|
||||
// and list the specific check that failed.
|
||||
//
|
||||
// Assuming you wish to register a method called "currentMinuteEvenCheck()
|
||||
// error" you could do that by doing:
|
||||
//
|
||||
// health.Register("even_minute", health.CheckFunc(currentMinuteEvenCheck))
|
||||
//
|
||||
// CheckFunc is a convenience type that implements Checker.
|
||||
//
|
||||
// Another way of registering a check could be by using an anonymous function
|
||||
// and the convenience method RegisterFunc. An example that makes the status
|
||||
// endpoint always return an error:
|
||||
//
|
||||
// health.RegisterFunc("my_check", func() error {
|
||||
// return Errors.new("This is an error!")
|
||||
// }))
|
||||
//
|
||||
// Examples
|
||||
//
|
||||
// You could also use the health checker mechanism to ensure your application
|
||||
// only comes up if certain conditions are met, or to allow the developer to
|
||||
// take the service out of rotation immediately. An example that checks
|
||||
// database connectivity and immediately takes the server out of rotation on
|
||||
// err:
|
||||
//
|
||||
// updater = health.NewStatusUpdater()
|
||||
// health.RegisterFunc("database_check", func() error {
|
||||
// return updater.Check()
|
||||
// }))
|
||||
//
|
||||
// conn, err := Connect(...) // database call here
|
||||
// if err != nil {
|
||||
// updater.Update(errors.New("Error connecting to the database: " + err.Error()))
|
||||
// }
|
||||
//
|
||||
// You can also use the predefined Checkers that come included with the health
|
||||
// package. First, import the checks:
|
||||
//
|
||||
// import "github.com/docker/distribution/health/checks
|
||||
//
|
||||
// After that you can make use of any of the provided checks. An example of
|
||||
// using a `FileChecker` to take the application out of rotation if a certain
|
||||
// file exists can be done as follows:
|
||||
//
|
||||
// health.Register("fileChecker", health.PeriodicChecker(checks.FileChecker("/tmp/disable"), time.Second*5))
|
||||
//
|
||||
// After registering the check, it is trivial to take an application out of
|
||||
// rotation from the console:
|
||||
//
|
||||
// # curl localhost:5001/debug/health
|
||||
// {}
|
||||
// # touch /tmp/disable
|
||||
// # curl localhost:5001/debug/health
|
||||
// {"fileChecker":"file exists"}
|
||||
//
|
||||
// You could also test the connectivity to a downstream service by using a
|
||||
// "HTTPChecker", but ensure that you only mark the test unhealthy if there
|
||||
// are a minimum of two failures in a row:
|
||||
//
|
||||
// health.Register("httpChecker", health.PeriodicThresholdChecker(checks.HTTPChecker("https://www.google.pt"), time.Second*5, 2))
|
||||
package health
|
306
gateway/vendor/github.com/docker/distribution/health/health.go
generated
vendored
Normal file
306
gateway/vendor/github.com/docker/distribution/health/health.go
generated
vendored
Normal file
@ -0,0 +1,306 @@
|
||||
package health
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/docker/distribution/context"
|
||||
"github.com/docker/distribution/registry/api/errcode"
|
||||
)
|
||||
|
||||
// A Registry is a collection of checks. Most applications will use the global
|
||||
// registry defined in DefaultRegistry. However, unit tests may need to create
|
||||
// separate registries to isolate themselves from other tests.
|
||||
type Registry struct {
|
||||
mu sync.RWMutex
|
||||
registeredChecks map[string]Checker
|
||||
}
|
||||
|
||||
// NewRegistry creates a new registry. This isn't necessary for normal use of
|
||||
// the package, but may be useful for unit tests so individual tests have their
|
||||
// own set of checks.
|
||||
func NewRegistry() *Registry {
|
||||
return &Registry{
|
||||
registeredChecks: make(map[string]Checker),
|
||||
}
|
||||
}
|
||||
|
||||
// DefaultRegistry is the default registry where checks are registered. It is
|
||||
// the registry used by the HTTP handler.
|
||||
var DefaultRegistry *Registry
|
||||
|
||||
// Checker is the interface for a Health Checker
|
||||
type Checker interface {
|
||||
// Check returns nil if the service is okay.
|
||||
Check() error
|
||||
}
|
||||
|
||||
// CheckFunc is a convenience type to create functions that implement
|
||||
// the Checker interface
|
||||
type CheckFunc func() error
|
||||
|
||||
// Check Implements the Checker interface to allow for any func() error method
|
||||
// to be passed as a Checker
|
||||
func (cf CheckFunc) Check() error {
|
||||
return cf()
|
||||
}
|
||||
|
||||
// Updater implements a health check that is explicitly set.
|
||||
type Updater interface {
|
||||
Checker
|
||||
|
||||
// Update updates the current status of the health check.
|
||||
Update(status error)
|
||||
}
|
||||
|
||||
// updater implements Checker and Updater, providing an asynchronous Update
|
||||
// method.
|
||||
// This allows us to have a Checker that returns the Check() call immediately
|
||||
// not blocking on a potentially expensive check.
|
||||
type updater struct {
|
||||
mu sync.Mutex
|
||||
status error
|
||||
}
|
||||
|
||||
// Check implements the Checker interface
|
||||
func (u *updater) Check() error {
|
||||
u.mu.Lock()
|
||||
defer u.mu.Unlock()
|
||||
|
||||
return u.status
|
||||
}
|
||||
|
||||
// Update implements the Updater interface, allowing asynchronous access to
|
||||
// the status of a Checker.
|
||||
func (u *updater) Update(status error) {
|
||||
u.mu.Lock()
|
||||
defer u.mu.Unlock()
|
||||
|
||||
u.status = status
|
||||
}
|
||||
|
||||
// NewStatusUpdater returns a new updater
|
||||
func NewStatusUpdater() Updater {
|
||||
return &updater{}
|
||||
}
|
||||
|
||||
// thresholdUpdater implements Checker and Updater, providing an asynchronous Update
|
||||
// method.
|
||||
// This allows us to have a Checker that returns the Check() call immediately
|
||||
// not blocking on a potentially expensive check.
|
||||
type thresholdUpdater struct {
|
||||
mu sync.Mutex
|
||||
status error
|
||||
threshold int
|
||||
count int
|
||||
}
|
||||
|
||||
// Check implements the Checker interface
|
||||
func (tu *thresholdUpdater) Check() error {
|
||||
tu.mu.Lock()
|
||||
defer tu.mu.Unlock()
|
||||
|
||||
if tu.count >= tu.threshold {
|
||||
return tu.status
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// thresholdUpdater implements the Updater interface, allowing asynchronous
|
||||
// access to the status of a Checker.
|
||||
func (tu *thresholdUpdater) Update(status error) {
|
||||
tu.mu.Lock()
|
||||
defer tu.mu.Unlock()
|
||||
|
||||
if status == nil {
|
||||
tu.count = 0
|
||||
} else if tu.count < tu.threshold {
|
||||
tu.count++
|
||||
}
|
||||
|
||||
tu.status = status
|
||||
}
|
||||
|
||||
// NewThresholdStatusUpdater returns a new thresholdUpdater
|
||||
func NewThresholdStatusUpdater(t int) Updater {
|
||||
return &thresholdUpdater{threshold: t}
|
||||
}
|
||||
|
||||
// PeriodicChecker wraps an updater to provide a periodic checker
|
||||
func PeriodicChecker(check Checker, period time.Duration) Checker {
|
||||
u := NewStatusUpdater()
|
||||
go func() {
|
||||
t := time.NewTicker(period)
|
||||
for {
|
||||
<-t.C
|
||||
u.Update(check.Check())
|
||||
}
|
||||
}()
|
||||
|
||||
return u
|
||||
}
|
||||
|
||||
// PeriodicThresholdChecker wraps an updater to provide a periodic checker that
|
||||
// uses a threshold before it changes status
|
||||
func PeriodicThresholdChecker(check Checker, period time.Duration, threshold int) Checker {
|
||||
tu := NewThresholdStatusUpdater(threshold)
|
||||
go func() {
|
||||
t := time.NewTicker(period)
|
||||
for {
|
||||
<-t.C
|
||||
tu.Update(check.Check())
|
||||
}
|
||||
}()
|
||||
|
||||
return tu
|
||||
}
|
||||
|
||||
// CheckStatus returns a map with all the current health check errors
|
||||
func (registry *Registry) CheckStatus() map[string]string { // TODO(stevvooe) this needs a proper type
|
||||
registry.mu.RLock()
|
||||
defer registry.mu.RUnlock()
|
||||
statusKeys := make(map[string]string)
|
||||
for k, v := range registry.registeredChecks {
|
||||
err := v.Check()
|
||||
if err != nil {
|
||||
statusKeys[k] = err.Error()
|
||||
}
|
||||
}
|
||||
|
||||
return statusKeys
|
||||
}
|
||||
|
||||
// CheckStatus returns a map with all the current health check errors from the
|
||||
// default registry.
|
||||
func CheckStatus() map[string]string {
|
||||
return DefaultRegistry.CheckStatus()
|
||||
}
|
||||
|
||||
// Register associates the checker with the provided name.
|
||||
func (registry *Registry) Register(name string, check Checker) {
|
||||
if registry == nil {
|
||||
registry = DefaultRegistry
|
||||
}
|
||||
registry.mu.Lock()
|
||||
defer registry.mu.Unlock()
|
||||
_, ok := registry.registeredChecks[name]
|
||||
if ok {
|
||||
panic("Check already exists: " + name)
|
||||
}
|
||||
registry.registeredChecks[name] = check
|
||||
}
|
||||
|
||||
// Register associates the checker with the provided name in the default
|
||||
// registry.
|
||||
func Register(name string, check Checker) {
|
||||
DefaultRegistry.Register(name, check)
|
||||
}
|
||||
|
||||
// RegisterFunc allows the convenience of registering a checker directly from
|
||||
// an arbitrary func() error.
|
||||
func (registry *Registry) RegisterFunc(name string, check func() error) {
|
||||
registry.Register(name, CheckFunc(check))
|
||||
}
|
||||
|
||||
// RegisterFunc allows the convenience of registering a checker in the default
|
||||
// registry directly from an arbitrary func() error.
|
||||
func RegisterFunc(name string, check func() error) {
|
||||
DefaultRegistry.RegisterFunc(name, check)
|
||||
}
|
||||
|
||||
// RegisterPeriodicFunc allows the convenience of registering a PeriodicChecker
|
||||
// from an arbitrary func() error.
|
||||
func (registry *Registry) RegisterPeriodicFunc(name string, period time.Duration, check CheckFunc) {
|
||||
registry.Register(name, PeriodicChecker(CheckFunc(check), period))
|
||||
}
|
||||
|
||||
// RegisterPeriodicFunc allows the convenience of registering a PeriodicChecker
|
||||
// in the default registry from an arbitrary func() error.
|
||||
func RegisterPeriodicFunc(name string, period time.Duration, check CheckFunc) {
|
||||
DefaultRegistry.RegisterPeriodicFunc(name, period, check)
|
||||
}
|
||||
|
||||
// RegisterPeriodicThresholdFunc allows the convenience of registering a
|
||||
// PeriodicChecker from an arbitrary func() error.
|
||||
func (registry *Registry) RegisterPeriodicThresholdFunc(name string, period time.Duration, threshold int, check CheckFunc) {
|
||||
registry.Register(name, PeriodicThresholdChecker(CheckFunc(check), period, threshold))
|
||||
}
|
||||
|
||||
// RegisterPeriodicThresholdFunc allows the convenience of registering a
|
||||
// PeriodicChecker in the default registry from an arbitrary func() error.
|
||||
func RegisterPeriodicThresholdFunc(name string, period time.Duration, threshold int, check CheckFunc) {
|
||||
DefaultRegistry.RegisterPeriodicThresholdFunc(name, period, threshold, check)
|
||||
}
|
||||
|
||||
// StatusHandler returns a JSON blob with all the currently registered Health Checks
|
||||
// and their corresponding status.
|
||||
// Returns 503 if any Error status exists, 200 otherwise
|
||||
func StatusHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == "GET" {
|
||||
checks := CheckStatus()
|
||||
status := http.StatusOK
|
||||
|
||||
// If there is an error, return 503
|
||||
if len(checks) != 0 {
|
||||
status = http.StatusServiceUnavailable
|
||||
}
|
||||
|
||||
statusResponse(w, r, status, checks)
|
||||
} else {
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}
|
||||
|
||||
// Handler returns a handler that will return 503 response code if the health
|
||||
// checks have failed. If everything is okay with the health checks, the
|
||||
// handler will pass through to the provided handler. Use this handler to
|
||||
// disable a web application when the health checks fail.
|
||||
func Handler(handler http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
checks := CheckStatus()
|
||||
if len(checks) != 0 {
|
||||
errcode.ServeJSON(w, errcode.ErrorCodeUnavailable.
|
||||
WithDetail("health check failed: please see /debug/health"))
|
||||
return
|
||||
}
|
||||
|
||||
handler.ServeHTTP(w, r) // pass through
|
||||
})
|
||||
}
|
||||
|
||||
// statusResponse completes the request with a response describing the health
|
||||
// of the service.
|
||||
func statusResponse(w http.ResponseWriter, r *http.Request, status int, checks map[string]string) {
|
||||
p, err := json.Marshal(checks)
|
||||
if err != nil {
|
||||
context.GetLogger(context.Background()).Errorf("error serializing health status: %v", err)
|
||||
p, err = json.Marshal(struct {
|
||||
ServerError string `json:"server_error"`
|
||||
}{
|
||||
ServerError: "Could not parse error message",
|
||||
})
|
||||
status = http.StatusInternalServerError
|
||||
|
||||
if err != nil {
|
||||
context.GetLogger(context.Background()).Errorf("error serializing health status failure message: %v", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.Header().Set("Content-Length", fmt.Sprint(len(p)))
|
||||
w.WriteHeader(status)
|
||||
if _, err := w.Write(p); err != nil {
|
||||
context.GetLogger(context.Background()).Errorf("error writing health status response body: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Registers global /debug/health api endpoint, creates default registry
|
||||
func init() {
|
||||
DefaultRegistry = NewRegistry()
|
||||
http.HandleFunc("/debug/health", StatusHandler)
|
||||
}
|
107
gateway/vendor/github.com/docker/distribution/health/health_test.go
generated
vendored
Normal file
107
gateway/vendor/github.com/docker/distribution/health/health_test.go
generated
vendored
Normal file
@ -0,0 +1,107 @@
|
||||
package health
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestReturns200IfThereAreNoChecks ensures that the result code of the health
|
||||
// endpoint is 200 if there are not currently registered checks.
|
||||
func TestReturns200IfThereAreNoChecks(t *testing.T) {
|
||||
recorder := httptest.NewRecorder()
|
||||
|
||||
req, err := http.NewRequest("GET", "https://fakeurl.com/debug/health", nil)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to create request.")
|
||||
}
|
||||
|
||||
StatusHandler(recorder, req)
|
||||
|
||||
if recorder.Code != 200 {
|
||||
t.Errorf("Did not get a 200.")
|
||||
}
|
||||
}
|
||||
|
||||
// TestReturns500IfThereAreErrorChecks ensures that the result code of the
|
||||
// health endpoint is 500 if there are health checks with errors
|
||||
func TestReturns503IfThereAreErrorChecks(t *testing.T) {
|
||||
recorder := httptest.NewRecorder()
|
||||
|
||||
req, err := http.NewRequest("GET", "https://fakeurl.com/debug/health", nil)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to create request.")
|
||||
}
|
||||
|
||||
// Create a manual error
|
||||
Register("some_check", CheckFunc(func() error {
|
||||
return errors.New("This Check did not succeed")
|
||||
}))
|
||||
|
||||
StatusHandler(recorder, req)
|
||||
|
||||
if recorder.Code != 503 {
|
||||
t.Errorf("Did not get a 503.")
|
||||
}
|
||||
}
|
||||
|
||||
// TestHealthHandler ensures that our handler implementation correct protects
|
||||
// the web application when things aren't so healthy.
|
||||
func TestHealthHandler(t *testing.T) {
|
||||
// clear out existing checks.
|
||||
DefaultRegistry = NewRegistry()
|
||||
|
||||
// protect an http server
|
||||
handler := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}))
|
||||
|
||||
// wrap it in our health handler
|
||||
handler = Handler(handler)
|
||||
|
||||
// use this swap check status
|
||||
updater := NewStatusUpdater()
|
||||
Register("test_check", updater)
|
||||
|
||||
// now, create a test server
|
||||
server := httptest.NewServer(handler)
|
||||
|
||||
checkUp := func(t *testing.T, message string) {
|
||||
resp, err := http.Get(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("error getting success status: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusNoContent {
|
||||
t.Fatalf("unexpected response code from server when %s: %d != %d", message, resp.StatusCode, http.StatusNoContent)
|
||||
}
|
||||
// NOTE(stevvooe): we really don't care about the body -- the format is
|
||||
// not standardized or supported, yet.
|
||||
}
|
||||
|
||||
checkDown := func(t *testing.T, message string) {
|
||||
resp, err := http.Get(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("error getting down status: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusServiceUnavailable {
|
||||
t.Fatalf("unexpected response code from server when %s: %d != %d", message, resp.StatusCode, http.StatusServiceUnavailable)
|
||||
}
|
||||
}
|
||||
|
||||
// server should be up
|
||||
checkUp(t, "initial health check")
|
||||
|
||||
// now, we fail the health check
|
||||
updater.Update(fmt.Errorf("the server is now out of commission"))
|
||||
checkDown(t, "server should be down") // should be down
|
||||
|
||||
// bring server back up
|
||||
updater.Update(nil)
|
||||
checkUp(t, "when server is back up") // now we should be back up.
|
||||
}
|
Reference in New Issue
Block a user