Exemple #1
0
        private static async Task DoLeakyBucketWait(AlgorithmCheckResult checkResult)
        {
            // Simulation leaky bucket algorithm queuing mechanism
            var wait = checkResult.RuleCheckResults.Max(d => d.Wait);

            if (wait > 0)
            {
                await Task.Delay((int)wait).ConfigureAwait(false);
            }
        }
Exemple #2
0
        /// <summary>
        /// setting response header
        /// </summary>
        /// <param name="context"></param>
        /// <param name="checkResult"></param>
        /// <returns></returns>
        private async Task SetHeaders(HttpContext context, AlgorithmCheckResult checkResult)
        {
            var headers = await BuildHttpHeaders(context, checkResult).ConfigureAwait(false);

            if (headers != null && headers.Count > 0)
            {
                foreach (var h in headers)
                {
                    context.Response.Headers.Append(h.Key, h.Value);
                }
            }
        }
Exemple #3
0
        /// <summary>
        /// rateLimit exception
        /// </summary>
        /// <returns></returns>
        private static AsyncPolicy <AlgorithmCheckResult> PollyRateLimitAdvancedCircuitBreakerAsync(string policyKey, EnableRateLimitAttribute enableRateLimitAttribute)
        {
            var getResult = _getPollyObj.TryGetValue(policyKey, out AsyncPolicy <AlgorithmCheckResult> pollyObj);

            if (getResult)
            {
                return(pollyObj);
            }
            int      retryCount       = enableRateLimitAttribute == null ? 1 : enableRateLimitAttribute.RetryCount;
            double   failureThreshold = enableRateLimitAttribute == null ? 0.75 : enableRateLimitAttribute.FailureThreshold;
            TimeSpan samplingDuration = enableRateLimitAttribute == null?TimeSpan.FromSeconds(10) : enableRateLimitAttribute.SamplingDuration;

            int      minimumThroughput = enableRateLimitAttribute == null ? 100 : enableRateLimitAttribute.MinimumThroughput;
            TimeSpan durationOfBreak   = enableRateLimitAttribute == null?TimeSpan.FromSeconds(10) : enableRateLimitAttribute.DurationOfBreak;

            var breakPolicy = Policy <AlgorithmCheckResult> .Handle <RateLimitException>().AdvancedCircuitBreakerAsync(
                failureThreshold: failureThreshold,
                samplingDuration: samplingDuration,
                minimumThroughput: minimumThroughput,
                durationOfBreak: durationOfBreak,
                onBreak: (r, t) =>
            {
                Console.WriteLine("onbreak");
            },
                onReset: () =>
            {
                Console.WriteLine("onReset");
            },
                onHalfOpen: () =>
            {
                Console.WriteLine("onHalfOpen");
            }
                );

            var retry = Policy <AlgorithmCheckResult> .Handle <RateLimitException>().WaitAndRetryAsync(retryCount, i => TimeSpan.FromMilliseconds(100 * i));

            var message = new AlgorithmCheckResult(new List <RuleCheckResult>()
            {
                new RuleCheckResult()
                {
                    IsLimit = true
                }
            })
            {
            };
            var fallback = Policy <AlgorithmCheckResult> .Handle <BrokenCircuitException>().FallbackAsync(message);

            var fallbackBreak = Policy.WrapAsync(fallback, retry, breakPolicy).WithPolicyKey(policyKey);

            _getPollyObj.TryAdd(policyKey, fallbackBreak);
            return(fallbackBreak);
        }
Exemple #4
0
 private async Task DoOnAfterUntriggeredDoNext(HttpContext context, AlgorithmCheckResult checkResult)
 {
     if (_interceptor != null)
     {
         if (_interceptor.OnAfterUntriggeredDoNextAsync != null)
         {
             await _interceptor.OnAfterUntriggeredDoNextAsync(context, checkResult).ConfigureAwait(false);
         }
         else if (_interceptor.OnAfterUntriggeredDoNext != null)
         {
             _interceptor.OnAfterUntriggeredDoNext(context, checkResult);
         }
     }
 }
Exemple #5
0
        private async Task <string> BuildHttpContent(HttpContext context, AlgorithmCheckResult checkResult)
        {
            string content = null;

            if (_error.BuildHttpContentAsync != null)
            {
                content = await _error.BuildHttpContentAsync(context, checkResult).ConfigureAwait(false);
            }
            else if (_error.BuildHttpContent != null)
            {
                content = _error.BuildHttpContent(context, checkResult);
            }

            return(content);
        }
Exemple #6
0
        /// <summary>
        /// setting rateLimit response content
        /// </summary>
        /// <param name="context"></param>
        /// <param name="checkResult"></param>
        /// <returns></returns>
        private async Task ReponseWithTooManyRequests(HttpContext context, AlgorithmCheckResult checkResult)
        {
            string content = await BuildHttpContent(context, checkResult).ConfigureAwait(false);

            if (!string.IsNullOrWhiteSpace(content))
            {
                var bodyContent = Encoding.UTF8.GetBytes(content);
                context.Response.ContentType = "application/json;charset=utf-8";
                await context.Response.Body.WriteAsync(bodyContent, 0, bodyContent.Length).ConfigureAwait(false);
            }
            else
            {
                await context.Response.WriteAsync(string.Empty).ConfigureAwait(false);
            }
        }
Exemple #7
0
        private async Task <Dictionary <string, StringValues> > BuildHttpHeaders(HttpContext context, AlgorithmCheckResult checkResult)
        {
            Dictionary <string, StringValues> headers = null;

            if (_error.BuildHttpHeadersAsync != null)
            {
                headers = await _error.BuildHttpHeadersAsync(context, checkResult).ConfigureAwait(false);
            }
            else if (_error.BuildHttpHeaders != null)
            {
                headers = _error.BuildHttpHeaders(context, checkResult);
            }

            return(headers);
        }