public void Should_reject_with_throttled_result_when_rejection_probability_allows()
        {
            options = new AdaptiveThrottlingOptions(Guid.NewGuid().ToString(), 1, minimumRequests, criticalRatio, 1.0);
            module  = new AdaptiveThrottlingModule(options);

            Accept(1);

            while (module.RejectionProbability < 0.999)
            {
                Reject(1);
            }

            for (var i = 0; i < 100; i++)
            {
                var requestsBefore = module.Requests;
                var acceptsBefore  = module.Accepts;

                var result = Execute(acceptedResult);
                if (result.Status == ClusterResultStatus.Throttled)
                {
                    module.Requests.Should().Be(requestsBefore + 1);
                    module.Accepts.Should().Be(acceptsBefore);
                    return;
                }
            }

            throw new AssertionFailedException("No requests were rejected in 100 attempts, which was highly expected.");
        }
        public void Setup()
        {
            replica        = new Uri("http://replica");
            request        = Request.Get("foo/bar");
            acceptedResult = new ClusterResult(ClusterResultStatus.Success, new[] { new ReplicaResult(replica, new Response(ResponseCode.Accepted), ResponseVerdict.Accept, TimeSpan.Zero) }, null, request);
            rejectedResult = new ClusterResult(ClusterResultStatus.ReplicasExhausted, new[] { new ReplicaResult(replica, new Response(ResponseCode.TooManyRequests), ResponseVerdict.Reject, TimeSpan.Zero) }, null, request);

            context = Substitute.For <IRequestContext>();
            context.Log.Returns(new SilentLog());

            options = new AdaptiveThrottlingOptions(Guid.NewGuid().ToString(), 1, minimumRequests);
            module  = new AdaptiveThrottlingModule(options);
        }
Пример #3
0
        /// <summary>
        /// Sets up an adaptive client throttling mechanism with given parameters.
        /// </summary>
        /// <param name="configuration">A configuration to be modified.</param>
        /// <param name="storageKey">See <see cref="AdaptiveThrottlingOptions.StorageKey"/>.</param>
        /// <param name="minutesToTrack">See <see cref="AdaptiveThrottlingOptions.MinutesToTrack"/>.</param>
        /// <param name="minimumRequests">See <see cref="AdaptiveThrottlingOptions.MinimumRequests"/>.</param>
        /// <param name="criticalRatio">See <see cref="AdaptiveThrottlingOptions.CriticalRatio"/>.</param>
        /// <param name="maximumRejectProbability">See <see cref="AdaptiveThrottlingOptions.MaximumRejectProbability"/>.</param>
        public static void SetupAdaptiveThrottling(
            this IClusterClientConfiguration configuration,
            string storageKey,
            int minutesToTrack              = ClusterClientDefaults.AdaptiveThrottlingMinutesToTrack,
            int minimumRequests             = ClusterClientDefaults.AdaptiveThrottlingMinimumRequests,
            double criticalRatio            = ClusterClientDefaults.AdaptiveThrottlingCriticalRatio,
            double maximumRejectProbability = ClusterClientDefaults.AdaptiveThrottlingRejectProbabilityCap)
        {
            var options = new AdaptiveThrottlingOptions(
                storageKey,
                minutesToTrack,
                minimumRequests,
                criticalRatio,
                maximumRejectProbability);

            configuration.AddRequestModule(new AdaptiveThrottlingModule(options), typeof(AbsoluteUrlSenderModule));
        }