From 6d010189a56b191538bdc534de7ede7e89f754c4 Mon Sep 17 00:00:00 2001 From: Francis Lavoie Date: Sat, 15 Apr 2023 09:06:41 -0400 Subject: [PATCH] Implement success ratio in health checks --- modules/caddyhttp/reverseproxy/hosts.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/modules/caddyhttp/reverseproxy/hosts.go b/modules/caddyhttp/reverseproxy/hosts.go index 6815ea626..732a71746 100644 --- a/modules/caddyhttp/reverseproxy/hosts.go +++ b/modules/caddyhttp/reverseproxy/hosts.go @@ -84,6 +84,15 @@ func (u *Upstream) Healthy() bool { if healthy && u.healthCheckPolicy != nil { healthy = u.Host.Fails() < u.healthCheckPolicy.MaxFails } + if healthy && u.healthCheckPolicy != nil && + u.healthCheckPolicy.MinSuccessRatio > 0 { + successes := u.Host.Successes() + if successes >= u.healthCheckPolicy.MinSuccesses { + fails := u.Host.Fails() + healthRatio := float64(fails) / float64(successes) + healthy = healthRatio < (1 - float64(u.healthCheckPolicy.MinSuccessRatio)) + } + } if healthy && u.cb != nil { healthy = u.cb.OK() }