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

Would it be possible to add a reset() method to clear state on local instances? #204

Open
kitsunde opened this issue Jun 7, 2022 · 1 comment

Comments

@kitsunde
Copy link

kitsunde commented Jun 7, 2022

Hello,

I'm using this inside of an AWS lambda for rate limiting during execution, one of the issues is we would like to reset state between runs as we do limiter.counts() to log the number of jobs that executed during the run.

We've worked around it by exporting a proxy object that lets us replace the bottleneck instance:

const Bottleneck = require('bottleneck');

const settings = { trackDoneStatus: true };

const limiter = new Proxy({ bottleneck: undefined }, {
  get(target, property) {
    if (property === 'reset') {
      return function reset() {
        target.bottleneck = undefined;
      };
    }

    if (target.bottleneck === undefined) {
      target.bottleneck = new Bottleneck(settings);
    }

    return target.bottleneck[property];
  },
});

module.exports = {
  limiter
}

Then in our application we do:

exports.handler = () > {
   limiter.reset();
   // Do stuff
   console.log(limiter.counts());
}

Ideally this could be done from the Bottleneck instance itself so we can keep a clean reference as Proxying like this creates other problems as event handlers and such don't work as expected. Extending bottleneck and attempting to clean out internal state means keeping track of internal changes to this library.

@dysbulic
Copy link

I'd like to second this. I'm using Bottleneck to limit the rate images are loaded from an IPFS gateway in response to a search. When a new search is entered, I need to stop loading the old results.

It'd be nice to be able to call a method and clear the job queue.

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