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

Added option to use the controller action as the throttle key #56

Open
wants to merge 2 commits 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
8 changes: 8 additions & 0 deletions WebApiThrottle/Models/EndpointThrottlingMethod.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace WebApiThrottle.Models
{
public enum EndpointThrottlingMethod
{
Url,
Action
}
}
4 changes: 4 additions & 0 deletions WebApiThrottle/Models/RequestIdentity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public class RequestIdentity

public string Endpoint { get; set; }

public string ActionName { get; set; }

public string ControllerName { get; set; }

public bool ForceWhiteList { get; set; }
}
}
4 changes: 4 additions & 0 deletions WebApiThrottle/ThrottlePolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

namespace WebApiThrottle
{
using Models;

/// <summary>
/// Rate limits policy
/// </summary>
Expand Down Expand Up @@ -80,6 +82,8 @@ public ThrottlePolicy(long? perSecond = null, long? perMinute = null, long? perH
/// </summary>
public bool EndpointThrottling { get; set; }

public EndpointThrottlingMethod EndpointThrottleMethod { get; set; }

public List<string> EndpointWhitelist { get; set; }

public IDictionary<string, RateLimits> EndpointRules { get; set; }
Expand Down
14 changes: 13 additions & 1 deletion WebApiThrottle/ThrottlingCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

namespace WebApiThrottle
{
using Models;

/// <summary>
/// Common code shared between ThrottlingHandler and ThrottlingFilter
/// </summary>
Expand Down Expand Up @@ -135,7 +137,17 @@ internal string ComputeThrottleKey(RequestIdentity requestIdentity, RateLimitPer

if (Policy.EndpointThrottling)
{
keyValues.Add(requestIdentity.Endpoint);
if (Policy.EndpointThrottleMethod == EndpointThrottlingMethod.Action &&
!string.IsNullOrWhiteSpace(requestIdentity.ActionName) &&
!string.IsNullOrWhiteSpace(requestIdentity.ControllerName))
{
//TODO: think of a better way to do this
keyValues.Add(requestIdentity.ControllerName + "." + requestIdentity.ActionName);
}
else if (Policy.EndpointThrottleMethod == EndpointThrottlingMethod.Url)
{
keyValues.Add(requestIdentity.Endpoint);
}
}

keyValues.Add(period.ToString());
Expand Down
8 changes: 8 additions & 0 deletions WebApiThrottle/ThrottlingFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,14 @@ protected virtual RequestIdentity SetIdentity(HttpRequestMessage request)
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() : "anon";

var actionDescriptor = request.GetActionDescriptor();
if (actionDescriptor == null)
return entry;

entry.ActionName = actionDescriptor.ActionName;
if(actionDescriptor.ControllerDescriptor != null)
entry.ControllerName = actionDescriptor.ControllerDescriptor.ControllerName;

return entry;
}
Expand Down
1 change: 1 addition & 0 deletions WebApiThrottle/WebApiThrottle.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Attributes\DisableThrottingAttribute.cs" />
<Compile Include="Models\EndpointThrottlingMethod.cs" />
<Compile Include="Net\DefaultIpAddressParser.cs" />
<Compile Include="Net\HttpRequestExtensions.cs" />
<Compile Include="Net\IIpAddressParser.cs" />
Expand Down