Beispiel #1
0
        public async Task Invoke(HttpContext context)
        {
            IRateLimit rateLimit = client.GetGrain <IRateLimit>(context.Request.Host.Host);
            var        para      = new RateLimitParameter {
                Rate = rate, BucketSize = bucketSize
            };
            double left = await rateLimit.IsNeedToLimit(para);

            if (left > 0)
            { // 水桶还没满,继续加1
                await _next(context);
            }
            else
            {
                ///如果超出30,进入下一个中间件,下一个中间件是用来加入黑名单的
                if (left <= -30)
                {
                    //Header中加入一个标志
                    context.Items.Add("BAN", "1");
                    await _next(context);
                }
                else
                {
                    context.Response.StatusCode = 429;
                }
            }
        }
Beispiel #2
0
 public Task <double> IsNeedToLimit(RateLimitParameter para)
 {
     rate       = para.Rate;
     bucketSize = para.BucketSize;
     refreshWater();
     if (water < bucketSize)
     { // 水桶还没满,继续加1
         water++;
     }
     total++;
     Console.WriteLine("work here");
     return(Task.FromResult(bucketSize - total));
 }