Skip to content

Commit

Permalink
PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
enocom committed Oct 26, 2022
1 parent e11d226 commit d5d4279
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
27 changes: 14 additions & 13 deletions internal/healthcheck/healthcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,16 @@ func (c *Check) HandleReadiness(w http.ResponseWriter, req *http.Request) {
q := req.URL.Query()
if v := q.Get("min-ready"); v != "" {
n, err := strconv.Atoi(v)
// If the query value parsed correctly, set it to minReady. Otherwise,
// ignore it.
if err == nil {
minReady = n
if err != nil {
c.logger.Errorf("[Health Check] min-ready %q was not a valid integer", v)
w.WriteHeader(http.StatusServiceUnavailable)
return
}
minReady = n
}

n, err := c.proxy.CheckConnections(ctx)
if minReady > n {
c.logger.Errorf("[Health Check] min-ready was greater than registered instances")
w.WriteHeader(http.StatusBadRequest)
return
}
if err != nil && !ready(err, minReady, n) {
if !ready(err, minReady, n) {
c.logger.Errorf("[Health Check] Readiness failed: %v", err)
w.WriteHeader(http.StatusServiceUnavailable)
w.Write([]byte(err.Error()))
Expand All @@ -118,13 +114,18 @@ func (c *Check) HandleReadiness(w http.ResponseWriter, req *http.Request) {
}

func ready(err error, minReady, total int) bool {
mErr, ok := err.(proxy.MultiErr)
if !ok {
return false
// If err is nil, then the proxy is ready.
if err == nil {
return true
}
// When minReady is not configured, any error means the proxy is not ready.
if minReady == 0 {
return false
}
mErr, ok := err.(proxy.MultiErr)
if !ok {
return false
}
notReady := len(mErr)
areReady := total - notReady
return areReady >= minReady
Expand Down
16 changes: 8 additions & 8 deletions internal/healthcheck/healthcheck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,11 @@ func TestReadinessWithMinReady(t *testing.T) {
minReady string
wantStatus int
}{
{
desc: "when min ready is not configured",
minReady: "0",
wantStatus: http.StatusServiceUnavailable,
},
{
desc: "when only one instance must be ready",
minReady: "1",
Expand All @@ -274,20 +279,15 @@ func TestReadinessWithMinReady(t *testing.T) {
minReady: "2",
wantStatus: http.StatusServiceUnavailable,
},
{
desc: "when min ready is not configured",
minReady: "0",
wantStatus: http.StatusServiceUnavailable,
},
{
desc: "when min ready is bogus",
minReady: "bogus",
wantStatus: http.StatusServiceUnavailable,
},
{
desc: "when min ready is greater than registered instances",
minReady: "100",
wantStatus: http.StatusBadRequest,
desc: "when min ready is not set",
minReady: "",
wantStatus: http.StatusServiceUnavailable,
},
}
p := newProxyWithParams(t, 0,
Expand Down

0 comments on commit d5d4279

Please sign in to comment.