feat: Add timeout handling to health check execution with goroutine-based result channel

- Add resultChan channel to receive health check results asynchronously
- Execute checker.CheckHealth in goroutine to enable timeout detection
- Add select statement to handle both successful completion and timeout cases
- Return StatusUnhealthy with "Health check timed out" message on context timeout
- Set ResponseTime to 10 seconds when timeout occurs
This commit is contained in:
nutcas3
2026-04-25 06:34:12 +03:00
parent 442e051e8d
commit 47c57c9b48
+20 -1
View File
@@ -94,7 +94,26 @@ func (hm *HealthMonitor) CheckHealth(ctx context.Context) SystemHealth {
for name, checker := range checks {
checkCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
result := checker.CheckHealth(checkCtx)
// Execute health check with timeout
resultChan := make(chan HealthCheck, 1)
go func() {
resultChan <- checker.CheckHealth(checkCtx)
}()
var result HealthCheck
select {
case result = <-resultChan:
// Check completed successfully
case <-checkCtx.Done():
// Timeout occurred
result = HealthCheck{
Name: name,
Status: StatusUnhealthy,
Message: "Health check timed out",
ResponseTime: 10 * time.Second,
}
}
cancel()
result.LastChecked = time.Now()