Example #1
0
        public void TestSetup()
        {
            property          = Guid.NewGuid().ToString().Substring(0, 8);
            propertyValue     = "value" + Guid.NewGuid().ToString().Substring(0, 8);
            blacklist         = new List <string>();
            whitelist         = new List <string>();
            globalLimit       = 0.75;
            individualLimits  = new Dictionary <string, double>();
            requestProperties = new Dictionary <string, string>
            {
                [property] = propertyValue
            };
            totalCapacity = 100;
            consumption   = 10;

            context = Substitute.For <IThrottlingQuotaContext>();
            context.CapacityLimit.Returns(_ => totalCapacity);
            context.GetConsumption(property, requestProperties[property]).Returns(_ => consumption);
        }
Example #2
0
        public ThrottlingQuotaVerdict Check(IReadOnlyDictionary <string, string> properties, IThrottlingQuotaContext context)
        {
            if (!properties.TryGetValue(Property, out var value))
            {
                return(ThrottlingQuotaVerdict.Allow());
            }

            if (blacklist.Contains(value))
            {
                return(ThrottlingQuotaVerdict.Reject($"'{Property}' = '{value}' is blacklisted."));
            }

            if (whitelist.Contains(value))
            {
                return(ThrottlingQuotaVerdict.Allow());
            }

            var consumption = context.GetConsumption(Property, value) + 1;
            var utilization = (double)consumption / Math.Max(1, context.CapacityLimit);
            var limit       = individualLimits.TryGetValue(value, out var individualLimit) ? individualLimit : globalLimit;

            return(utilization <= limit
                ? ThrottlingQuotaVerdict.Allow()
                : ThrottlingQuotaVerdict.Reject($"Capacity utilization for '{Property}' = '{value}' ({utilization:F2}) would exceed the configured limit ({limit:F2})."));
        }