Move HTTP code out of health.go

Put it with the related HTTP code.
This commit is contained in:
Tim Hockin 2014-08-10 23:50:06 -07:00
parent c67c1edfb4
commit 24c516ec1d
2 changed files with 23 additions and 27 deletions

View File

@ -16,11 +16,7 @@ limitations under the License.
package health
import (
"net/http"
"github.com/golang/glog"
)
import ()
type Status int
@ -30,25 +26,3 @@ const (
Unhealthy
Unknown
)
// HTTPGetInterface is an abstract interface for testability. It abstracts the interface of http.Client.Get.
type HTTPGetInterface interface {
Get(url string) (*http.Response, error)
}
// DoHTTPCheck checks if a GET request to the url succeeds.
// If the HTTP response code is successful (i.e. 400 > code >= 200), it returns Healthy.
// If the HTTP response code is unsuccessful, it returns Unhealthy.
// It returns Unknown and err if the HTTP communication itself fails.
func DoHTTPCheck(url string, client HTTPGetInterface) (Status, error) {
res, err := client.Get(url)
if err != nil {
return Unknown, err
}
defer res.Body.Close()
if res.StatusCode >= http.StatusOK && res.StatusCode < http.StatusBadRequest {
return Healthy, nil
}
glog.V(1).Infof("Health check failed for %s, Response: %v", url, *res)
return Unhealthy, nil
}

View File

@ -61,6 +61,12 @@ func (m *MuxHealthChecker) HealthCheck(currentState api.PodState, container api.
return checker.HealthCheck(currentState, container)
}
// HTTPGetInterface is an abstract interface for testability. It abstracts the interface of http.Client.Get.
type HTTPGetInterface interface {
Get(url string) (*http.Response, error)
}
// DoHTTPCheck checks if a GET request to the url succeeds.
// HTTPHealthChecker is an implementation of HealthChecker which checks container health by sending HTTP Get requests.
type HTTPHealthChecker struct {
client HTTPGetInterface
@ -115,6 +121,22 @@ func formatURL(host string, port int, path string) string {
return fmt.Sprintf("http://%s:%d%s", host, port, path)
}
// If the HTTP response code is successful (i.e. 400 > code >= 200), it returns Healthy.
// If the HTTP response code is unsuccessful, it returns Unhealthy.
// It returns Unknown and err if the HTTP communication itself fails.
func DoHTTPCheck(url string, client HTTPGetInterface) (Status, error) {
res, err := client.Get(url)
if err != nil {
return Unknown, err
}
defer res.Body.Close()
if res.StatusCode >= http.StatusOK && res.StatusCode < http.StatusBadRequest {
return Healthy, nil
}
glog.V(1).Infof("Health check failed for %s, Response: %v", url, *res)
return Unhealthy, nil
}
// HealthCheck checks if the container is healthy by trying sending HTTP Get requests to the container.
func (h *HTTPHealthChecker) HealthCheck(currentState api.PodState, container api.Container) (Status, error) {
host, port, path, err := getURLParts(currentState, container)