Skip to content

limiter.removeTokens of RateLimiterQueue with maximum wait time #212

@bkniffler

Description

@bkniffler

I'd love to implement a maximum wait on removeTokens. Our serverless environment only allows execution time of maximum 30 seconds. I imagine we'd wait 25s, throw an error if we still don't have a token available. The current RateLimiterQueue could wait for hours or even days, which wouldn't work for this usecase.

Thought about something similar to

    const rateLimit = await Promise.race([
      limiter.removeTokens(1),
      waitFor(maxWait),
    ]);

But noticed that, obviously, the limiter.removeTokens is not cancelled and the token is removed even though we wouldn't permit the operation.

What do you think, is there any way to solve this without a PR on rate-limiter-flexible? With a PR I could imagine one of these solutions:

  • adding support for CancelablePromise (as second argument on limiter.removeTokens)
  • add support for AbortController (as second argument on limiter.removeTokens)
  • have an optional ID (as second argument on limiter.removeTokens) and add another method limiter.cancelRemove(id)`

What do you think?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions