-
Notifications
You must be signed in to change notification settings - Fork 863
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Active and passive health checks (#459)
From time to time, destinations can get unhealthy and start failing requests processing due to various reasons, thus to prevent request failures and maintain a good quality of service YARP must monitor destinations health status and stop sending traffic to the ones became unhealthy until they have recovered. This PR implements active and passive health check mechanisms where the former periodically probes destinations with dedicated HTTP requests and the latter watches for client request proxying results. Fixes #228
- Loading branch information
Showing
93 changed files
with
3,519 additions
and
290 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace Microsoft.ReverseProxy.Sample.Controllers | ||
{ | ||
/// <summary> | ||
/// Controller for active health check probes. | ||
/// </summary> | ||
[ApiController] | ||
public class HealthController : ControllerBase | ||
{ | ||
private static volatile int _count; | ||
/// <summary> | ||
/// Returns 200 if server is healthy. | ||
/// </summary> | ||
[HttpGet] | ||
[Route("/api/health")] | ||
public IActionResult CheckHealth() | ||
{ | ||
_count++; | ||
// Simulate temporary health degradation. | ||
return _count % 10 < 4 ? Ok() : StatusCode(500); | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/ReverseProxy/Abstractions/ClusterDiscovery/Contract/ActiveHealthCheckMonitorOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
|
||
namespace Microsoft.ReverseProxy.Abstractions | ||
{ | ||
/// <summary> | ||
/// Defines options for the active health check monitor. | ||
/// </summary> | ||
public class ActiveHealthCheckMonitorOptions | ||
{ | ||
/// <summary> | ||
/// Default probing interval. | ||
/// </summary> | ||
public TimeSpan DefaultInterval { get; set; } = TimeSpan.FromSeconds(15); | ||
|
||
/// <summary> | ||
/// Default probes timeout. | ||
/// </summary> | ||
public TimeSpan DefaultTimeout { get; set; } = TimeSpan.FromSeconds(10); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
src/ReverseProxy/Abstractions/ClusterDiscovery/Contract/ActiveHealthCheckOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
|
||
namespace Microsoft.ReverseProxy.Abstractions | ||
{ | ||
/// <summary> | ||
/// Active health check options. | ||
/// </summary> | ||
public sealed class ActiveHealthCheckOptions | ||
{ | ||
/// <summary> | ||
/// Whether active health checks are enabled. | ||
/// </summary> | ||
public bool Enabled { get; set; } | ||
|
||
/// <summary> | ||
/// Health probe interval. | ||
/// </summary> | ||
public TimeSpan? Interval { get; set; } | ||
|
||
/// <summary> | ||
/// Health probe timeout, after which a destination is considered unhealthy. | ||
/// </summary> | ||
public TimeSpan? Timeout { get; set; } | ||
|
||
/// <summary> | ||
/// Active health check policy. | ||
/// </summary> | ||
public string Policy { get; set; } | ||
|
||
/// <summary> | ||
/// HTTP health check endpoint path. | ||
/// </summary> | ||
public string Path { get; set; } | ||
|
||
internal ActiveHealthCheckOptions DeepClone() | ||
{ | ||
return new ActiveHealthCheckOptions | ||
{ | ||
Enabled = Enabled, | ||
Interval = Interval, | ||
Timeout = Timeout, | ||
Policy = Policy, | ||
Path = Path, | ||
}; | ||
} | ||
|
||
internal static bool Equals(ActiveHealthCheckOptions options1, ActiveHealthCheckOptions options2) | ||
{ | ||
if (options1 == null && options2 == null) | ||
{ | ||
return true; | ||
} | ||
|
||
if (options1 == null || options2 == null) | ||
{ | ||
return false; | ||
} | ||
|
||
return options1.Enabled == options2.Enabled | ||
&& options1.Interval == options2.Interval | ||
&& options1.Timeout == options2.Timeout | ||
&& string.Equals(options1.Policy, options2.Policy, StringComparison.OrdinalIgnoreCase) | ||
&& string.Equals(options1.Path, options2.Path, StringComparison.OrdinalIgnoreCase); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
...rseProxy/Abstractions/ClusterDiscovery/Contract/ConsecutiveFailuresHealthPolicyOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
namespace Microsoft.ReverseProxy.Abstractions | ||
{ | ||
/// <summary> | ||
/// Defines options for the consecutive failures active health check policy. | ||
/// </summary> | ||
public class ConsecutiveFailuresHealthPolicyOptions | ||
{ | ||
/// <summary> | ||
/// Name of the consecutive failure threshold metadata parameter. | ||
/// It's the number of consecutive failure that needs to happen in order to mark a destination as unhealthy. | ||
/// </summary> | ||
public static readonly string ThresholdMetadataName = "ConsecutiveFailuresHealthPolicy.Threshold"; | ||
|
||
/// <summary> | ||
/// Default consecutive failures threshold that is applied if it's not set on a cluster's metadata. | ||
/// </summary> | ||
public long DefaultThreshold { get; set; } = 2; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/ReverseProxy/Abstractions/ClusterDiscovery/Contract/HealthCheckConstants.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
namespace Microsoft.ReverseProxy.Abstractions | ||
{ | ||
public static class HealthCheckConstants | ||
{ | ||
public static class PassivePolicy | ||
{ | ||
public static readonly string TransportFailureRate = nameof(TransportFailureRate); | ||
} | ||
|
||
public static class ActivePolicy | ||
{ | ||
public static readonly string ConsecutiveFailures = nameof(ConsecutiveFailures); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.