public RateLimiterDispatcherTests(ITestOutputHelper testOutput)
        {
            _TestOutput = testOutput;
            var limiter = new CountByIntervalAwaitableConstraint(1, TimeSpan.FromMilliseconds(Interval));

            _RateLimiterDispatcher = new RateLimiterDispatcher(limiter);
        }
示例#2
0
 public CountByIntervalAwaitableConstraintTest()
 {
     _Origin   = new DateTime(2000, 1, 1);
     _MockTime = new MockTime(_Origin);
     _CountByIntervalAwaitableConstraint1 = new CountByIntervalAwaitableConstraint(1, TimeSpan.FromMilliseconds(100), _MockTime);
     _CountByIntervalAwaitableConstraint2 = new CountByIntervalAwaitableConstraint(5, TimeSpan.FromSeconds(1), _MockTime);
 }
示例#3
0
 public RPCInterceptorState()
 {
     GeneralConstraintMulti  = new CountByIntervalAwaitableConstraint(2, TimeSpan.FromSeconds(1));
     GeneralConstraintSingle = new CountByIntervalAwaitableConstraint(1, TimeSpan.FromMilliseconds(400));
     TimeConstraint          = TimeLimiter.Compose(GeneralConstraintSingle, GeneralConstraintMulti);
     TimeConstraintLogs      = TimeLimiter.Compose(new CountByIntervalAwaitableConstraint(1, TimeSpan.FromSeconds(2)));
 }
示例#4
0
        static GetMarketDataTask()
        {
            CountByIntervalAwaitableConstraint constraint = new CountByIntervalAwaitableConstraint(3, TimeSpan.FromSeconds(5));


            CountByIntervalAwaitableConstraint constraint2 = new CountByIntervalAwaitableConstraint(1, TimeSpan.FromMilliseconds(1500));

            TimeConstraint = TimeLimiter.Compose(constraint, constraint2);
        }
示例#5
0
        public ToolsTask() : base("Find Nodes By Wallet")
        {
            CountByIntervalAwaitableConstraint constraint = new CountByIntervalAwaitableConstraint(4, TimeSpan.FromSeconds(1));


            CountByIntervalAwaitableConstraint constraint2 = new CountByIntervalAwaitableConstraint(1, TimeSpan.FromMilliseconds(200));

            TimeConstraint = TimeLimiter.Compose(constraint, constraint2);
        }
示例#6
0
        public async Task TestOneThread()
        {
            var constraint     = new CountByIntervalAwaitableConstraint(5, TimeSpan.FromSeconds(1));
            var constraint2    = new CountByIntervalAwaitableConstraint(1, TimeSpan.FromMilliseconds(100));
            var timeConstraint = TimeLimiter.Compose(constraint, constraint2);

            for (var i = 0; i < 1000; i++)
            {
                await timeConstraint.Enqueue(() => ConsoleIt());
            }
        }
示例#7
0
        public Server()
        {
            Limiter = new IpRateLimiter(ip =>
            {
                var constraint = new CountByIntervalAwaitableConstraint(10, TimeSpan.FromSeconds(1));
                var constraint2 = new CountByIntervalAwaitableConstraint(35, TimeSpan.FromSeconds(10));

                // Compose the two constraints
                return TimeLimiter.Compose(constraint, constraint2);
            });
        }
示例#8
0
        /// <summary>
        /// Increases the connection rate limit, after wating for the ip rate limit
        /// </summary>
        private void IncreaseRateLimit()
        {
            Task.Run(async() =>
            {
                await IpRateLimiter.Instance.WaitUntilAllowed(this.Context.Headers["X-Real-Ip"]);

                var constraint2 = new CountByIntervalAwaitableConstraint(10, TimeSpan.FromSeconds(2));
                var heavyUsage  = new CountByIntervalAwaitableConstraint(40, TimeSpan.FromSeconds(20));

                limiter = TimeLimiter.Compose(constraint2, heavyUsage);
            }).ConfigureAwait(false);
        }
示例#9
0
        private static Func <string, TimeLimiter> DefaultLimiter()
        {
            return((id) =>
            {
                var constraint = new CountByIntervalAwaitableConstraint(1, TimeSpan.FromSeconds(1));
                var constraint2 = new CountByIntervalAwaitableConstraint(5, TimeSpan.FromSeconds(10));
                var heavyUsage = new CountByIntervalAwaitableConstraint(10, TimeSpan.FromMinutes(1));
                var abuse = new CountByIntervalAwaitableConstraint(50, TimeSpan.FromMinutes(20));

                // Compose the two constraints
                return TimeLimiter.Compose(constraint, constraint2, heavyUsage, abuse);
            });
        }
示例#10
0
        public void AddRateLimit(int amount, TimeSpan time)
        {
            if (amount < 0)
            {
                throw new ArgumentException("You must supply a valid amount", nameof(amount));
            }

            if (time == null)
            {
                throw new ArgumentException("You must supply a valid time", nameof(time));
            }

            var constraint = new CountByIntervalAwaitableConstraint(amount, time);

            this.timeLimiters.Add(constraint);
        }
        private void SetThrottler()
        {
            if (this.targetThroughput > 0)
            {
                // 25% extra for overhead - maybe, need a more scientific way to calc, 0% for now
                double overheadCompensator = this.dryRun ? 1.0 : 1.0;

                double controlInterval = 1.0; // seconds

                // how many invokations per second are allowed
                double invocationsPerControlInterval = (double)(this.targetThroughput * overheadCompensator) / (double)this.batchSize * controlInterval;

                double clampBelowInterval = controlInterval / invocationsPerControlInterval * 1000.0;

                // renormalise to avoid fractional invocations
                if (invocationsPerControlInterval < 100)
                {
                    double normaliser = 100.0 / invocationsPerControlInterval;
                    invocationsPerControlInterval = normaliser * invocationsPerControlInterval;
                    controlInterval = normaliser * controlInterval;
                }

                // does not make sence to clamp from below at < 15ms - timers aren't that precise
                if (clampBelowInterval < 15.0)
                {
                    this.throttler = TimeLimiter.GetFromMaxCountByInterval(Convert.ToInt32(invocationsPerControlInterval), TimeSpan.FromSeconds(controlInterval));
                }
                else
                {
                    var clampAbove = new CountByIntervalAwaitableConstraint(Convert.ToInt32(invocationsPerControlInterval), TimeSpan.FromSeconds(controlInterval));
                    // Clamp from below: e.g. one invocation every 100 ms
                    var clampBelow = new CountByIntervalAwaitableConstraint(1, TimeSpan.FromMilliseconds(clampBelowInterval));
                    //Compose the two constraints
                    this.throttler = TimeLimiter.Compose(clampAbove, clampBelow);
                }
            }
            else // no throttling
            {
                this.throttler = TimeLimiter.GetFromMaxCountByInterval(Int32.MaxValue, TimeSpan.FromSeconds(1));
            }
        }
示例#12
0
        public async Task Test100Thread()
        {
            var constraint     = new CountByIntervalAwaitableConstraint(5, TimeSpan.FromSeconds(1));
            var constraint2    = new CountByIntervalAwaitableConstraint(1, TimeSpan.FromMilliseconds(100));
            var timeConstraint = TimeLimiter.Compose(constraint, constraint2);

            var tasks = new List <Task>();

            for (int i = 0; i < 100; i++)
            {
                tasks.Add(Task.Run(async() =>
                {
                    for (int j = 0; j < 10; j++)
                    {
                        await timeConstraint.Enqueue(() => ConsoleIt());
                    }
                }));
            }

            await Task.WhenAll(tasks.ToArray());
        }