53 lines
1.6 KiB
C#
53 lines
1.6 KiB
C#
using ChatBot.Services.Interfaces;
|
|
|
|
namespace ChatBot.Services.ErrorHandlers
|
|
{
|
|
/// <summary>
|
|
/// Error handler for rate limit errors (HTTP 429)
|
|
/// </summary>
|
|
public class RateLimitErrorHandler : IErrorHandler
|
|
{
|
|
private readonly ILogger<RateLimitErrorHandler> _logger;
|
|
|
|
public RateLimitErrorHandler(ILogger<RateLimitErrorHandler> logger)
|
|
{
|
|
_logger = logger;
|
|
}
|
|
|
|
public bool CanHandle(Exception exception)
|
|
{
|
|
return exception.Message.Contains("429")
|
|
|| exception.Message.Contains("Too Many Requests")
|
|
|| exception.Message.Contains("rate limit", StringComparison.OrdinalIgnoreCase);
|
|
}
|
|
|
|
public async Task<ErrorHandlingResult> HandleAsync(
|
|
Exception exception,
|
|
int attempt,
|
|
string currentModel,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
_logger.LogWarning(
|
|
exception,
|
|
"Rate limit exceeded on attempt {Attempt} for model {Model}",
|
|
attempt,
|
|
currentModel
|
|
);
|
|
|
|
// Apply exponential backoff for rate limiting
|
|
var delay = TimeSpan.FromSeconds(Math.Pow(2, attempt - 1));
|
|
var jitter = TimeSpan.FromMilliseconds(Random.Shared.Next(0, 2000));
|
|
|
|
_logger.LogInformation(
|
|
"Rate limit hit, waiting {Delay} before retry",
|
|
delay.Add(jitter)
|
|
);
|
|
|
|
await Task.Delay(delay.Add(jitter), cancellationToken);
|
|
|
|
return ErrorHandlingResult.Retry();
|
|
}
|
|
}
|
|
}
|