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

fix: add timeout for health checks #1388

Merged
merged 4 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,26 @@ public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context
{
try
{
using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
cts.CancelAfter(TimeSpan.FromSeconds(40));

var sw = System.Diagnostics.Stopwatch.StartNew();
using var redis = await ConnectionMultiplexer.ConnectAsync(_settings.Redis.ConnectionString);
var db = redis.GetDatabase();
await db.PingAsync();
sw.Stop();
arealmaas marked this conversation as resolved.
Show resolved Hide resolved

if (sw.Elapsed > TimeSpan.FromSeconds(5))
{
return HealthCheckResult.Degraded($"Redis connection is slow ({sw.Elapsed.TotalSeconds:N1}s).");
}

return HealthCheckResult.Healthy("Redis connection is healthy.");
}
catch (OperationCanceledException)
{
return HealthCheckResult.Unhealthy("Redis health check timed out after 40s.");
}
arealmaas marked this conversation as resolved.
Show resolved Hide resolved
catch (RedisConnectionException ex)
{
return HealthCheckResult.Unhealthy("Unable to connect to Redis.", exception: ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,29 @@ public async Task<HealthCheckResult> CheckHealthAsync(
{
try
{
var response = await client.GetAsync(url, cancellationToken);
if (!response.IsSuccessStatusCode)
using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
cts.CancelAfter(TimeSpan.FromSeconds(40));

var sw = System.Diagnostics.Stopwatch.StartNew();
var response = await client.GetAsync(url, cts.Token);
sw.Stop();

if (sw.Elapsed > TimeSpan.FromSeconds(5))
{
_logger.LogWarning("Health check response was slow for endpoint: {Url}. Elapsed time: {Elapsed:N1}s", url, sw.Elapsed.TotalSeconds);
unhealthyEndpoints.Add($"{url} (Degraded - Response time: {sw.Elapsed.TotalSeconds:N1}s)");
}
else if (!response.IsSuccessStatusCode)
{
arealmaas marked this conversation as resolved.
Show resolved Hide resolved
_logger.LogWarning("Health check failed for endpoint: {Url}. Status Code: {StatusCode}", url, response.StatusCode);
unhealthyEndpoints.Add($"{url} (Status Code: {response.StatusCode})");
}
}
catch (OperationCanceledException)
{
_logger.LogWarning("Health check timed out for endpoint: {Url}", url);
unhealthyEndpoints.Add($"{url} (Timeout after 40s)");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception occurred while checking endpoint: {Url}", url);
Expand Down
Loading