Skip to content

Limiter does not work #35

Open
Open

Description

@bitliner

My code is as following:

constructor:()=>{
  this.RateLimiterOnSeconds = new RateLimiter(4, 1*1000); // 5 requests per second
  this.RateLimiterOnHours = new RateLimiter(2000, 1*1000*60*60); // 2000 request per hour
}
sendReq1: ()=>{
  schedule(..)
}
sendReq2: ()=>{
  schedule(...)
}
schedule: (fn)=>{
  this.RateLimiterOnSeconds.removeTokens(1,(err, remainingRequests)=>{
    this.RateLimiterOnHours.removeTokens(1, () => {
      console.log('request executed in second...',new Date().getTime()/1000, remainingRequests);
      fn();
    });
  });
}

The limit is exceeded.

Indeed, the console.log in the schedule() method prints:

request executed in second... 1487770295.951 4
request executed in second... 1487770296.043 4
request executed in second... 1487770296.219 4
request executed in second... 1487770296.258 4
request executed in second... 1487770296.427 4
request executed in second... 1487770296.56 4
request executed in second... 1487770296.628 4
request executed in second... 1487770296.803 4
request executed in second... 1487770296.831 4
request executed in second... 1487770296.921 4
request executed in second... 1487770297.126 4
request executed in second... 1487770297.214 4
request executed in second... 1487770297.311 4
request executed in second... 1487770297.421 4
request executed in second... 1487770297.605 4
request executed in second... 1487770297.686 4
request executed in second... 1487770297.803 4

and as you can see, in the second 1487770297 there are 7 requests (instead of 5), and number of tokens to remove is constant.

