From 47c57c9b48317492fe5da2335265b9dc6371b389 Mon Sep 17 00:00:00 2001 From: nutcas3 Date: Sat, 25 Apr 2026 06:34:12 +0300 Subject: [PATCH] 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 --- apps/api-server/internal/monitoring/health.go | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/apps/api-server/internal/monitoring/health.go b/apps/api-server/internal/monitoring/health.go index ea344c0..121a7b9 100644 --- a/apps/api-server/internal/monitoring/health.go +++ b/apps/api-server/internal/monitoring/health.go @@ -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()