Skip to content
This repository has been archived by the owner on Nov 1, 2023. It is now read-only.

Adding effective increment for requests #151

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions WebApiThrottle/ThrottlingCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ internal List<KeyValuePair<RateLimitPeriod, long>> RatesWithDefaults(List<KeyVal
return defRates;
}

internal ThrottleCounter ProcessRequest(TimeSpan timeSpan, string id)
internal ThrottleCounter ProcessRequest(TimeSpan timeSpan, string id, int effectiveInffective)
{
var throttleCounter = new ThrottleCounter()
{
Expand All @@ -202,7 +202,7 @@ internal ThrottleCounter ProcessRequest(TimeSpan timeSpan, string id)
if (entry.Value.Timestamp + timeSpan >= DateTime.UtcNow)
{
// increment request count
var totalRequests = entry.Value.TotalRequests + 1;
var totalRequests = entry.Value.TotalRequests + effectiveInffective;

// deep copy
throttleCounter = new ThrottleCounter
Expand Down
7 changes: 6 additions & 1 deletion WebApiThrottle/ThrottlingFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public override void OnActionExecuting(HttpActionContext actionContext)
{
// increment counter
var requestId = ComputeThrottleKey(identity, rateLimitPeriod);
var throttleCounter = core.ProcessRequest(timeSpan, requestId);
var throttleCounter = core.ProcessRequest(timeSpan, requestId, this.EffectiveIncrement);

// check if key expired
if (throttleCounter.Timestamp + timeSpan < DateTime.UtcNow)
Expand Down Expand Up @@ -222,6 +222,11 @@ public override void OnActionExecuting(HttpActionContext actionContext)
base.OnActionExecuting(actionContext);
}

protected virtual int EffectiveIncrement
{
get { return 1; }
}

protected virtual RequestIdentity SetIdentity(HttpRequestMessage request)
{
var entry = new RequestIdentity();
Expand Down
21 changes: 13 additions & 8 deletions WebApiThrottle/ThrottlingHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ public ThrottlingHandler()
/// <param name="ipAddressParser">
/// The IpAddressParser
/// </param>
public ThrottlingHandler(ThrottlePolicy policy,
IPolicyRepository policyRepository,
IThrottleRepository repository,
public ThrottlingHandler(ThrottlePolicy policy,
IPolicyRepository policyRepository,
IThrottleRepository repository,
IThrottleLogger logger,
IIpAddressParser ipAddressParser = null)
{
Expand Down Expand Up @@ -178,7 +178,7 @@ protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage reques
{
// increment counter
var requestId = ComputeThrottleKey(identity, rateLimitPeriod);
var throttleCounter = core.ProcessRequest(timeSpan, requestId);
var throttleCounter = core.ProcessRequest(timeSpan, requestId, this.EffectiveIncrement);

// check if key expired
if (throttleCounter.Timestamp + timeSpan < DateTime.UtcNow)
Expand All @@ -195,8 +195,8 @@ protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage reques
Logger.Log(core.ComputeLogEntry(requestId, identity, throttleCounter, rateLimitPeriod.ToString(), rateLimit, request));
}

var message = !string.IsNullOrEmpty(this.QuotaExceededMessage)
? this.QuotaExceededMessage
var message = !string.IsNullOrEmpty(this.QuotaExceededMessage)
? this.QuotaExceededMessage
: "API calls quota exceeded! maximum admitted {0} per {1}.";

var content = this.QuotaExceededContent != null
Expand All @@ -217,6 +217,11 @@ protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage reques
return base.SendAsync(request, cancellationToken);
}

protected virtual int EffectiveIncrement
{
get { return 1; }
}

protected IPAddress GetClientIp(HttpRequestMessage request)
{
return core.GetClientIp(request);
Expand All @@ -227,8 +232,8 @@ protected virtual RequestIdentity SetIdentity(HttpRequestMessage request)
var entry = new RequestIdentity();
entry.ClientIp = core.GetClientIp(request).ToString();
entry.Endpoint = request.RequestUri.AbsolutePath.ToLowerInvariant();
entry.ClientKey = request.Headers.Contains("Authorization-Token")
? request.Headers.GetValues("Authorization-Token").First()
entry.ClientKey = request.Headers.Contains("Authorization-Token")
? request.Headers.GetValues("Authorization-Token").First()
: "anon";

return entry;
Expand Down
15 changes: 10 additions & 5 deletions WebApiThrottle/ThrottlingMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ public ThrottlingMiddleware(OwinMiddleware next)
/// <param name="ipAddressParser">
/// The IpAddressParser
/// </param>
public ThrottlingMiddleware(OwinMiddleware next,
ThrottlePolicy policy,
IPolicyRepository policyRepository,
IThrottleRepository repository,
public ThrottlingMiddleware(OwinMiddleware next,
ThrottlePolicy policy,
IPolicyRepository policyRepository,
IThrottleRepository repository,
IThrottleLogger logger,
IIpAddressParser ipAddressParser)
: base(next)
Expand Down Expand Up @@ -174,7 +174,7 @@ public override async Task Invoke(IOwinContext context)
{
// increment counter
var requestId = ComputeThrottleKey(identity, rateLimitPeriod);
var throttleCounter = core.ProcessRequest(timeSpan, requestId);
var throttleCounter = core.ProcessRequest(timeSpan, requestId, this.EffectiveIncrement);

// check if key expired
if (throttleCounter.Timestamp + timeSpan < DateTime.UtcNow)
Expand Down Expand Up @@ -213,6 +213,11 @@ public override async Task Invoke(IOwinContext context)
await Next.Invoke(context);
}

protected virtual int EffectiveIncrement
{
get { return 1; }
}

protected virtual RequestIdentity SetIdentity(IOwinRequest request)
{
var entry = new RequestIdentity();
Expand Down