@marcel91200 / caddy Mirror · read-only / commits / 878ae000
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 14, 05:15 UTC

fix goroutine leak in healthcheckers

dev committed Jun 7, 2019 at 14:04 UTC 878ae0002ad777826ad3f8d764628a69dd543601
2 files changed +14 -6
modules/caddyhttp/reverseproxy/healthchecker.go
+13 -5
@@ -15,24 +15,32 @@ type HealthChecker struct {
15 upstream Upstream
16 Ticker *time.Ticker
17 HTTPClient *http.Client
18 + StopChan chan bool
19 }
20
21 // ScheduleChecks periodically runs health checks against an upstream host.
22 func (h *HealthChecker) ScheduleChecks(url string) {
23 // check if a host is healthy on start vs waiting for timer
24 h.upstream.SetHealthiness(h.IsHealthy(url))
25 + stop := make(chan bool)
26 + h.StopChan = stop
27
25 - for {
26 - select {
27 - case <-h.Ticker.C:
28 - h.upstream.SetHealthiness(h.IsHealthy(url))
28 + go func() {
29 + for {
30 + select {
31 + case <-h.Ticker.C:
32 + h.upstream.SetHealthiness(h.IsHealthy(url))
33 + case <-stop:
34 + return
35 + }
36 }
30 - }
37 + }()
38 }
39
40 // Stop stops the healthchecker from makeing further requests.
41 func (h *HealthChecker) Stop() {
42 h.Ticker.Stop()
43 + close(h.StopChan)
44 }
45
46 // IsHealthy attempts to check if a upstream host is healthy.
modules/caddyhttp/reverseproxy/upstream.go
+1 -1
@@ -140,7 +140,7 @@ func NewLoadBalancedReverseProxy(lb *LoadBalanced, ctx caddy2.Context) error {
140 // TODO :- if path is empty why does this empty the entire Target?
141 // nu.Target.Path = uc.HealthCheckPath
142
143 - go nu.healthChecker.ScheduleChecks(nu.Target.String())
143 + nu.healthChecker.ScheduleChecks(nu.Target.String())
144 lb.HealthCheckers = append(lb.HealthCheckers, nu.healthChecker)
145
146 us = append(us, nu)