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

Promise issue #26

Closed
tomblanchard opened this issue Apr 6, 2019 · 2 comments
Closed

Promise issue #26

tomblanchard opened this issue Apr 6, 2019 · 2 comments

Comments

@tomblanchard
Copy link

I need to execute 100 promises while respecting a leaky bucket limit, this is the code I have so far:

var Bucket = require("dripping-bucket").Bucket;

var bucket = new Bucket({
  buckets: {
    size: 35,
    refreshRate: 2,
    refreshInterval: 1
  }
});

function requests() {
  return Array.from({ length: 100 }, () => {
    return new Promise(async (resolve, reject) => {
      var delay = await bucket.getDelay("testing");

      if (!delay) {
        console.log(`request ${new Date()}`);
        await bucket.returnToken("testing");
        resolve();
      } else {
        setTimeout(() => {
          console.log(`request ${new Date()}`);
          resolve();
        }, delay);
      }
    });
  });
};

Promise.all(requests());

I set the bucket up to have a size of 35 with a limit of 2 calls per second, however when I run this code, every promise resolves at the same time, here's what my console looks like:

request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)
request Sat Apr 06 2019 12:23:01 GMT+0100 (British Summer Time)

I'm obviously doing something wrong here but I'm not sure what I'm missing, any help would be greatly appreciated because this looks to be the only node package which supports a true leaky bucket limit with the ability to store state for multiple entities in Redis.

@roman-kaspar
Copy link

roman-kaspar commented May 13, 2019

Sorry I missed your question earlier. Hope you were able to solve the problem already, if not, find the code I'd use below.

Using 100 promises seems to be an overkill to me, 1 promise would do the same job. The trouble with your code was that the getDelay() function was called from each promise individually, while in order for the code to work, it must be invoked from one "execution thread" (for this scenario, consider each promise a new "execution thread"). I will update the documentation, I can see that it is not very clear in this regard.

Also, the getDelay() function was meant to "ask if we can proceed", and in case we cannot (the return value is not zero), you should ask again (i.e. invoke getDelay() function) in returned number of milliseconds.

Please let me know if this works for you, so that we can close this issue.

const Bucket = require('dripping-bucket').Bucket;

const bucket = new Bucket({
  buckets: {
    size: 35,
    refreshRate: 2,
    refreshInterval: 1,
  },
});

const promise = new Promise((resolve) => {
  let remaining = 100;
  const step = async () => {
    const delay = await bucket.getDelay('testing');
    if (!delay) {
      remaining -= 1;
      // do the actual work
      console.log(new Date());
      await bucket.returnToken('testing');
    }
    if (remaining) {
      setTimeout(step, delay);
    } else {
      resolve();
    }
  }
  step();
});

promise.then(() => { console.log('done'); });

@roman-kaspar roman-kaspar pinned this issue May 14, 2019
@roman-kaspar
Copy link

No follow-up in 2 months, closing the issue, ping me if you have questions.

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