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

Only apply rules if they exist #138

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions WebApiThrottle/ThrottlingCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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)
Expand All @@ -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)
Expand Down