Skip to content

Ratelimit

Ratelimit module could be used to avoid sending too many requests to the backend during a configurable period of time.

Usage

If you don't want more than 3 requests per second, you should define a circuit like this:

typescript
import * as Mollitia from 'mollitia';
const circuit = new Mollitia.Circuit({
  options: {
    modules: [
      new Mollitia.Ratelimit({
        limitPeriod: 1000,
        limitForPeriod: 3
      })
    ]
  }
});
import * as Mollitia from 'mollitia';
const circuit = new Mollitia.Circuit({
  options: {
    modules: [
      new Mollitia.Ratelimit({
        limitPeriod: 1000,
        limitForPeriod: 3
      })
    ]
  }
});

If you don't want more than 3 requests per second, and also at least 200ms between each request, you should define a circuit like this:

typescript
const ratelimit = new Mollitia.Ratelimit({
  limitPeriod: 1000,
  limitForPeriod: 3
});
const ratelimit2 = new Mollitia.Ratelimit({
  limitPeriod: 200,
  limitForPeriod: 1
});
const circuit = new Mollitia.Circuit({
  options: {
    modules: [
      ratelimit2,
      ratelimit
    ]
  }
});
const ratelimit = new Mollitia.Ratelimit({
  limitPeriod: 1000,
  limitForPeriod: 3
});
const ratelimit2 = new Mollitia.Ratelimit({
  limitPeriod: 200,
  limitForPeriod: 1
});
const circuit = new Mollitia.Circuit({
  options: {
    modules: [
      ratelimit2,
      ratelimit
    ]
  }
});

API Reference

Options

NameDescriptionDefault
limitPeriodSpecifies the time period during which the ratelimit is calculated0
limitForPeriodSpecifies the maximum number of requests during the periodInfinity

Methods

NameDescriptionReturns
getExecParams()Returns the circuit function parameters.any[] params

Events

NameDescriptionParams
executeCalled when the module is executed.Mollitia.Circuit circuit, Promise<T> promise, any[] params