-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add throttle to rate limit the API calls
- Loading branch information
Showing
4 changed files
with
71 additions
and
2 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
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,59 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.Controllers; | ||
using Microsoft.AspNetCore.Mvc.Filters; | ||
using Microsoft.Extensions.Caching.Memory; | ||
using System; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Server.Filters | ||
{ | ||
public enum TimeUnit | ||
{ | ||
Second = 1, | ||
Minute = 60, | ||
Hour = 3600, | ||
Day = 86400 | ||
} | ||
|
||
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] | ||
public class ThrottleAttribute : ActionFilterAttribute | ||
{ | ||
public TimeUnit TimeUnit { get; set; } | ||
public int Count { get; set; } | ||
|
||
public override void OnActionExecuting(ActionExecutingContext filterContext) | ||
{ | ||
var seconds = Convert.ToInt32(TimeUnit); | ||
|
||
var controller = filterContext.ActionDescriptor as ControllerActionDescriptor; | ||
Console.WriteLine(filterContext.HttpContext.Connection.RemoteIpAddress); | ||
|
||
var key = string.Join( | ||
"-", | ||
seconds, | ||
filterContext.HttpContext.Request.Method, | ||
controller.ControllerName, | ||
controller.ActionName, | ||
filterContext.HttpContext.Connection.RemoteIpAddress | ||
); | ||
|
||
// increment the cache value | ||
var cnt = 1; | ||
IMemoryCache cache = filterContext.HttpContext.RequestServices.GetService<IMemoryCache>(); | ||
if (cache.TryGetValue(key, out cnt)) | ||
{ | ||
cnt++; | ||
} | ||
cache.Set(key, cnt, DateTime.UtcNow.AddSeconds(seconds)); | ||
|
||
if (cnt > Count) | ||
{ | ||
filterContext.Result = new ContentResult | ||
{ | ||
Content = "You are allowed to make only " + Count + " requests per " + TimeUnit.ToString().ToLower() | ||
}; | ||
filterContext.HttpContext.Response.StatusCode = 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