/// <summary>
 /// 创建漏桶算法的限流服务
 /// </summary>
 /// <param name="maxQPS"></param>
 /// <param name="limitSize"></param>
 public LeakyBucketLimitingService(double maxQPS, int limitSize)
 {
     LimitSize         = limitSize;
     MaxQPS            = maxQPS;
     limitedQueue      = new LimitedQueue <RequestObject>(limitSize);
     cancellationToken = new CancellationTokenSource();
     task = Task.Factory.StartNew(TokenProcess, cancellationToken.Token);
 }
Exemplo n.º 2
0
        private readonly object lockObject = new object();//用于加锁
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="maxQPS">最大QPS</param>
        /// <param name="limitSize">最大限制同时并发数</param>
        public TokenBucketLimitingService(double maxQPS, int limitSize)
        {
            MaxQPS    = maxQPS;
            LimitSize = limitSize;

            limitedQueue = new LimitedQueue <object>(limitSize);
            for (int i = 0; i < limitSize; i++)//先放满令牌
            {
                limitedQueue.Enqueue(new object());
            }
            cancellationToken = new CancellationTokenSource();
            task = Task.Factory.StartNew(new Action(TokenProcess), cancellationToken.Token);
        }