From af3d811bcfc16d9f1abfe865e273a2cc211dfa8c Mon Sep 17 00:00:00 2001 From: Gilad Suberri Date: Sun, 9 Feb 2020 14:10:27 +0200 Subject: [PATCH] Only apply rules if they exist --- WebApiThrottle/ThrottlingCore.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/WebApiThrottle/ThrottlingCore.cs b/WebApiThrottle/ThrottlingCore.cs index a179792..ca2972e 100644 --- a/WebApiThrottle/ThrottlingCore.cs +++ b/WebApiThrottle/ThrottlingCore.cs @@ -249,7 +249,7 @@ internal TimeSpan GetTimeSpanFromPeriod(RateLimitPeriod rateLimitPeriod) internal void ApplyRules(RequestIdentity identity, TimeSpan timeSpan, RateLimitPeriod rateLimitPeriod, ref long rateLimit) { // apply endpoint rate limits - if (Policy.EndpointRules != null) + if (Policy.EndpointRules?.Any() == true) { var rules = Policy.EndpointRules.Where(x => identity.Endpoint.IndexOf(x.Key, 0, StringComparison.InvariantCultureIgnoreCase) != -1).ToList(); if (rules.Any()) @@ -265,7 +265,7 @@ internal void ApplyRules(RequestIdentity identity, TimeSpan timeSpan, RateLimitP } // apply custom rate limit for clients that will override endpoint limits - if (Policy.ClientRules != null && Policy.ClientRules.Keys.Contains(identity.ClientKey)) + if (Policy.ClientRules?.Any() == true && Policy.ClientRules.Keys.Contains(identity.ClientKey)) { var limit = Policy.ClientRules[identity.ClientKey].GetLimit(rateLimitPeriod); if (limit > 0) @@ -276,7 +276,7 @@ internal void ApplyRules(RequestIdentity identity, TimeSpan timeSpan, RateLimitP // enforce ip rate limit as is most specific string ipRule = null; - if (Policy.IpRules != null && ContainsIp(Policy.IpRules.Keys.ToList(), identity.ClientIp, out ipRule)) + if (Policy.IpRules?.Any() == true && ContainsIp(Policy.IpRules.Keys.ToList(), identity.ClientIp, out ipRule)) { var limit = Policy.IpRules[ipRule].GetLimit(rateLimitPeriod); if (limit > 0)