You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
2.6 KiB

using System.Text.Json.Serialization;
namespace X1.Domain.ThirdPartyDeviceHttpClient.Models
{
/// <summary>
/// 熔断器配置选项
/// </summary>
/// <remarks>
/// 定义熔断器的行为参数,包括失败阈值、恢复时间等配置
/// </remarks>
public class CircuitBreakerOptions
{
/// <summary>
/// 是否启用熔断器
/// </summary>
[JsonPropertyName("enabled")]
public bool Enabled { get; set; } = true;
/// <summary>
/// 失败阈值 - 连续失败多少次后触发熔断
/// </summary>
[JsonPropertyName("failureThreshold")]
public int FailureThreshold { get; set; } = 5;
/// <summary>
/// 熔断持续时间(秒)- 熔断后多长时间尝试恢复
/// </summary>
[JsonPropertyName("durationOfBreak")]
public int DurationOfBreak { get; set; } = 30;
/// <summary>
/// 采样持续时间(秒)- 统计失败次数的时间窗口
/// </summary>
[JsonPropertyName("samplingDuration")]
public int SamplingDuration { get; set; } = 60;
/// <summary>
/// 最小吞吐量 - 在采样期间内最少需要多少请求才进行熔断判断
/// </summary>
[JsonPropertyName("minimumThroughput")]
public int MinimumThroughput { get; set; } = 10;
/// <summary>
/// 失败率阈值 - 失败率超过多少百分比时触发熔断(0.0-1.0)
/// </summary>
[JsonPropertyName("failureRateThreshold")]
public double FailureRateThreshold { get; set; } = 0.5;
/// <summary>
/// 是否启用半开状态 - 熔断器在恢复期间是否允许部分请求通过
/// </summary>
[JsonPropertyName("enableHalfOpen")]
public bool EnableHalfOpen { get; set; } = true;
/// <summary>
/// 半开状态下的请求数量 - 允许通过的请求数量
/// </summary>
[JsonPropertyName("handledEventsAllowedBeforeBreaking")]
public int HandledEventsAllowedBeforeBreaking { get; set; } = 2;
/// <summary>
/// 验证配置是否有效
/// </summary>
/// <returns>验证结果</returns>
public bool IsValid()
{
return FailureThreshold > 0
&& DurationOfBreak > 0
&& SamplingDuration > 0
&& MinimumThroughput > 0
&& FailureRateThreshold > 0.0
&& FailureRateThreshold <= 1.0
&& HandledEventsAllowedBeforeBreaking > 0;
}
}
}