mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-23 00:32:50 +08:00
Fix: errors when using rate limiting (#811)
* Fix: errors when using rate limiting Add: QuotaExceededError class for requesting too much Add: QuotaExceededError error code Add: Add an error when limit is reached Reflact: Extract GetResponseMessage method for getting default or configured response message for requ * Fix: modify check_we_have_considered_all_errors_in_these_tests for adding a new OcelotErrorCode
This commit is contained in:
parent
799abf55c4
commit
e76a51ffc9
@ -37,6 +37,7 @@
|
||||
CouldNotFindPlaceholderError,
|
||||
CouldNotFindAggregatorError,
|
||||
CannotAddPlaceholderError,
|
||||
CannotRemovePlaceholderError
|
||||
CannotRemovePlaceholderError,
|
||||
QuotaExceededError
|
||||
}
|
||||
}
|
||||
|
@ -70,6 +70,9 @@ namespace Ocelot.RateLimit.Middleware
|
||||
// break execution
|
||||
await ReturnQuotaExceededResponse(context.HttpContext, options, retrystring);
|
||||
|
||||
// Set Error
|
||||
context.Errors.Add(new QuotaExceededError(this.GetResponseMessage(options)));
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
@ -117,7 +120,7 @@ namespace Ocelot.RateLimit.Middleware
|
||||
|
||||
public virtual Task ReturnQuotaExceededResponse(HttpContext httpContext, RateLimitOptions option, string retryAfter)
|
||||
{
|
||||
var message = string.IsNullOrEmpty(option.QuotaExceededMessage) ? $"API calls quota exceeded! maximum admitted {option.RateLimitRule.Limit} per {option.RateLimitRule.Period}." : option.QuotaExceededMessage;
|
||||
var message = this.GetResponseMessage(option);
|
||||
|
||||
if (!option.DisableRateLimitHeaders)
|
||||
{
|
||||
@ -128,6 +131,14 @@ namespace Ocelot.RateLimit.Middleware
|
||||
return httpContext.Response.WriteAsync(message);
|
||||
}
|
||||
|
||||
private string GetResponseMessage(RateLimitOptions option)
|
||||
{
|
||||
var message = string.IsNullOrEmpty(option.QuotaExceededMessage)
|
||||
? $"API calls quota exceeded! maximum admitted {option.RateLimitRule.Limit} per {option.RateLimitRule.Period}."
|
||||
: option.QuotaExceededMessage;
|
||||
return message;
|
||||
}
|
||||
|
||||
private Task SetRateLimitHeaders(object rateLimitHeaders)
|
||||
{
|
||||
var headers = (RateLimitHeaders)rateLimitHeaders;
|
||||
|
12
src/Ocelot/RateLimit/QuotaExceededError.cs
Normal file
12
src/Ocelot/RateLimit/QuotaExceededError.cs
Normal file
@ -0,0 +1,12 @@
|
||||
using Ocelot.Errors;
|
||||
|
||||
namespace Ocelot.RateLimit
|
||||
{
|
||||
public class QuotaExceededError : Error
|
||||
{
|
||||
public QuotaExceededError(string message)
|
||||
: base(message, OcelotErrorCode.QuotaExceededError)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -125,7 +125,7 @@ namespace Ocelot.UnitTests.Responder
|
||||
// If this test fails then it's because the number of error codes has changed.
|
||||
// You should make the appropriate changes to the test cases here to ensure
|
||||
// they cover all the error codes, and then modify this assertion.
|
||||
Enum.GetNames(typeof(OcelotErrorCode)).Length.ShouldBe(36, "Looks like the number of error codes has changed. Do you need to modify ErrorsToHttpStatusCodeMapper?");
|
||||
Enum.GetNames(typeof(OcelotErrorCode)).Length.ShouldBe(37, "Looks like the number of error codes has changed. Do you need to modify ErrorsToHttpStatusCodeMapper?");
|
||||
}
|
||||
|
||||
private void ShouldMapErrorToStatusCode(OcelotErrorCode errorCode, HttpStatusCode expectedHttpStatusCode)
|
||||
|
Loading…
x
Reference in New Issue
Block a user