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 separate timeout for /health requests done by visor #683

Merged
merged 3 commits into from
Mar 4, 2021
Merged
Show file tree
Hide file tree
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
17 changes: 13 additions & 4 deletions pkg/visor/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,32 +168,41 @@ func (v *Visor) collectHealthStats(services map[string]HealthCheckable) map[stri
name string
status int
}
var wg sync.WaitGroup
ctx := context.Background()

ctx, cancel := context.WithTimeout(context.Background(), InnerHealthTimeout)
defer cancel()

responses := make(chan healthResponse, len(services))

var wg sync.WaitGroup
wg.Add(len(services))
for name, service := range services {
go func(name string, service HealthCheckable) {
defer wg.Done()

if service == nil {
responses <- healthResponse{name, http.StatusNotFound}
wg.Done()
return
}

status, err := service.Health(ctx)
if err != nil {
v.log.WithError(err).Warnf("Failed to check service health, service name: %s", name)
status = http.StatusInternalServerError
}

responses <- healthResponse{name, status}
wg.Done()
}(name, service)
}
wg.Wait()

close(responses)

results := make(map[string]int)
for response := range responses {
results[response.name] = response.status
}

return results
}

Expand Down
6 changes: 5 additions & 1 deletion pkg/visor/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ import (
const (
// RPCPrefix is the prefix used with all RPC calls.
RPCPrefix = "app-visor"
// HealthTimeout defines timeout for /health endpoint calls.
// HealthTimeout defines timeout for /health endpoint calls done from hypervisor.
HealthTimeout = 5 * time.Second
// InnerHealthTimeout defines timeout for /health endpoint calls done from visor.
// We keep it is less than the `HealthTimeout`, so that the outer call would
// definitely complete.
InnerHealthTimeout = 3 * time.Second
)

var (
Expand Down