Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bucket is being created with all values = NaN #5

Open
huntonas opened this issue Apr 25, 2016 · 2 comments
Open

bucket is being created with all values = NaN #5

huntonas opened this issue Apr 25, 2016 · 2 comments

Comments

@huntonas
Copy link

I have been debugging why I keep getting returned that the limit has been reached even on the first call, and I found that somehow there is a bucket being returned in line 81 of tokenthrottle/index.js with all of the values set at NaN. This causes it to not create a new bucket with the options that I am sending in.
This is where I am initializing everything upon start up. I then store it in a factory and call it when I am about to POST or GET to another server (which is what I am throttle'ing). I am not sure why the a bucket is being returned or how to stop it from being returned or even where it is being created. Have you ever seen this before?

    client = require('restify').createJsonClient({
        url: url,
        version: '*',
        userAgent: false
    });
    var limiterClient;

    if(redisClient && options.limiterOptions && options.limiterOptions.enable){

        var limiterParams = {
            expiry: options.limiterOptions.redisExpiry * 1000,
            prefix: options.limiterOptions.prefix,
            rate: options.limiterOptions.rate,
            window: options.limiterOptions.window
        };

        var limiter = require('tokenthrottle-redis')(limiterParams, redisClient);
    }

    require('./lib/factory').init(client, logger, {options: options.limiterOptions, client: limiter}, url);

And this is where the bucket is being returned which is causing a new bucket to not be created on the first call

self.getter.call(self.table, key, function (err, bucket) {
    if (err) {
      return cb(new Error("Unable to check token table" + err))
    }
    if (bucket) {
      // Recreate the token bucket
      bucket = TokenBucket(bucket)
    }
    else {
      // Make a new one
      bucket = TokenBucket({
        capacity: burst,
        fillRate: rate,
        window: wnd,
      })
    }

    var hasCapacity = bucket.consume(1)

    //console.log("Throttle(%s): num_tokens= %d -- throttled: %s", key, bucket.tokens, !hasCapacity)

    self.putter.call(self.table, key, bucket, function (err) {
      // Error here is not fatal -- we were able to determine throttle status, just not save state.
      if (err) {
        err = new Error("Error saving throttle information to table" + err)
      }
      if (!hasCapacity) {
        return cb(err, true)
      }
      return cb(err, false)
    })
  })

Any help is appreciated. Please let me know if I did not provide enough information.

@brycebaril
Copy link
Owner

Hmm, not something I've seen before.

Could you verify what you're setting in:

 var limiterParams = {
            expiry: options.limiterOptions.redisExpiry * 1000,
            prefix: options.limiterOptions.prefix,
            rate: options.limiterOptions.rate,
            window: options.limiterOptions.window
        };

So we can see what the settings the limiter is being constructed with?

@huntonas
Copy link
Author

expiry: 60000, //also 60 but tried 60k to see if it was timing out in ms for some reason
prefix: "blah",
rate: 600,
window: 60000

At the point where I am calling client.rateLimit, these are the values on the client:

burst: 600,
rate: 600,
window: 60000

When it reaches Throttle.prototype.rateLimit, the context has the correct expiry and prefix values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants