summaryrefslogtreecommitdiff
path: root/modules/caddyhttp/reverseproxy/healthchecks.go
diff options
context:
space:
mode:
authorFrancis Lavoie <lavofr@gmail.com>2022-08-31 17:01:30 -0400
committerGitHub <noreply@github.com>2022-08-31 15:01:30 -0600
commitd4d8bbcfc64d1194079cae35697709f6d267d02f (patch)
tree8617401e3560f1eb5986878747180862822d4d4c /modules/caddyhttp/reverseproxy/healthchecks.go
parent68d8ac9802c5e7bf5bf55d1a3c1db634edc93999 (diff)
events: Implement event system (#4912)
Co-authored-by: Matt Holt <mholt@users.noreply.github.com>
Diffstat (limited to 'modules/caddyhttp/reverseproxy/healthchecks.go')
-rw-r--r--modules/caddyhttp/reverseproxy/healthchecks.go18
1 files changed, 13 insertions, 5 deletions
diff --git a/modules/caddyhttp/reverseproxy/healthchecks.go b/modules/caddyhttp/reverseproxy/healthchecks.go
index eb98638..cf22d26 100644
--- a/modules/caddyhttp/reverseproxy/healthchecks.go
+++ b/modules/caddyhttp/reverseproxy/healthchecks.go
@@ -284,6 +284,13 @@ func (h *Handler) doActiveHealthCheck(dialInfo DialInfo, hostAddr string, upstre
}
}
+ markUnhealthy := func() {
+ // dispatch an event that the host newly became unhealthy
+ if upstream.setHealthy(false) {
+ h.events.Emit(h.ctx, "unhealthy", map[string]any{"host": hostAddr})
+ }
+ }
+
// do the request, being careful to tame the response body
resp, err := h.HealthChecks.Active.httpClient.Do(req)
if err != nil {
@@ -291,7 +298,7 @@ func (h *Handler) doActiveHealthCheck(dialInfo DialInfo, hostAddr string, upstre
zap.String("host", hostAddr),
zap.Error(err),
)
- upstream.setHealthy(false)
+ markUnhealthy()
return nil
}
var body io.Reader = resp.Body
@@ -311,7 +318,7 @@ func (h *Handler) doActiveHealthCheck(dialInfo DialInfo, hostAddr string, upstre
zap.Int("status_code", resp.StatusCode),
zap.String("host", hostAddr),
)
- upstream.setHealthy(false)
+ markUnhealthy()
return nil
}
} else if resp.StatusCode < 200 || resp.StatusCode >= 400 {
@@ -319,7 +326,7 @@ func (h *Handler) doActiveHealthCheck(dialInfo DialInfo, hostAddr string, upstre
zap.Int("status_code", resp.StatusCode),
zap.String("host", hostAddr),
)
- upstream.setHealthy(false)
+ markUnhealthy()
return nil
}
@@ -331,14 +338,14 @@ func (h *Handler) doActiveHealthCheck(dialInfo DialInfo, hostAddr string, upstre
zap.String("host", hostAddr),
zap.Error(err),
)
- upstream.setHealthy(false)
+ markUnhealthy()
return nil
}
if !h.HealthChecks.Active.bodyRegexp.Match(bodyBytes) {
h.HealthChecks.Active.logger.Info("response body failed expectations",
zap.String("host", hostAddr),
)
- upstream.setHealthy(false)
+ markUnhealthy()
return nil
}
}
@@ -346,6 +353,7 @@ func (h *Handler) doActiveHealthCheck(dialInfo DialInfo, hostAddr string, upstre
// passed health check parameters, so mark as healthy
if upstream.setHealthy(true) {
h.HealthChecks.Active.logger.Info("host is up", zap.String("host", hostAddr))
+ h.events.Emit(h.ctx, "healthy", map[string]any{"host": hostAddr})
}
return nil