-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rate limit #37
Merged
Merged
Rate limit #37
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
2ba91ae
Merge remote-tracking branch 'refs/remotes/TomPallister/develop' into…
geffzhang 19f96b7
Merge branch 'release/1.1.0'
2e0bd6d
Merge pull request #2 from TomPallister/develop
geffzhang 338d1c0
Merge pull request #5 from TomPallister/develop
geffzhang f04988f
Merge remote-tracking branch 'refs/remotes/TomPallister/develop' into…
geffzhang 456c2b9
Merge branch 'develop' of https://github.com/geffzhang/Ocelot into de…
geffzhang ca9b4b9
Merge branch 'release/1.1.1'
TomPallister d2a31d0
Merge branch 'release/1.3.0'
TomPallister bc5f284
Merge pull request #6 from TomPallister/develop
geffzhang 9267918
Merge pull request #7 from TomPallister/master
geffzhang 08c9700
Merge remote-tracking branch 'refs/remotes/origin/master' into develop
geffzhang e1d5ef3
implement Request Rate limit, this feature is options
geffzhang 9b06afc
refactor ratelimit config
geffzhang e1f16c2
add ratelimit acceptance test
geffzhang 659d124
reset sdk version
geffzhang 2fa6e66
refactor code
geffzhang c966227
Merge pull request #9 from TomPallister/develop
geffzhang 0aad1f8
Merge remote-tracking branch 'refs/remotes/origin/develop' into RateL…
geffzhang ab6ae8a
merge newest code for develop
geffzhang f302ee7
remove bak file
geffzhang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace Ocelot.Configuration.File | ||
{ | ||
public class FileRateLimitOptions | ||
{ | ||
/// <summary> | ||
/// Gets or sets the HTTP header that holds the client identifier, by default is X-ClientId | ||
/// </summary> | ||
public string ClientIdHeader { get; set; } = "ClientId"; | ||
|
||
/// <summary> | ||
/// Gets or sets a value that will be used as a formatter for the QuotaExceeded response message. | ||
/// If none specified the default will be: | ||
/// API calls quota exceeded! maximum admitted {0} per {1} | ||
/// </summary> | ||
public string QuotaExceededMessage { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the counter prefix, used to compose the rate limit counter cache key | ||
/// </summary> | ||
public string RateLimitCounterPrefix { get; set; } = "ocelot"; | ||
|
||
/// <summary> | ||
/// Disables X-Rate-Limit and Rety-After headers | ||
/// </summary> | ||
public bool DisableRateLimitHeaders { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the HTTP Status code returned when rate limiting occurs, by default value is set to 429 (Too Many Requests) | ||
/// </summary> | ||
public int HttpStatusCode { get; set; } = 429; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace Ocelot.Configuration.File | ||
{ | ||
|
||
public class FileRateLimitRule | ||
{ | ||
public FileRateLimitRule() | ||
{ | ||
ClientWhitelist = new List<string>(); | ||
} | ||
|
||
public List<string> ClientWhitelist { get; set; } | ||
|
||
/// <summary> | ||
/// Enables endpoint rate limiting based URL path and HTTP verb | ||
/// </summary> | ||
public bool EnableRateLimiting { get; set; } | ||
|
||
/// <summary> | ||
/// Rate limit period as in 1s, 1m, 1h | ||
/// </summary> | ||
public string Period { get; set; } | ||
|
||
public double PeriodTimespan { get; set; } | ||
/// <summary> | ||
/// Maximum number of requests that a client can make in a defined period | ||
/// </summary> | ||
public long Limit { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace Ocelot.Configuration | ||
{ | ||
/// <summary> | ||
/// RateLimit Options | ||
/// </summary> | ||
public class RateLimitOptions | ||
{ | ||
public RateLimitOptions(bool enbleRateLimiting, string clientIdHeader, List<string> clientWhitelist,bool disableRateLimitHeaders, | ||
string quotaExceededMessage, string rateLimitCounterPrefix, RateLimitRule rateLimitRule, int httpStatusCode) | ||
{ | ||
EnableRateLimiting = enbleRateLimiting; | ||
ClientIdHeader = clientIdHeader; | ||
ClientWhitelist = clientWhitelist?? new List<string>(); | ||
DisableRateLimitHeaders = disableRateLimitHeaders; | ||
QuotaExceededMessage = quotaExceededMessage; | ||
RateLimitCounterPrefix = rateLimitCounterPrefix; | ||
RateLimitRule = rateLimitRule; | ||
HttpStatusCode = httpStatusCode; | ||
} | ||
|
||
public RateLimitRule RateLimitRule { get; private set; } | ||
|
||
public List<string> ClientWhitelist { get; private set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the HTTP header that holds the client identifier, by default is X-ClientId | ||
/// </summary> | ||
public string ClientIdHeader { get; private set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the HTTP Status code returned when rate limiting occurs, by default value is set to 429 (Too Many Requests) | ||
/// </summary> | ||
public int HttpStatusCode { get; private set; } | ||
|
||
/// <summary> | ||
/// Gets or sets a value that will be used as a formatter for the QuotaExceeded response message. | ||
/// If none specified the default will be: | ||
/// API calls quota exceeded! maximum admitted {0} per {1} | ||
/// </summary> | ||
public string QuotaExceededMessage { get; private set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the counter prefix, used to compose the rate limit counter cache key | ||
/// </summary> | ||
public string RateLimitCounterPrefix { get; private set; } | ||
|
||
/// <summary> | ||
/// Enables endpoint rate limiting based URL path and HTTP verb | ||
/// </summary> | ||
public bool EnableRateLimiting { get; private set; } | ||
|
||
/// <summary> | ||
/// Disables X-Rate-Limit and Rety-After headers | ||
/// </summary> | ||
public bool DisableRateLimitHeaders { get; private set; } | ||
} | ||
|
||
public class RateLimitRule | ||
{ | ||
public RateLimitRule(string period, TimeSpan periodTimespan, long limit) | ||
{ | ||
Period = period; | ||
PeriodTimespan = periodTimespan; | ||
Limit = limit; | ||
} | ||
|
||
/// <summary> | ||
/// Rate limit period as in 1s, 1m, 1h,1d | ||
/// </summary> | ||
public string Period { get; private set; } | ||
|
||
public TimeSpan PeriodTimespan { get; private set; } | ||
/// <summary> | ||
/// Maximum number of requests that a client can make in a defined period | ||
/// </summary> | ||
public long Limit { get; private set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using Microsoft.AspNetCore.Http; | ||
using Ocelot.Configuration; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace Ocelot.RateLimit | ||
{ | ||
public class ClientRateLimitProcessor | ||
{ | ||
private readonly IRateLimitCounterHandler _counterHandler; | ||
private readonly RateLimitCore _core; | ||
|
||
public ClientRateLimitProcessor(IRateLimitCounterHandler counterHandler) | ||
{ | ||
_counterHandler = counterHandler; | ||
_core = new RateLimitCore(_counterHandler); | ||
} | ||
|
||
public RateLimitCounter ProcessRequest(ClientRequestIdentity requestIdentity, RateLimitOptions option) | ||
{ | ||
return _core.ProcessRequest(requestIdentity, option); | ||
} | ||
|
||
public string RetryAfterFrom(DateTime timestamp, RateLimitRule rule) | ||
{ | ||
return _core.RetryAfterFrom(timestamp, rule); | ||
} | ||
|
||
public RateLimitHeaders GetRateLimitHeaders(HttpContext context, ClientRequestIdentity requestIdentity, RateLimitOptions option) | ||
{ | ||
return _core.GetRateLimitHeaders(context, requestIdentity, option); | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this class be changed so that it's properties are {get; private set;} when constructing an object I prefer that it's properties cannot be changed.