Check docker daemon is alive in ping endpoint

This commit is contained in:
Marcos Lilljedahl
2017-07-31 12:00:49 -03:00
parent 3f4c96286e
commit fa3b56ecfe

View File

@@ -1,9 +1,12 @@
package handlers package handlers
import ( import (
"context"
"log" "log"
"net/http" "net/http"
"time"
"github.com/docker/docker/client"
"github.com/play-with-docker/play-with-docker/config" "github.com/play-with-docker/play-with-docker/config"
"github.com/shirou/gopsutil/load" "github.com/shirou/gopsutil/load"
) )
@@ -11,6 +14,21 @@ import (
func Ping(rw http.ResponseWriter, req *http.Request) { func Ping(rw http.ResponseWriter, req *http.Request) {
// Get system load average of the last 5 minutes and compare it against a threashold. // Get system load average of the last 5 minutes and compare it against a threashold.
c, err := client.NewEnvClient()
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
return
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Microsecond)
defer cancel()
if _, err := c.Info(ctx); err != nil && err == context.DeadlineExceeded {
log.Printf("Docker info took to long to respond\n")
rw.WriteHeader(http.StatusGatewayTimeout)
}
a, err := load.Avg() a, err := load.Avg()
if err != nil { if err != nil {
log.Println("Cannot get system load average!", err) log.Println("Cannot get system load average!", err)