From 0e0e8bb763aa59c1aa1422cbc40ea77b2c05f7dd Mon Sep 17 00:00:00 2001 From: dmalanij Date: Wed, 5 Apr 2017 12:59:27 +0200 Subject: [PATCH 1/4] Async identity building in Owin middleware Moved from SetIdentity to a SetIdentityAsync to allow better extensibility --- WebApiThrottle/ThrottlingMiddleware.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/WebApiThrottle/ThrottlingMiddleware.cs b/WebApiThrottle/ThrottlingMiddleware.cs index a9be672..06ec480 100644 --- a/WebApiThrottle/ThrottlingMiddleware.cs +++ b/WebApiThrottle/ThrottlingMiddleware.cs @@ -140,7 +140,7 @@ public override async Task Invoke(IOwinContext context) core.Repository = Repository; core.Policy = policy; - var identity = SetIdentity(request); + var identity = await SetIdentityAsync(request); if (core.IsWhitelisted(identity)) { @@ -213,7 +213,13 @@ public override async Task Invoke(IOwinContext context) await Next.Invoke(context); } + [Obsolete("This method is deprecated, use SetIdentityAsync instead")] protected virtual RequestIdentity SetIdentity(IOwinRequest request) + { + throw new NotImplementedException("This method is deprecated, use SetIdentityAsync instead"); + } + + protected virtual Task SetIdentityAsync(IOwinRequest request) { var entry = new RequestIdentity(); entry.ClientIp = request.RemoteIpAddress; @@ -222,7 +228,7 @@ protected virtual RequestIdentity SetIdentity(IOwinRequest request) ? request.Headers.GetValues("Authorization-Token").First() : "anon"; - return entry; + return Task.FromResult(entry); } protected virtual string ComputeThrottleKey(RequestIdentity requestIdentity, RateLimitPeriod period) From 408aff64038da8cfd7131f0959ca1f6124590169 Mon Sep 17 00:00:00 2001 From: dmalanij Date: Wed, 5 Apr 2017 14:05:40 +0200 Subject: [PATCH 2/4] Async identity building in ThrottlingHandler Moved from SetIdentity to a SetIdentityAsync to allow better extensibility --- WebApiThrottle/ThrottlingHandler.cs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/WebApiThrottle/ThrottlingHandler.cs b/WebApiThrottle/ThrottlingHandler.cs index 7bdcd40..0b05509 100644 --- a/WebApiThrottle/ThrottlingHandler.cs +++ b/WebApiThrottle/ThrottlingHandler.cs @@ -129,7 +129,7 @@ public ThrottlePolicy Policy /// public HttpStatusCode QuotaExceededResponseCode { get; set; } - protected override Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) + protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { // get policy from repo if (policyRepository != null) @@ -139,17 +139,17 @@ protected override Task SendAsync(HttpRequestMessage reques if (policy == null || (!policy.IpThrottling && !policy.ClientThrottling && !policy.EndpointThrottling)) { - return base.SendAsync(request, cancellationToken); + return await base.SendAsync(request, cancellationToken); } core.Repository = Repository; core.Policy = policy; - var identity = SetIdentity(request); + var identity = await SetIdentityAsync(request); if (core.IsWhitelisted(identity)) { - return base.SendAsync(request, cancellationToken); + return await base.SendAsync(request, cancellationToken); } TimeSpan timeSpan = TimeSpan.FromSeconds(1); @@ -204,7 +204,7 @@ protected override Task SendAsync(HttpRequestMessage reques : string.Format(message, rateLimit, rateLimitPeriod); // break execution - return QuotaExceededResponse( + return await QuotaExceededResponse( request, content, QuotaExceededResponseCode, @@ -214,7 +214,7 @@ protected override Task SendAsync(HttpRequestMessage reques } // no throttling required - return base.SendAsync(request, cancellationToken); + return await base.SendAsync(request, cancellationToken); } protected IPAddress GetClientIp(HttpRequestMessage request) @@ -222,7 +222,13 @@ protected IPAddress GetClientIp(HttpRequestMessage request) return core.GetClientIp(request); } + [Obsolete("This method is deprecated, use SetIdentityAsync instead")] protected virtual RequestIdentity SetIdentity(HttpRequestMessage request) + { + throw new NotImplementedException("This method is deprecated, use SetIdentityAsync instead"); + } + + protected virtual Task SetIdentityAsync(HttpRequestMessage request) { var entry = new RequestIdentity(); entry.ClientIp = core.GetClientIp(request).ToString(); @@ -231,7 +237,7 @@ protected virtual RequestIdentity SetIdentity(HttpRequestMessage request) ? request.Headers.GetValues("Authorization-Token").First() : "anon"; - return entry; + return Task.FromResult(entry); } protected virtual string ComputeThrottleKey(RequestIdentity requestIdentity, RateLimitPeriod period) From e07ac8fccf1d2c38df680510fa171f3c4aadbdc7 Mon Sep 17 00:00:00 2001 From: dmalanij Date: Wed, 5 Apr 2017 14:08:50 +0200 Subject: [PATCH 3/4] Updated CustomThrottlingHandler in demo Implpemented async methdo --- .../Helpers/CustomThrottlingHandler.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/WebApiThrottle.WebApiDemo/Helpers/CustomThrottlingHandler.cs b/WebApiThrottle.WebApiDemo/Helpers/CustomThrottlingHandler.cs index 0522dfe..6130fba 100644 --- a/WebApiThrottle.WebApiDemo/Helpers/CustomThrottlingHandler.cs +++ b/WebApiThrottle.WebApiDemo/Helpers/CustomThrottlingHandler.cs @@ -1,20 +1,21 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Threading.Tasks; using System.Web; namespace WebApiThrottle.WebApiDemo.Helpers { public class CustomThrottlingHandler : ThrottlingHandler { - protected override RequestIdentity SetIdentity(System.Net.Http.HttpRequestMessage request) + protected override Task SetIdentityAsync(System.Net.Http.HttpRequestMessage request) { - return new RequestIdentity() + return Task.FromResult(new RequestIdentity() { ClientKey = request.Headers.Contains("Authorization-Key") ? request.Headers.GetValues("Authorization-Key").First() : "anon", ClientIp = base.GetClientIp(request).ToString(), Endpoint = request.RequestUri.AbsolutePath.ToLowerInvariant() - }; + }); } } } \ No newline at end of file From c87bdb7fa965749e4cd0d193d7ee391053c00261 Mon Sep 17 00:00:00 2001 From: dmalanij Date: Wed, 5 Apr 2017 15:04:27 +0200 Subject: [PATCH 4/4] Async identity building in ThrottlingFilter Moved from SetIdentity to a SetIdentityAsync to allow better extensibility --- WebApiThrottle/ThrottlingFilter.cs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/WebApiThrottle/ThrottlingFilter.cs b/WebApiThrottle/ThrottlingFilter.cs index 7f1a93c..1b6faf7 100644 --- a/WebApiThrottle/ThrottlingFilter.cs +++ b/WebApiThrottle/ThrottlingFilter.cs @@ -129,7 +129,7 @@ public ThrottlePolicy Policy /// public HttpStatusCode QuotaExceededResponseCode { get; set; } - public override void OnActionExecuting(HttpActionContext actionContext) + public override async Task OnActionExecutingAsync(HttpActionContext actionContext, System.Threading.CancellationToken cancellationToken) { EnableThrottlingAttribute attrPolicy = null; var applyThrottling = ApplyThrottling(actionContext, out attrPolicy); @@ -145,7 +145,7 @@ public override void OnActionExecuting(HttpActionContext actionContext) core.Repository = Repository; core.Policy = Policy; - var identity = SetIdentity(actionContext.Request); + var identity = await SetIdentityAsync(actionContext.Request); if (!core.IsWhitelisted(identity)) { @@ -219,17 +219,24 @@ public override void OnActionExecuting(HttpActionContext actionContext) } } - base.OnActionExecuting(actionContext); + await base.OnActionExecutingAsync(actionContext, cancellationToken); } + + [Obsolete("This method is deprecated, use SetIdentityAsync instead")] protected virtual RequestIdentity SetIdentity(HttpRequestMessage request) + { + throw new NotImplementedException("This method is deprecated, use SetIdentityAsync instead"); + } + + protected virtual Task SetIdentityAsync(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() : "anon"; - return entry; + return Task.FromResult(entry); } protected virtual string ComputeThrottleKey(RequestIdentity requestIdentity, RateLimitPeriod period)