mirror of
				https://github.com/nsnail/Ocelot.git
				synced 2025-11-04 09:15:27 +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:
		
				
					committed by
					
						
						Thiago Loureiro
					
				
			
			
				
	
			
			
			
						parent
						
							799abf55c4
						
					
				
				
					commit
					e76a51ffc9
				
			@@ -37,6 +37,7 @@
 | 
			
		||||
        CouldNotFindPlaceholderError,
 | 
			
		||||
        CouldNotFindAggregatorError,
 | 
			
		||||
        CannotAddPlaceholderError,
 | 
			
		||||
        CannotRemovePlaceholderError
 | 
			
		||||
        CannotRemovePlaceholderError,
 | 
			
		||||
        QuotaExceededError
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -69,6 +69,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)
 | 
			
		||||
        {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user