superagent-throttle icon indicating copy to clipboard operation
superagent-throttle copied to clipboard

Temporarily disabling throttling

Open faune opened this issue 4 years ago • 1 comments

Hi there, Cool plugin for superagent! But I think I introduced a bug into https://github.com/faune/homebridge-grohe-sense while integrating this plugin. My plugin has an option to enable/disable throttling in its config, and I believe I incorrectly implemented this using:

    // Throttling support for HTTP requests
    this.throttle = new Throttle({
      'active': this.config['throttle_support'],        // set false to pause queue
      'rate': this.config['throttle_rate'],             // how many requests can be sent every `ratePer`
      'ratePer': this.config['throttle_rateper'],       // number of ms in which `rate` requests may be sent
      'concurrent': this.config['throttle_concurrent'], // how many requests can be sent concurrently
    });

If active is set to false, it seems all requests are paused.

I have a generic getURL() function called with an URI endpoint like this:

private async getURL(url: string) {
    if (!this.accessToken) {
      this.log.error('getURL(): Cannot call getURL() before an access token has been acquired');
    }
    this.log.debug('getURL(): GET: ', url);
    
    return new Promise<superagent.Response>((resolve, reject) => {
      superagent
        .get(url)
        .use(this.throttle.plugin())
        .set('Content-Type', 'application/json')
        .set('Authorization', `Bearer ${this.accessToken}`)
        .set('accept', 'json')
        .end((err, res) => {
          if (err) {
            const errMsg = `getURL(): Unexpected server response: ${err}`;
            reject(errMsg);
          } else {
            resolve(res);
          }
        });
    });
  }

How can I still keep this generic getURL() function which sets .use(this.throttle.plugin()) and keep it optional if a user wants to enable/disable throttling?

faune avatar Jul 27 '21 23:07 faune

Sure ok.

Unfortunately I haven't really used superagent since I published this plugin. I'm sorry but it's more or less unmaintained at this time.

As a workaround maybe you could use the options method to set the rate to a really high number if the user wants to "disable" throttling.

leviwheatcroft avatar Aug 08 '21 08:08 leviwheatcroft