-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from jezzsantos/master
Declarative Limits
- Loading branch information
Showing
26 changed files
with
747 additions
and
194 deletions.
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
43 changes: 43 additions & 0 deletions
43
src/ServiceStack.RateLimit.Redis/AttributeLimitProvider.cs
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,43 @@ | ||
// // This Source Code Form is subject to the terms of the Mozilla Public | ||
// // License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// // file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
namespace ServiceStack.RateLimit.Redis | ||
{ | ||
using Configuration; | ||
using Interfaces; | ||
using Models; | ||
using Web; | ||
|
||
public class AttributeLimitProvider : ILimitProvider | ||
{ | ||
private readonly IAppSettings appSettings; | ||
|
||
public AttributeLimitProvider(IAppSettings appSettings) | ||
{ | ||
appSettings.ThrowIfNull(nameof(appSettings)); | ||
|
||
this.appSettings = appSettings; | ||
} | ||
|
||
public Limits GetLimits(IRequest request) | ||
{ | ||
var limits = request.Items.GetValueOrDefault(LimitRateAttribute.RequestItemName) as Limits; | ||
if (limits != null) | ||
{ | ||
return limits; | ||
} | ||
|
||
return new Limits | ||
{ | ||
Request = LimitProviderConstants.DefaultLimits, | ||
User = null | ||
}; | ||
} | ||
|
||
public string GetRateLimitScriptId() | ||
{ | ||
return appSettings.GetString(LimitProviderConstants.ScriptKey); | ||
} | ||
} | ||
} |
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
24 changes: 24 additions & 0 deletions
24
src/ServiceStack.RateLimit.Redis/LimitProviderConstants.cs
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,24 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
namespace ServiceStack.RateLimit.Redis | ||
{ | ||
using System.Collections.Generic; | ||
using Models; | ||
|
||
public class LimitProviderConstants | ||
{ | ||
public const string ScriptKey = "script:ratelimit"; | ||
public const int DefaultPerMinute = 10; | ||
public const int DefaultPerHour = 30; | ||
|
||
public static readonly LimitGroup DefaultLimits=new LimitGroup | ||
{ | ||
Limits = new List<LimitPerSecond> | ||
{ | ||
new LimitPerSecond { Seconds = 60, Limit = DefaultPerMinute }, | ||
new LimitPerSecond { Seconds = 3600, Limit = DefaultPerHour } | ||
} | ||
}; | ||
} | ||
} |
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,114 @@ | ||
// // This Source Code Form is subject to the terms of the Mozilla Public | ||
// // License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// // file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
namespace ServiceStack.RateLimit.Redis | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Models; | ||
using Web; | ||
|
||
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] | ||
public class LimitRateAttribute : RequestFilterAttribute | ||
{ | ||
public const string RequestItemName = "RateLimit.Limits"; | ||
|
||
public LimitRateAttribute() : this(LimitType.PerRequest, LimitProviderConstants.DefaultPerMinute, | ||
(int) RatePeriod.PerMinute) | ||
{ | ||
} | ||
|
||
public LimitRateAttribute(LimitType type) : this(type, LimitProviderConstants.DefaultPerMinute, | ||
(int) RatePeriod.PerMinute) | ||
{ | ||
} | ||
|
||
public LimitRateAttribute(int limit, int seconds) : this(LimitType.PerRequest, limit, seconds) | ||
{ | ||
} | ||
|
||
public LimitRateAttribute(int limit, RatePeriod period = RatePeriod.PerMinute) : this(LimitType.PerRequest, | ||
limit, (int) period) | ||
{ | ||
} | ||
|
||
public LimitRateAttribute(LimitType type, int limit, RatePeriod period = RatePeriod.PerMinute) : this(type, | ||
limit, (int) period) | ||
{ | ||
} | ||
|
||
public LimitRateAttribute(LimitType type, int limit, int seconds) | ||
{ | ||
if (limit <= 0) | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(limit)); | ||
} | ||
if (seconds <= 0) | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(seconds)); | ||
} | ||
|
||
Limit = limit; | ||
Seconds = seconds; | ||
Type = type; | ||
} | ||
|
||
public int Limit { get; } | ||
|
||
public int Seconds { get; } | ||
|
||
public LimitType Type { get; } | ||
|
||
public override void Execute(IRequest req, IResponse res, object requestDto) | ||
{ | ||
var limits = req.Items.GetValueOrDefault(RequestItemName) as Limits; | ||
if (limits == null) | ||
{ | ||
req.Items[RequestItemName] = new Limits | ||
{ | ||
Request = Type == LimitType.PerRequest | ||
? new LimitGroup | ||
{ | ||
Limits = new List<LimitPerSecond> | ||
{ | ||
this.ConvertTo<LimitPerSecond>() | ||
} | ||
} | ||
: null, | ||
User = Type == LimitType.PerUser | ||
? new LimitGroup | ||
{ | ||
Limits = new List<LimitPerSecond> | ||
{ | ||
this.ConvertTo<LimitPerSecond>() | ||
} | ||
} | ||
: null | ||
}; | ||
} | ||
else | ||
{ | ||
var group = Type == LimitType.PerRequest ? limits.Request : limits.User; | ||
var limit = (group ?? new LimitGroup()).Limits.Safe().ToList(); | ||
limit.Add(this.ConvertTo<LimitPerSecond>()); | ||
group.Limits = limit; | ||
} | ||
} | ||
} | ||
|
||
public enum RatePeriod | ||
{ | ||
PerSecond = 1, | ||
PerMinute = 60, | ||
PerHour = 3600, | ||
PerDay = 86400 | ||
} | ||
|
||
public enum LimitType | ||
{ | ||
PerUser = 0, | ||
PerRequest = 1 | ||
} | ||
} |
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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<packages> | ||
<package id="ServiceStack" version="4.0.56" targetFramework="net452" /> | ||
<package id="ServiceStack.Client" version="4.0.56" targetFramework="net452" /> | ||
<package id="ServiceStack.Common" version="4.0.56" targetFramework="net452" /> | ||
<package id="ServiceStack.Interfaces" version="4.0.56" targetFramework="net452" /> | ||
<package id="ServiceStack.Redis" version="4.0.56" targetFramework="net452" /> | ||
<package id="ServiceStack.Text" version="4.0.56" targetFramework="net452" /> | ||
<package id="ServiceStack" version="4.5.14" targetFramework="net452" /> | ||
<package id="ServiceStack.Client" version="4.5.14" targetFramework="net452" /> | ||
<package id="ServiceStack.Common" version="4.5.14" targetFramework="net452" /> | ||
<package id="ServiceStack.Interfaces" version="4.5.14" targetFramework="net452" /> | ||
<package id="ServiceStack.Redis" version="4.5.14" targetFramework="net452" /> | ||
<package id="ServiceStack.Text" version="4.5.14" targetFramework="net452" /> | ||
</packages> |
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
Oops, something went wrong.