Skip to content

Commit

Permalink
Move HTTP code out of health.go
Browse files Browse the repository at this point in the history
Put it with the related HTTP code.
  • Loading branch information
thockin committed Aug 11, 2014
1 parent c67c1ed commit 24c516e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 27 deletions.
28 changes: 1 addition & 27 deletions pkg/health/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ limitations under the License.

package health

import (
"net/http"

"github.com/golang/glog"
)
import ()

type Status int

Expand All @@ -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
}
22 changes: 22 additions & 0 deletions pkg/health/health_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 24c516e

Please sign in to comment.