mirror of
https://github.com/andwati/telecom-platform.git
synced 2026-10-31 00:52:16 +00:00
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:
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user