Activity

  1. hcientist commented on Mar 20, 2017

    @hcientist

    @bitliner (i know this is not your primary concern, but I also want to use this package and am trying to troubleshoot) why does your code say

    this.RateLimiterOnSeconds = new RateLimiter(4, 1*1000); // 5 requests per second

    wouldn't this be 4 per second?

    also, your comment points to second 1487770297, but doesn't second 1487770296 already demonstrate too many requests per second?

    also, I cannot replicate your issue with static remainingRequests.

    here is my test:

    'use strict'
    
    var RateLimiter = require('limiter').RateLimiter
    
    var rateLimiterOnSeconds = new RateLimiter(4, 1*1000)
    var rateLimiterOnHours = new RateLimiter(2000, 1*1000*60*60)
    
    var queries = []
    var queryCount = 100
    for (var i=0; i<queryCount; i++) {
      queries.push(i)
    }
    
    function testIt (queryIndex) {
      var p = new Promise((resolve, reject) => {
        rateLimiterOnSeconds.removeTokens(1,(err, remainingRequests)=>{
          if (err) {
            console.log('error!', err);
            reject(err)
          } else {
            rateLimiterOnHours.removeTokens(1, (err2, rem2) => {
              if (err2) {
                console.log('error2!', err2);
                reject(err)
              } else {
                resolve(queryIndex)
                console.log(queryIndex, new Date().getTime()/1000, remainingRequests, rem2)
              }
            })
          }
        })
      })
      return p;
    }
    
    Promise.all(queries.map(query => {
      return testIt(query);
    }))
      .then(resultsObjList => {
        console.log('done', resultsObjList)
      })
      .catch(err => {
        console.log(err)
      })
    

    for which i get the output:

    0 1490051070.527 3 1999
    1 1490051070.529 2 1998
    2 1490051070.53 1 1997
    3 1490051070.53 0.0040000000000000036 1996
    4 1490051070.766 0.008000000000000007 1995.1394444444445
    5 1490051071.013 0.0040000000000000036 1994.2766666666666
    6 1490051071.267 0.020000000000000018 1993.4177777777777
    7 1490051071.517 0.008000000000000007 1992.5566666666666
    8 1490051071.767 0.020000000000000018 1991.6955555555555
    9 1490051072.017 0.020000000000000018 1990.8344444444444
    10 1490051072.265 0.008000000000000007 1989.9722222222222
    11 1490051072.512 0 1989.1094444444443
    12 1490051072.763 0.0040000000000000036 1988.2488888888888
    13 1490051073.016 0.01200000000000001 1987.3894444444443
    14 1490051073.265 0.01200000000000001 1986.5277777777776
    15 1490051073.516 0.016000000000000014 1985.667222222222
    99 1490051073.764 0.008000000000000007 1984.8049999999998
    42 1490051074.013 0.0040000000000000036 1983.9433333333332
    43 1490051074.267 0.016000000000000014 1983.0844444444442
    44 1490051074.515 0.008000000000000007 1982.222222222222
    76 1490051074.764 0.0040000000000000036 1981.3605555555553
    78 1490051075.016 0.016000000000000014 1980.5005555555554
    79 1490051075.264 0.0040000000000000036 1979.638333333333
    80 1490051075.517 0.016000000000000014 1978.7788888888886
    82 1490051075.764 0.008000000000000007 1977.9161111111107
    83 1490051076.017 0.020000000000000018 1977.0566666666662
    16 1490051076.267 0.016000000000000014 1976.195555555555
    94 1490051076.521 0 1975.3366666666661
    95 1490051076.765 0.01200000000000001 1974.4722222222217
    96 1490051077.015 0.008000000000000007 1973.6111111111106
    97 1490051077.265 0.008000000000000007 1972.7499999999995
    55 1490051077.517 0.016000000000000014 1971.8899999999996
    56 1490051077.765 0.01200000000000001 1971.0277777777774
    57 1490051078.013 0 1970.165555555555
    92 1490051078.267 0.020000000000000018 1969.3066666666662
    93 1490051078.516 0.016000000000000014 1968.4449999999995
    63 1490051078.765 0.01200000000000001 1967.5833333333328
    62 1490051079.014 0.0040000000000000036 1966.7216666666661
    37 1490051079.267 0.020000000000000018 1965.8622222222216
    38 1490051079.517 0.020000000000000018 1965.0011111111105
    39 1490051079.763 0 1964.1377777777773
    20 1490051080.015 0.01200000000000001 1963.2777777777774
    40 1490051080.263 0.0040000000000000036 1962.415555555555
    41 1490051080.515 0.01200000000000001 1961.5555555555552
    84 1490051080.765 0.008000000000000007 1960.6944444444441
    68 1490051081.014 0.008000000000000007 1959.8327777777774
    69 1490051081.266 0.016000000000000014 1958.9727777777775
    70 1490051081.514 0.008000000000000007 1958.1105555555553
    71 1490051081.765 0.01200000000000001 1957.2499999999998
    72 1490051082.014 0.0040000000000000036 1956.388333333333
    87 1490051082.265 0.01200000000000001 1955.5277777777776
    88 1490051082.514 0.008000000000000007 1954.666111111111
    89 1490051082.765 0.01200000000000001 1953.8055555555554
    90 1490051083.014 0.0040000000000000036 1952.9433333333332
    91 1490051083.267 0.020000000000000018 1952.0844444444442
    98 1490051083.517 0.020000000000000018 1951.2233333333331
    45 1490051083.766 0.016000000000000014 1950.3616666666665
    46 1490051084.015 0.01200000000000001 1949.4999999999998
    47 1490051084.266 0.016000000000000014 1948.6394444444443
    48 1490051084.517 0.020000000000000018 1947.7788888888888
    50 1490051084.762 0 1946.915
    51 1490051085.012 0 1946.0538888888889
    49 1490051085.263 0.0040000000000000036 1945.1933333333334
    21 1490051085.515 0.008000000000000007 1944.3333333333335
    33 1490051085.766 0.01200000000000001 1943.472777777778
    34 1490051086.013 0.0040000000000000036 1942.6100000000001
    35 1490051086.263 0.0040000000000000036 1941.748888888889
    36 1490051086.517 0.020000000000000018 1940.89
    85 1490051086.762 0 1940.0261111111113
    86 1490051087.017 0.020000000000000018 1939.167777777778
    52 1490051087.267 0.016000000000000014 1938.3066666666668
    53 1490051087.516 0.016000000000000014 1937.4450000000002
    54 1490051087.767 0.020000000000000018 1936.5844444444447
    58 1490051088.016 0.016000000000000014 1935.722777777778
    59 1490051088.266 0.016000000000000014 1934.861666666667
    60 1490051088.515 0.01200000000000001 1934.0000000000002
    61 1490051088.762 0 1933.1372222222224
    64 1490051089.016 0.016000000000000014 1932.2783333333334
    65 1490051089.264 0.008000000000000007 1931.4161111111111
    66 1490051089.516 0.016000000000000014 1930.5561111111112
    67 1490051089.763 0.0040000000000000036 1929.6933333333334
    73 1490051090.016 0.016000000000000014 1928.8338888888889
    74 1490051090.267 0.020000000000000018 1927.9733333333334
    75 1490051090.517 0.020000000000000018 1927.1122222222223
    77 1490051090.763 0.0040000000000000036 1926.248888888889
    81 1490051091.015 0.01200000000000001 1925.3888888888891
    17 1490051091.266 0.016000000000000014 1924.5283333333336
    18 1490051091.517 0.020000000000000018 1923.6677777777782
    19 1490051091.765 0.008000000000000007 1922.8055555555559
    24 1490051092.012 0 1921.942777777778
    22 1490051092.262 0 1921.081666666667
    25 1490051092.515 0.01200000000000001 1920.2222222222224
    26 1490051092.766 0.016000000000000014 1919.361666666667
    27 1490051093.016 0.01200000000000001 1918.5005555555558
    29 1490051093.264 0.008000000000000007 1917.6383333333335
    30 1490051093.516 0.016000000000000014 1916.7783333333336
    31 1490051093.767 0.020000000000000018 1915.9177777777782
    32 1490051094.013 0.0040000000000000036 1915.054444444445
    23 1490051094.267 0.020000000000000018 1914.195555555556
    28 1490051094.516 0.016000000000000014 1913.3338888888893
    done [ 0,
      1,
      2,
      3,
      4,
      5,
      6,
      7,
      8,
      9,
      10,
      11,
      12,
      13,
      14,
      15,
      16,
      17,
      18,
      19,
      20,
      21,
      22,
      23,
      24,
      25,
      26,
      27,
      28,
      29,
      30,
      31,
      32,
      33,
      34,
      35,
      36,
      37,
      38,
      39,
      40,
      41,
      42,
      43,
      44,
      45,
      46,
      47,
      48,
      49,
      50,
      51,
      52,
      53,
      54,
      55,
      56,
      57,
      58,
      59,
      60,
      61,
      62,
      63,
      64,
      65,
      66,
      67,
      68,
      69,
      70,
      71,
      72,
      73,
      74,
      75,
      76,
      77,
      78,
      79,
      80,
      81,
      82,
      83,
      84,
      85,
      86,
      87,
      88,
      89,
      90,
      91,
      92,
      93,
      94,
      95,
      96,
      97,
      98,
      99 ]
    
  2. hcientist commented on Mar 21, 2017

    @hcientist

    I am trying to limit a call to an external API to 5 queries per second and 200 queries per minute. My code is like below:

    let perSecondLimiter = new RateLimiter(5, 1001)
    let perMinLimiter = new RateLimiter(200, 60*1000+1)
    
    function search (query) {
      let p = new Promise(function (resolve, reject) {
        perSecondLimiter.removeTokens(1, (psErr, remainingPS) => {
          if (psErr) {
            reject(psErr)
          } else {
            perMinLimiter.removeTokens(1, (pmErr, remainingPM) => {
              if (pmErr) {
                reject(pmErr)
              } else {
                console.log(query, new Date().getTime()/1000, remainingPM, remainingPS)
                var requestOptions = {
                  jar: true
                }
                requestOptions.url = encodeURI(THIRD_PARTY_API_URL + query)
                request(requestOptions, myCallback(resolve, reject))
              }
            })
          }
        })
      })
      return p
    }
    

    here is some output of a test with 1000 queries (I have the test stop when it detects the service has cut us off due to exceeding rate limits):

    0 1490055774.498 199 4
    1 1490055774.505 198 3
    2 1490055774.506 197 2
    3 1490055774.506 196 1
    4 1490055774.506 195 0
    59 1490055774.689 194.6366560557324 0.00899100899100902
    245 1490055774.901 194.34331094481757 0.01798201798201804
    305 1490055775.088 193.96663388943517 0.0019980019980019303
    361 1490055775.29 193.63995600073332 0.0009990009990008542
    91 1490055775.493 193.31661138981016 0.009990009990009874
    315 1490055775.69 192.97326711221478 0.003996003996003861
    652 1490055775.891 192.64325594573424 0.0029970029970027845
    355 1490055776.092 192.3132447792537 0.006993006993006867
    670 1490055776.297 191.99323344610926 0.025974025974025983
    756 1490055776.489 191.63655605739908 0
    757 1490055776.691 191.30987816869722 0.00899100899100902
    397 1490055776.896 190.9898668355528 0.027972027972027913
    563 1490055777.091 190.64318928017872 0.0019980019980019303
    564 1490055777.29 190.30651155814076 0.0009990009990010762
    467 1490055777.496 189.9931667805537 0.02497502497502513
    931 1490055777.695 189.65648905851575 0.018981018981019115
    586 1490055777.892 189.31314478092037 0.007992007992008165
    587 1490055778.092 188.9798003366611 0.0019980019980021524
    88 1490055778.296 188.65978900351666 0.025974025974026205
    89 1490055778.491 188.30977817036387 2.220446049250313e-16
    367 1490055778.694 187.98310028166202 0.008991008991009242
    357 1490055778.897 187.66308894851758 0.022977022977023198
    232 1490055779.092 187.3130781153648 0.0019980019980021524
    233 1490055779.294 186.98640022666294 0.005994005994006235
    868 1490055779.496 186.65972233796109 0.014985014985015255
    165 1490055779.695 186.32304461592312 0.013986013986014179
    390 1490055779.898 185.99970000499997 0.022977022977023198
    909 1490055780.098 185.6663555607407 0.021978021978022122
    951 1490055780.294 185.31967800536663 0.0009990009990010762
    146 1490055780.498 184.9996666722222 0.01998001998002019
    20 1490055780.694 184.65298911684812 0.003996003996004305
    21 1490055780.899 184.33631106148238 0.02297702297702342
    398 1490055781.097 183.99630006166572 0.01198801198801247
    499 1490055781.298 183.66628889518518 0.015984015984016553
    622 1490055781.499 183.3329444509259 0.014985014985015477
    629 1490055781.7 183.00626656222406 0.023976023976024496
    109 1490055781.899 182.6695888401861 0.017982017982018483
    24 1490055782.099 182.33624439592683 0.01698301698301763
    689 1490055782.299 182.00289995166756 0.020979020979021712
    63 1490055782.504 181.68622189630182 0.004995004995005825
    415 1490055782.696 181.3262112298129 0.003996003996004749
    64 1490055782.902 181.01286645222586 0.022977022977023864
    81 1490055783.099 180.6695221746305 0.011988011988012914
    905 1490055783.302 180.34617756370733 0.025974025974027093
    731 1490055783.501 180.00949984166937 0.01998001998002108
    733 1490055783.697 179.6628222862953 0.003996003996005193
    30 1490055783.898 179.33281111981475 0.002997002997004339
    449 1490055784.1 179.0061332311129 0.011988011988013358
    603 1490055784.297 178.66278895351752 0.0009990009990024085
    604 1490055784.497 178.32944450925825 1.5543122344752192e-15
    605 1490055784.701 178.00943317611382 0.01398601398601551
    774 1490055784.902 177.67942200963327 0.02297702297702453
    144 1490055785.104 177.35274412093142 0.021978021978023454
    861 1490055785.303 177.01606639889346 0.0209790209790226
    131 1490055785.498 176.66605556574066 1.5543122344752192e-15
    944 1490055785.702 176.34604423259623 0.01898101898102067
    945 1490055785.903 176.01603306611568 0.022977022977024752
    666 1490055786.104 175.68602189963514 0.021978021978023676
    199 1490055786.302 175.34601089981848 0.015984015984017663
    200 1490055786.505 175.02266628889532 0.02997002997003162
    455 1490055786.702 174.67932201129994 0.008991008991010574
    80 1490055786.905 174.3559774003768 0.02297702297702453
    738 1490055787.103 174.0126331227814 0.01198801198801358
    992 1490055787.305 173.68928851185825 0.0209790209790226
    136 1490055787.5 173.33927767870546 1.5543122344752192e-15
    153 1490055787.705 173.02259962333972 0.01898101898102067
    593 1490055787.905 172.68925517908045 0.017982017982019594
    854 1490055788.103 172.3492441792638 0.006993006993008644
    145 1490055788.303 172.01589973500452 0.005994005994007567
    825 1490055788.505 171.68922184630267 0.014985014985016587
    767 1490055788.705 171.3525441242647 0.01398601398601551
    768 1490055788.905 171.02253295778416 0.012987012987014657
    876 1490055789.105 170.6891885135249 0.01198801198801358
    700 1490055789.305 170.35584406926563 0.010989010989012504
    525 1490055789.505 170.02249962500636 0.009990009990011428
    41 1490055789.704 169.6858219029684 0.003996003996005415
    974 1490055789.905 169.35581073648785 0.007992007992009498
    561 1490055790.106 169.0257995700073 0.01198801198801358
    882 1490055790.305 168.68912184796935 0.005994005994007567
    932 1490055790.505 168.35577740371008 0.004995004995006491
    174 1490055790.705 168.0224329594508 0.003996003996005415
    856 1490055790.908 167.69908834852765 0.017982017982019594
    246 1490055791.105 167.35574407093227 0.006993006993008644
    247 1490055791.308 167.03239946000912 0.015984015984017663
    248 1490055791.505 166.68905518241374 1.7763568394002505e-15
    548 1490055791.705 166.35571073815447 0.003996003996005859
    657 1490055791.91 166.03903268278873 0.022977022977024975
    252 1490055792.107 165.69568840519335 0.006993006993009088
    844 1490055792.306 165.3590106831554 0.0059940059940080115
    94 1490055792.507 165.02899951667484 0.004995004995007157
    95 1490055792.706 164.69232179463688 0.003996003996006303
    370 1490055792.908 164.36564390593503 0.007992007992010386
    836 1490055793.107 164.02563290611837 0.001998001998004373
    860 1490055793.311 163.70895485075263 0.02097902097902349
    837 1490055793.507 163.36227729537856 0.004995004995007601
    838 1490055793.711 163.04226596223413 0.023976023976026495
    827 1490055793.911 162.70892151797486 0.02297702297702542
    828 1490055794.112 162.3789103514943 0.021978021978024342
    673 1490055794.308 162.03223279612024 0.0009990009990032966
    490 1490055794.511 161.70888818519708 0.014985014985017253
    461 1490055794.712 161.37887701871654 0.018981018981021336
    450 1490055794.908 161.03219946334247 0.002997002997005449
    777 1490055795.109 160.70218829686192 0.001998001998004373
    271 1490055795.31 160.37217713038137 0.010989010989013392
    762 1490055795.51 160.0388326861221 0.004995004995007379
    540 1490055795.712 159.71215479742025 0.013986013986016399
    19 1490055795.912 159.378810353161 0.017982017982020482
    147 1490055796.11 159.03879935334433 0.001998001998004595
    222 1490055796.31 158.70545490908506 0.0009990009990037407
    643 1490055796.509 158.3687771870471 2.6645352591003757e-15
    690 1490055796.713 158.04876585390267 0.013986013986016843
    84 1490055796.914 157.71875468742212 0.022977022977025863
    430 1490055797.113 157.38207696538416 0.011988011988014913
    55 1490055797.311 157.0420659655675 0.0009990009990039628
    477 1490055797.513 156.71538807686565 0.01498501498501792
    782 1490055797.714 156.3853769103851 0.013986013986017065
    941 1490055797.913 156.04869918834714 0.012987012987016211
    42 1490055798.114 155.7186880218666 0.011988011988015135
    155 1490055798.315 155.38867685538605 0.015984015984019218
    521 1490055798.514 155.0519991333481 0.009990009990013204
    327 1490055798.716 154.72532124464624 0.02397602397602716
    659 1490055798.912 154.37864368927217 0.002997002997006115
    406 1490055799.115 154.055299078349 0.011988011988015135
    900 1490055799.315 153.72195463408974 0.010989010989014059
    727 1490055799.517 153.3952767453879 0.019980019980023078
    901 1490055799.714 153.0519324677925 0.008991008991012128
    902 1490055799.917 152.72858785686935 0.017982017982021148
    570 1490055800.118 152.3985766903888 0.02197802197802523
    338 1490055800.314 152.05189913501474 0.0009990009990044069
    339 1490055800.519 151.735221079649 0.02497502497502846
    862 1490055800.715 151.3852102464962 0.003996003996007413
    863 1490055800.917 151.06186563557304 0.017982017982021592
    431 1490055801.117 150.72518791353508 0.011988011988015579
    340 1490055801.316 150.3918434692758 0.005994005994009566
    341 1490055801.518 150.06516558057396 0.014985014985018585
    729 1490055801.719 149.73515441409342 0.018981018981022668
    509 1490055801.916 149.39181013649804 0.0029970029970067813
    510 1490055802.116 149.05846569223877 0.006993006993010864
    965 1490055802.321 148.74178763687303 0.02097902097902482
    653 1490055802.518 148.39844335927765 0.00999000999001387
    654 1490055802.72 148.0717654705758 0.01898101898102289
    648 1490055802.919 147.73508774853784 0.017982017982021814
    321 1490055803.116 147.39174347094246 0.001998001998005927
    304 1490055803.319 147.0683988600193 0.010989010989014947
    883 1490055803.519 146.73505441576003 0.00999000999001387
    884 1490055803.718 146.39837669372207 0.008991008991012794
    393 1490055803.923 146.08169863835633 0.02797202797203191
    394 1490055804.123 145.74835419409706 0.026973026973030834
    755 1490055804.32 145.40500991650168 0.010989010989014947
    37 1490055804.522 145.07833202779983 0.024975024975028903
    216 1490055804.719 144.73498775020445 0.0039960039960078575
    217 1490055804.922 144.4116431392813 0.017982017982022036
    218 1490055805.12 144.07163213946464 0.006993006993011086
    859 1490055805.322 143.7449542507628 0.020979020979025043
    187 1490055805.518 143.39827669538872 3.9968028886505635e-15
    188 1490055805.722 143.07826536224428 0.013986013986018175
    189 1490055805.919 142.7349210846489 0.0029970029970072254
    190 1490055806.124 142.41824302928316 0.02197802197802634
    904 1490055806.322 142.0782320294665 0.01098901098901539
    906 1490055806.522 141.74488758520724 0.009990009990014537
    227 1490055806.722 141.41154314094797 0.008991008991013683
    458 1490055806.922 141.0781986966887 0.007992007992012828
    426 1490055807.122 140.74485425242943 0.006993006993011974
    325 1490055807.323 140.4148430859489 0.010989010989016057
    698 1490055807.52 140.0714988083535 5.10702591327572e-15
    699 1490055807.726 139.75815403076646 0.02397602397602916
    388 1490055807.921 139.40814319761367 0.0029970029970081136
    389 1490055808.123 139.08146530891182 0.011988011988017133
    977 1490055808.323 138.74812086465255 0.0009990009990061832
    684 1490055808.521 138.4081098648359 5.10702591327572e-15
    685 1490055808.727 138.09476508724885 0.023976023976028937
    686 1490055808.926 137.75808736521088 0.017982017982022924
    869 1490055809.123 137.4147430876155 0.006993006993011974
    794 1490055809.322 137.07806536557754 0.0009990009990059612
    795 1490055809.526 136.7580540324331 0.01498501498502014
    115 1490055809.723 136.41470975483773 0.00399600399600919
    116 1490055809.927 136.0946984216933 0.022977022977028083
    933 1490055810.127 135.75802069965533 0.01698301698302207
    934 1490055810.326 135.42467625539607 0.010989010989016057
    402 1490055810.527 135.09466508891552 0.01498501498502014
    403 1490055810.724 134.75132081132014 0.00399600399600919
    404 1490055810.925 134.4213096448396 0.0029970029970083356
    31 1490055811.124 134.08463192280163 0.0019980019980074815
    378 1490055811.328 133.7646205896572 0.020979020979026597
    573 1490055811.526 133.42460958984054 0.004995004995010488
    574 1490055811.728 133.0979317011387 0.018981018981024445
    746 1490055811.927 132.76125397910073 0.007992007992013495
    322 1490055812.129 132.43457609039888 0.021978021978027673
    752 1490055812.328 132.09789836836092 0.010989010989016723
    360 1490055812.528 131.76455392410165 0.014985014985020806
    919 1490055812.726 131.424542924285 0.003996003996009856
    920 1490055812.926 131.09119848002572 0.0029970029970087797
    551 1490055813.129 130.76785386910257 0.0119880119880178
    54 1490055813.331 130.44117598040071 0.02097902097902682
    289 1490055813.529 130.10116498058406 0.014985014985020806
    290 1490055834.482 198.94333427776246 0.023976023976029825
    33 1490055834.484 197.95000083331988 0.012987012987018876
    708 1490055834.485 196.95333411109857 0.010989010989016945
    40 1490055834.485 195.95333411109857 0.010989010989016723
    365 1490055834.486 194.95666738887726 0.02297702297702875
    382 1490055834.486 193.95666738887726 0.021978021978027673
    910 1490055834.486 192.95666738887726 0.020979020979026597
    15 1490055834.487 191.96000066665596 0.01998001998002552
    549 1490055834.487 190.96000066665596 0.00899100899101457
    316 1490055834.487 189.96000066665596 0.00699300699301264
    554 1490055834.487 188.96000066665596 0.00499500499501071
    916 1490055834.488 187.96333394443465 0.018981018981024667
    523 1490055834.488 186.96333394443465 0.0069930069930128624
    524 1490055834.488 185.96333394443465 0.015984015984021882
    221 1490055834.488 184.96333394443465 0.0029970029970090017
    230 1490055834.489 183.96666722221335 0.016983016983022958
    994 1490055834.489 182.96666722221335 0.010989010989016945
    866 1490055834.489 181.96666722221335 0.022977022977029193
    966 1490055834.49 180.97000049999204 6.217248937900877e-15
    967 1490055834.49 179.97000049999204 0.0039960039960103
    253 1490055834.49 178.97000049999204 0.010989010989017611
    576 1490055834.49 177.97000049999204 0.009990009990016535
    740 1490055834.491 176.97333377777073 0.020979020979027485
    384 1490055834.491 175.97333377777073 0.015984015984022992
    60 1490055834.491 174.97333377777073 0.02297702297703008
    354 1490055834.491 173.97333377777073 0.006993006993014195
    893 1490055834.492 172.97666705554943 0.006993006993014195
    250 1490055834.492 171.97666705554943 0.001998001998009258
    185 1490055834.492 170.97666705554943 0.009990009990017423
    506 1490055834.492 169.97666705554943 0.02197802197802945
    49 1490055834.493 168.98000033332812 0.022977022977030748
    707 1490055834.493 167.98000033332812 0.0019980019980079256
    651 1490055834.493 166.98000033332812 0.014985014985020806
    424 1490055834.493 165.98000033332812 0.028971028971034762
    425 1490055834.493 164.98000033332812 0.027972027972033686
    422 1490055834.494 163.98333361110681 0.02697302697303261
    680 1490055834.494 162.98333361110681 0.014985014985020806
    843 1490055834.494 161.98333361110681 0.023976023976029825
    16 1490055834.494 160.98333361110681 0.018981018981024667
    17 1490055834.495 159.98333361110681 0.027972027972033686
    125 1490055834.495 158.9866668888855 0.016983016983022736
    126 1490055834.495 157.9866668888855 0.01598401598402166
    251 1490055834.495 156.9866668888855 0.014985014985020584
    294 1490055834.495 155.9866668888855 0.012987012987018653
    317 1490055834.496 154.9900001666642 0.010989010989016723
    479 1490055834.496 153.9900001666642 0.02297702297702875
    284 1490055834.496 152.9900001666642 5.995204332975845e-15
    287 1490055834.496 151.9900001666642 0.003996003996010078
    215 1490055834.496 150.9900001666642 0.0029970029970090017
    871 1490055834.496 149.9900001666642 0.021978021978027895
    201 1490055834.497 148.9933334444429 0.02097902097902682
    717 1490055834.497 147.9933334444429 5.773159728050814e-15
    956 1490055834.497 146.9933334444429 0.003996003996009856
    694 1490055834.497 145.9933334444429 0.019980019980025965
    865 1490055834.497 144.9933334444429 0.003996003996010078
    56 1490055834.498 143.9966667222216 0.01698301698302318
    454 1490055834.498 142.9966667222216 0.010989010989017167
    547 1490055834.498 141.9966667222216 0.022977022977029415
    788 1490055834.498 140.9966667222216 0.006993006993013529
    531 1490055834.498 139.9966667222216 0.018981018981025555
    595 1490055834.498 138.9966667222216 0.01798201798202448
    377 1490055834.499 138.00000000000028 0.0019980019980085917
    503 1490055834.499 137.00000000000028 0.015984015984022548
    152 1490055834.499 136.00000000000028 0.014985014985021472
    32 1490055834.499 135.00000000000028 0.003996003996010522
    522 1490055834.5 134.00333327777898 0.01798201798202448
    374 1490055834.5 133.00333327777898 0.02197802197802856
    291 1490055834.5 132.00333327777898 6.661338147750939e-15
    423 1490055834.5 131.00333327777898 0.018981018981025777
    614 1490055834.5 130.00333327777898 0.017982017982024923
    412 1490055834.501 129.00666655555767 0.01698301698302407
    108 1490055834.501 128.00666655555767 0.00999000999001698
    495 1490055834.501 127.00666655555767 0.008991008991015903
    103 1490055834.501 126.00666655555767 0.010989010989018277
    220 1490055834.501 125.00666655555767 0.004995004995012264
    444 1490055834.502 124.00999983333638 0.01898101898102622
    331 1490055834.502 123.00999983333638 0.007992007992015271
    243 1490055834.502 122.00999983333638 0.010989010989018277
    663 1490055834.502 121.00999983333638 0.004995004995012264
    716 1490055834.502 120.00999983333638 0.008991008991016347
    161 1490055834.503 119.01333311111509 0.017982017982025367
    641 1490055834.503 118.01333311111509 0.010989010989018277
    186 1490055834.503 117.01333311111509 0.00399600399601141
    83 1490055834.503 116.01333311111509 0.002997002997010556
    655 1490055834.503 115.01333311111509 0.0109890109890185
    877 1490055834.504 114.0166663888938 7.549516567451064e-15
    878 1490055834.504 113.0166663888938 0.003996003996011632
    915 1490055834.504 112.0166663888938 0.02197802197802967
    806 1490055834.504 111.0166663888938 0.020979020979028595
    303 1490055834.504 110.0166663888938 7.549516567451064e-15
    28 1490055834.504 109.0166663888938 0.018981018981026665
    881 1490055834.505 108.0199996666725 0.01798201798202559
    112 1490055834.505 107.0199996666725 0.016983016983024513
    46 1490055834.505 106.0199996666725 0.015984015984023436
    846 1490055834.505 105.0199996666725 0.01498501498502236
    446 1490055834.505 104.0199996666725 0.013986013986021284
    963 1490055834.506 103.0199996666725 0.012987012987020208
    964 1490055834.506 102.02333294445121 0.011988011988019132
    763 1490055834.506 101.02333294445121 0.025974025974033088
    937 1490055834.506 100.02333294445121 0.014985014985022138
    938 1490055834.506 99.02333294445121 0.003996003996011188
    205 1490055834.506 98.02333294445121 0.002997002997010334
    206 1490055834.507 97.02666622222992 0.001998001998009258
    82 1490055834.507 96.02666622222992 0.015984015984023214
    356 1490055834.57 95.23666272228826 0.10989010989011727
    358 1490055834.753 94.84665255579104 0.02397602397603138
    359 1490055834.954 94.51664138931048 0.027972027972035463
    
  3. hcientist commented on Apr 20, 2017

    @hcientist

    perhaps this would be a better approach for my project? https://github.com/eventEmitter/request-rate-limiter will update

  4. jonathanstokes commented on Mar 15, 2019

    @jonathanstokes

    I have this same problem. It's like the whole purpose of the library isn't working.

  5. josh-mercarto commented on Apr 18, 2020

    @josh-mercarto

    This library is still not working for me. This simple test fails and my mockFn is never called.

    import * as Limiter from "limiter";
    
    test("should invoke function if tokens are remaining", () => {
    const mockFn = jest.fn().mockReturnValue(5);
    const limiter = new Limiter.RateLimiter(5, "sec");
    
        limiter.removeTokens(1, function (err, remainingRequests) {
          mockFn();
        });
        expect(mockFn).toHaveBeenCalled();
    }
    
  6. jhurliman commented on Apr 18, 2020

    @jhurliman
    Owner

    @josh-mercarto that test looks like you are expecting the callback to have been synchronously executed before moving to the next line? That won’t happen.

  7. added a commit that references this issue on Apr 4, 2021
  8. boly38 commented on Apr 4, 2021

    @boly38

    Hello,

    following #68 I'm using this lib on my website which is making rate limited etsy api call (creharmony/node-etsy-client#19)
    I encounter issue because when setting rate limiter do 10 or 9,8,or 7 per seconds, I reached etsy limits in any cases (10/sec).
    So for me there is an issue I'm trying to understand. To diagnose this library, I've done a little sample.

    ==> cf draft PR #70

    What I encounter during my tests:

    • setting 10/sec, the test show me that I'm almost always around 10 per seconds
    • sometime I got 11 queries in the second window. That's violate the contract but dont hurt me so much
    • but sometime at the first slot, there is double of allowed queries that are made, ie. 20queries/sec! and this point seems to be a major issue because I can't deal with this point.
    PromiseWithResult==>RESULT:promiseWithResult18
    PromiseWithResult==>RESULT:promiseWithResult19
    duration: 1006ms => 20 / 20
    PromiseWithResult==>RESULT:promiseWithResult20
    PromiseWithResult==>RESULT:promiseWithResult21
    
    

    I dont see a fix for this 2017 issue so I'm less and less confident in this library. Any hint would be greatly appreciated

  9. jhurliman commented on Sep 10, 2026

    @jhurliman
    Owner

    The concurrent allowance and initial-interval bugs are fixed in 4.0.0 and covered by deterministic regressions. There is also a separate composition issue in the original example: awaiting two independent limiters does not reserve both budgets at the eventual work start, so work can accumulate behind the second limiter. #105 documents this distinction and the fixed-window limitation explicitly. Parent/child TokenBuckets provide atomic hierarchical token-bucket accounting, but do not provide strict sliding-window limits. Leaving this open through review of the clarification rather than claiming that arbitrary sequential limiter composition is fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions