How to automatically handle calls using rateLimit typescript

18 Views Asked by At

I am using rateLimit decorator from the utils-decorator: https://vlio20.github.io/utils-decorators/#rateLimit

The function looks like this:

  @rateLimit({
    allowedCalls: Number(process.env['GET_RATE_LIMIT'] || '200'),
    timeSpanMs: Number(process.env['GET_RATE_LIMIT_TIME_SPAN_MS'] || '1000'),
  })
  async getById(
    context: GetContext,
    query: GetQuery,
  ): Promise<ClientResult<Vacancy>> {
    return this._withRetries(() => this._getById(context, query));
  }

If I set the GET_VACANCY_RATE_LIMIT to a lower value, say 5 then my application struggles and throws error:

You have exceeded the amount of allowed calls

I want to automatically handle this by slowing down instead of throwing errors. How do I achieve this? One approach that comes to my mind is to calculate the time elapsed since the last request and wait for the remaining delay before making a new request. But not sure if it'd work.

0

There are 0 best solutions below