예제 #1
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)));
 }
예제 #2
0
        static GetMarketDataTask()
        {
            CountByIntervalAwaitableConstraint constraint = new CountByIntervalAwaitableConstraint(3, TimeSpan.FromSeconds(5));


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

            TimeConstraint = TimeLimiter.Compose(constraint, constraint2);
        }
예제 #3
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);
        }
예제 #4
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());
            }
        }
예제 #5
0
        public async Task Compose_composes_the_contraints()
        {
            var constraints = Enumerable.Range(0, 3).Select(_ => GetSubstituteAwaitableConstraint()).ToArray();
            var composed    = TimeLimiter.Compose(constraints);

            await composed.Enqueue(() => { });

            await Task.WhenAll(
                constraints.Select(c => c.Received().WaitForReadiness(Arg.Any <CancellationToken>())).ToArray()
                );
        }
예제 #6
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);
            });
        }
예제 #7
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);
        }
예제 #8
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);
            });
        }
예제 #9
0
        private async Task OnBeforeRequestAsync(object sender, EventArgs e)
        {
            if (sender == null || e == null)
            {
                return;
            }

            var clientRequest = e as ClientRequestEventArgs;

            if (string.IsNullOrEmpty(clientRequest.ResponseContent)) // la respuesta no está cacheada
            {
                await TimeLimiter.Compose(this.timeLimiters.ToArray());
            }
        }
        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));
            }
        }
예제 #11
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());
        }