Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a maximum timeout for healtcheck call #73

Merged
merged 1 commit into from
Jan 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 56 additions & 42 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,54 +257,68 @@ func getHealthCheckURL(endpoint, healthCheckPath string, healthCheckPort int) (s

// healthCheck - background routine which checks if a backend is up or down.
func (b *Backend) healthCheck() {
req, err := http.NewRequest(http.MethodGet, b.healthCheckURL, nil)
for {
err := b.doHealthCheck()
if err != nil {
console.Fatalln(err)
}

time.Sleep(b.healthCheckDuration)
}
}

func (b *Backend) doHealthCheck() error {
// Set up a maximum timeout time for the healtcheck operation
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()

req, err := http.NewRequestWithContext(ctx, http.MethodGet, b.healthCheckURL, nil)
if err != nil {
console.Fatalln(err)
return err
}

for {
reqTime := time.Now().UTC()
resp, err := b.httpClient.Do(req)
respTime := time.Now().UTC()
if err == nil {
// Drain the connection.
io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
}
if err != nil || (err == nil && resp.StatusCode != http.StatusOK) {
if globalLoggingEnabled && (!b.Online() || b.Stats.UpSince.IsZero()) {
logMsg(logMessage{Endpoint: b.endpoint, Status: "down", Error: err})
}
// observed an error, take the backend down.
b.setOffline()
if b.Stats.DowntimeStart.IsZero() {
b.Stats.DowntimeStart = time.Now().UTC()
}
} else {
var downtimeEnd time.Time
if !b.Stats.DowntimeStart.IsZero() {
now := time.Now().UTC()
b.updateDowntime(now.Sub(b.Stats.DowntimeStart))
downtimeEnd = now
}
if globalLoggingEnabled && !b.Online() && !b.Stats.UpSince.IsZero() {
logMsg(logMessage{
Endpoint: b.endpoint,
Status: "up",
DowntimeDuration: downtimeEnd.Sub(b.Stats.DowntimeStart),
})
}
b.Stats.UpSince = time.Now().UTC()
b.Stats.DowntimeStart = time.Time{}
b.setOnline()
reqTime := time.Now().UTC()
resp, err := b.httpClient.Do(req)
respTime := time.Now().UTC()
if err == nil {
// Drain the connection.
io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
}
if err != nil || (err == nil && resp.StatusCode != http.StatusOK) {
if globalLoggingEnabled && (!b.Online() || b.Stats.UpSince.IsZero()) {
logMsg(logMessage{Endpoint: b.endpoint, Status: "down", Error: err})
}
if globalTrace != "application" {
if resp != nil {
traceHealthCheckReq(req, resp, reqTime, respTime, b)
}
// observed an error, take the backend down.
b.setOffline()
if b.Stats.DowntimeStart.IsZero() {
b.Stats.DowntimeStart = time.Now().UTC()
}
time.Sleep(b.healthCheckDuration)
} else {
var downtimeEnd time.Time
if !b.Stats.DowntimeStart.IsZero() {
now := time.Now().UTC()
b.updateDowntime(now.Sub(b.Stats.DowntimeStart))
downtimeEnd = now
}
if globalLoggingEnabled && !b.Online() && !b.Stats.UpSince.IsZero() {
logMsg(logMessage{
Endpoint: b.endpoint,
Status: "up",
DowntimeDuration: downtimeEnd.Sub(b.Stats.DowntimeStart),
})
}
b.Stats.UpSince = time.Now().UTC()
b.Stats.DowntimeStart = time.Time{}
b.setOnline()
}
if globalTrace != "application" {
if resp != nil {
traceHealthCheckReq(req, resp, reqTime, respTime, b)
}
}

return nil
}

func (b *Backend) updateDowntime(downtime time.Duration) {
Expand Down