Skip to content

Commit

Permalink
Merge pull request #1 from caddyserver/fix/goroutine-leak-healthchecker
Browse files Browse the repository at this point in the history
fix goroutine leak in healthcheckers
  • Loading branch information
mholt authored Jun 7, 2019
2 parents 37da91c + 878ae00 commit 8947ae0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
18 changes: 13 additions & 5 deletions modules/caddyhttp/reverseproxy/healthchecker.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,32 @@ type HealthChecker struct {
upstream Upstream
Ticker *time.Ticker
HTTPClient *http.Client
StopChan chan bool
}

// ScheduleChecks periodically runs health checks against an upstream host.
func (h *HealthChecker) ScheduleChecks(url string) {
// check if a host is healthy on start vs waiting for timer
h.upstream.SetHealthiness(h.IsHealthy(url))
stop := make(chan bool)
h.StopChan = stop

for {
select {
case <-h.Ticker.C:
h.upstream.SetHealthiness(h.IsHealthy(url))
go func() {
for {
select {
case <-h.Ticker.C:
h.upstream.SetHealthiness(h.IsHealthy(url))
case <-stop:
return
}
}
}
}()
}

// Stop stops the healthchecker from makeing further requests.
func (h *HealthChecker) Stop() {
h.Ticker.Stop()
close(h.StopChan)
}

// IsHealthy attempts to check if a upstream host is healthy.
Expand Down
2 changes: 1 addition & 1 deletion modules/caddyhttp/reverseproxy/upstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func NewLoadBalancedReverseProxy(lb *LoadBalanced, ctx caddy2.Context) error {
// TODO :- if path is empty why does this empty the entire Target?
// nu.Target.Path = uc.HealthCheckPath

go nu.healthChecker.ScheduleChecks(nu.Target.String())
nu.healthChecker.ScheduleChecks(nu.Target.String())
lb.HealthCheckers = append(lb.HealthCheckers, nu.healthChecker)

us = append(us, nu)
Expand Down

0 comments on commit 8947ae0

Please sign in to comment.