Beispiel #1
0
 public LeakyBucketStrategy(int maximumRequestQuota, int restoreRateAmount, int restoreRateTimeAmount, EnumRestoreRateTimePeriod restoreRateTimePeriod)
 {
     MaximumRequestQuota   = maximumRequestQuota;
     RestoreRateAmount     = restoreRateAmount;
     RestoreRateTimePeriod = restoreRateTimePeriod;
     RestoreRateTimeAmount = restoreRateTimeAmount;
     buckets = new ConcurrentDictionary <int, Bucket>();
 }
Beispiel #2
0
        public void MultipleRulesEUUser()
        {
            User user = api.AuthenticateUser("test auth token");

            user.CountryCode = "EU";
            var filter = new LocationBasedFilter {
                CountryCode = user.CountryCode
            };
            //leacky bucket params
            int maximumRequestQuota   = 2;
            int restoreRateAmount     = 1;
            int restoreRateTimeAmount = 2;
            EnumRestoreRateTimePeriod restoreRateTimePeriod = EnumRestoreRateTimePeriod.Seconds;

            int fixedWindowStrategyMaxRequestsPerWindow = 10;
            int fixedWindowStrategyTimeWindowInSeconds  = 2;

            List <RateLimiterRule> rules = new List <RateLimiterRule> {
                new RateLimiterRule(new FixedWindowStrategy(fixedWindowStrategyMaxRequestsPerWindow, fixedWindowStrategyTimeWindowInSeconds),
                                    new LocationBasedFilter {
                    CountryCode = "US"
                }),
                new RateLimiterRule(new LeakyBucketStrategy(maximumRequestQuota, restoreRateAmount, restoreRateTimeAmount, restoreRateTimePeriod),
                                    new LocationBasedFilter {
                    CountryCode = "EU"
                })
            };

            int expectedAllowedCount = 5;
            int actualAllowedCount   = 0;

            user.Id = 200;
            //  trigger Rate limiting with different users with EU token.LeakyBucket expected. Requests 1,2,4,5 allowed, rest are denied
            for (int i = 1; i <= 20; i++)
            {
                if (rateLimiter.ValidatedRuleList(user.Id, i, rules, filter))
                {
                    actualAllowedCount++;
                    getCallActualOutput = api.DoGetCall(user);
                    TestContext.WriteLine("Allowed request id: {0}", i);
                }
                else
                {
                    TestContext.WriteLine("Denied request id: {0}", i);
                }
                if (i % 5 == 0)
                {
                    Thread.Sleep(2500);
                }
            }
            TestContext.WriteLine("actualAllowCount: {0}", actualAllowedCount);

            Assert.AreEqual(expectedAllowedCount, actualAllowedCount);
        }
Beispiel #3
0
        public void SimpleRuleWithLeakyBucketStrategyNoFilters()
        {
            User user = api.AuthenticateUser("test auth token");

            int maximumRequestQuota   = 10;
            int restoreRateAmount     = 1;
            int restoreRateTimeAmount = 2;
            EnumRestoreRateTimePeriod restoreRateTimePeriod = EnumRestoreRateTimePeriod.Seconds;

            int             requestId = 123;
            RateLimiterRule rule      = new RateLimiterRule(new LeakyBucketStrategy(maximumRequestQuota, restoreRateAmount, restoreRateTimeAmount, restoreRateTimePeriod));

            if (rateLimiter.ValidateRule(user.Id, requestId, rule))
            {
                getCallActualOutput = api.DoGetCall(user);
            }

            Assert.AreEqual(getCallExpectedOutput, getCallActualOutput);
        }