RateLimiter的 SmoothBursty(非warmup预热)及SmoothWarmingUp(预热,冷启动)

时间:2022-07-26 16:47:36

SmoothBursty

主要思想

记录 1秒内的微秒数/permitsPerSencond = 时间间隔interval,每一个interval可获得一个令牌
根据允许使用多少秒内的令牌参数,计算出maxPermits
setRate时初始化下次interval时间,及storedPermits

acquire时,计算当前nowMicros,如果大于下次interval时间时间,则更新storedPermits和下次interval时间,计算storedPermits能否满足此次acquire,如果能,则需要等待的时间为0,如果不能,则计算还需要多少微秒等待,并在非同步块外执行sleep操作

如果其他线程已经刷新了nextFreeTicketMicros,会如下情况acquire是无timeout的

Thread 1: acquire 11 -> storedPermits不能满足要求 -> waitTime = (acquire - stored) * stableIntervalMicros -> nextFreeTicketMicros += waitMicros ----->  out lock sleep
Thread 2: acquire 2 -> nowMicros < nextFreeTicketMicros , stored = 0,被线程1消耗完了 -> freshPermits = requiredPermits - storedPermitsToSpend 即 = requiredPermits -> waitTime = freshPermits * stableIntervalMicros
-> nextFreeTicketMicros += waitTime,此时的nextFreeTicketMicros包含了Thread1需要等待的时间 -------> out lock sleep a longer time

tryAquire(num,timeout)逻辑

timeoutMicros = timeout.toMicros
lock()
nowMicros = ...
canAcquire = nextFreeTicketMicros <= nowMicros + timeoutMicros
if(!canAcquire){
 return false;
}
else{
  microsToWait = ...
} 
unlock()
sleep(microsToWait)
return true;

SmoothWarmingUp

主要思想和SmoothBursty相似,由于带预热过程,刚开始由于availablePermitsAboveThreshold>0.0,速率会较慢,如果持续获取令牌,则会使availablePermitsAboveThreshold=0,速率变快

  • 从0->thresholdPermits,生成一个令牌的时间:stableIntervalMicros
  • 从thresholdPermits-> maxPermits ,生成一个令牌的时间:stableIntervalMicros + permits * slope;

    @Override
    final long reserveEarliestAvailable(int requiredPermits, long nowMicros) {
    resync(nowMicros);
    long returnValue = nextFreeTicketMicros;
    //当前需要且尽最大可能消费的
    double storedPermitsToSpend = min(requiredPermits, this.storedPermits);
    //新鲜permits个数,这些个数是一定会产生等待的,除了0
    double freshPermits = requiredPermits - storedPermitsToSpend;
    //计算需要wait的总时间
    long waitMicros =
    //非busty类型的storedPermitsToWaitTime直接返回0
    storedPermitsToWaitTime(this.storedPermits, storedPermitsToSpend)
    + (long) (freshPermits * stableIntervalMicros);
    //下次有票时间
    this.nextFreeTicketMicros = LongMath.saturatedAdd(nextFreeTicketMicros, waitMicros);
    this.storedPermits -= storedPermitsToSpend;
    return returnValue;
    }

     //已知permitsToTake <= storedPermits
     @Override
     long storedPermitsToWaitTime(double storedPermits, double permitsToTake) {
       //减去预热需要保留的permits,剩下的可消耗的数量
       double availablePermitsAboveThreshold = storedPermits - thresholdPermits;
       long micros = 0;
       // measuring the integral on the right part of the function (the climbing line)
       //如果有剩余可用的令牌
       if (availablePermitsAboveThreshold > 0.0) {
        //剩余可用的和需要获取的个数取小值
         double permitsAboveThresholdToTake = min(availablePermitsAboveThreshold, permitsToTake);
         // TODO(cpovirk): Figure out a good name for this variable.
         //用可消耗的数量 + (可消耗的数量 - 实际消耗的数量)permitsToTime
         //在预热阶段从thresholdPermits到maxPermits的耗时并非是stableIntervalMicros * n
         //会耗费更多的时间,其计算规则不同,所以才需要把permitsAboveThresholdToTake从permitsToTake减去
         //length 可能作为一个经验值,相当于补充permitsAboveThresholdToTake个令牌需要的平均时间值*2
                     //剩余可用的-实际需要且最大能消耗的令牌,得到最终剩余的令牌个数,可能是0
         double length = permitsToTime(availablePermitsAboveThreshold)
             + permitsToTime(availablePermitsAboveThreshold - permitsAboveThresholdToTake);
         //这里确实不好理解,从语义环境来说,它是从 thresholdPermits 到 maxPertmis 过程中
         //生成 permitsAboveThresholdToTake 个令牌需要耗费的时间
         //并且带coldFactor的构造函数不是public,SmoothWarmingUp也是private-package的
         micros = (long) (permitsAboveThresholdToTake * length / 2.0);
         //从permitsToTake中减去保留预热需留下个数后最终消耗的个数,这部分个数由于是提前存在的、富余的
         //因此不需要计算到wait时间
         permitsToTake -= permitsAboveThresholdToTake;
       }
       // measuring the integral on the left part of the function (the horizontal line)
       //如果没有剩余可用令牌,走的是stableIntervalMicros * n
       micros += (stableIntervalMicros * permitsToTake);
       return micros;
     }   

length/2可以理解为下图
RateLimiter的 SmoothBursty(非warmup预热)及SmoothWarmingUp(预热,冷启动)

    //permits值越小,需要的时间就越少,值越大,需要的时间就越大
    private double permitsToTime(double permits) {
      //double coldIntervalMicros = stableIntervalMicros * coldFactor;
      // thresholdPermits = 0.5 * warmupPeriodMicros / stableIntervalMicros;
      //maxPermits =
      thresholdPermits + 2.0 * warmupPeriodMicros / (stableIntervalMicros + coldIntervalMicros);
      //slope带比率的时间,可以理解为增长因子
      //slope =  (coldIntervalMicros - stableIntervalMicros) / (maxPermits - thresholdPermits)
      //return表示成这样更易于理解 stableIntervalMicros + (coldIntervalMicros - stableIntervalMicros) * (permits/(maxPermits - thresholdPermits))
      return stableIntervalMicros + permits * slope;
    }