namespace Dpz.Core.Infrastructure.RateLimiting;
/// <summary>
/// 基于行为的限流结果
/// 支持对被限流的请求进行延迟处理
/// </summary>
public class RateLimitResult
{
/// <summary>
/// 是否允许请求
/// </summary>
public bool IsAllowed { get; init; }
/// <summary>
/// 是否被限流(需要延迟后返回429)
/// </summary>
public bool IsBlocked { get; init; }
/// <summary>
/// 延迟时间(毫秒)- 仅在被限流时使用
/// </summary>
public int DelayMs { get; init; }
/// <summary>
/// 结果消息
/// </summary>
public string Message { get; init; } = string.Empty;
/// <summary>
/// 创建允许通过的结果
/// </summary>
public static RateLimitResult Allow() => new() { IsAllowed = true };
/// <summary>
/// 创建被限流的结果(带延迟)
/// </summary>
public static RateLimitResult Block(string message, int delayMs = 0) =>
new()
{
IsAllowed = false,
IsBlocked = true,
Message = message,
DelayMs = delayMs
};
}