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

Commit 388501f

Browse files
committed
Added per Week rate limit
1 parent b926d1a commit 388501f

File tree

4 files changed

+11
-3
lines changed

4 files changed

+11
-3
lines changed

WebApiThrottle.Demo/App_Start/WebApiConfig.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static void Register(HttpConfiguration config)
3131
//Web API throttling
3232
config.MessageHandlers.Add(new ThrottlingHandler()
3333
{
34-
Policy = new ThrottlePolicy(perSecond: 1, perMinute: 20, perHour: 30, perDay: 35)
34+
Policy = new ThrottlePolicy(perSecond: 1, perMinute: 20, perHour: 30, perDay: 35, perWeek: 3000)
3535
{
3636
//scope to IPs
3737
IpThrottling = true,

WebApiThrottle/RateLimit.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class RateLimits
1313
public long PerMinute { get; set; }
1414
public long PerHour { get; set; }
1515
public long PerDay { get; set; }
16+
public long PerWeek { get; set; }
1617

1718
public long GetLimit(RateLimitPeriod period)
1819
{
@@ -26,6 +27,8 @@ public long GetLimit(RateLimitPeriod period)
2627
return PerHour;
2728
case RateLimitPeriod.Day:
2829
return PerDay;
30+
case RateLimitPeriod.Week:
31+
return PerWeek;
2932
default:
3033
return PerSecond;
3134
}
@@ -37,6 +40,7 @@ public enum RateLimitPeriod
3740
Second = 1,
3841
Minute,
3942
Hour,
40-
Day
43+
Day,
44+
Week
4145
}
4246
}

WebApiThrottle/ThrottlePolicy.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,14 @@ public class ThrottlePolicy
4343
/// <summary>
4444
/// Configure default request limits per second, minute, hour or day
4545
/// </summary>
46-
public ThrottlePolicy(long? perSecond, long? perMinute = null, long? perHour = null, long? perDay = null)
46+
public ThrottlePolicy(long? perSecond, long? perMinute = null, long? perHour = null, long? perDay = null, long? perWeek = null)
4747
{
4848
Rates = new Dictionary<RateLimitPeriod, long>();
4949
if (perSecond.HasValue) Rates.Add(RateLimitPeriod.Second, perSecond.Value);
5050
if (perMinute.HasValue) Rates.Add(RateLimitPeriod.Minute, perMinute.Value);
5151
if (perHour.HasValue) Rates.Add(RateLimitPeriod.Hour, perHour.Value);
5252
if (perDay.HasValue) Rates.Add(RateLimitPeriod.Day, perDay.Value);
53+
if (perWeek.HasValue) Rates.Add(RateLimitPeriod.Week, perWeek.Value);
5354
}
5455

5556
}

WebApiThrottle/ThrottlingHandler.cs

+3
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage reques
9191
case RateLimitPeriod.Day:
9292
timeSpan = TimeSpan.FromDays(1);
9393
break;
94+
case RateLimitPeriod.Week:
95+
timeSpan = TimeSpan.FromDays(7);
96+
break;
9497
}
9598

9699
//increment counter

0 commit comments

Comments
 (0)