nexus-agents - v2.80.0
    Preparing search index...

    Class AdapterRateLimiter

    Token bucket rate limiter for controlling request rates.

    The token bucket algorithm works as follows:

    1. A bucket holds tokens up to a maximum capacity
    2. Tokens are added at a fixed rate (refillRate per second)
    3. Each request consumes one or more tokens
    4. If insufficient tokens, the request is rejected or waits
    const limiter = new RateLimiter({
    capacity: 100, // Max 100 tokens
    refillRate: 10, // 10 tokens per second
    });

    if (limiter.tryAcquire()) {
    // Proceed with operation
    } else {
    // Rate limited
    }

    // Or wait for tokens
    await limiter.waitForTokens();
    Index

    Constructors

    Methods

    • Attempts to acquire the specified number of tokens.

      Parameters

      • tokens: number = 1

        Number of tokens to acquire (default: 1)

      Returns boolean

      true if tokens were acquired, false if rate limited

      if (limiter.tryAcquire(5)) {
      // Acquired 5 tokens
      }
    • Waits until the specified number of tokens are available, then acquires them.

      Parameters

      • tokens: number = 1

        Number of tokens to acquire (default: 1)

      Returns Promise<void>

      Promise that resolves when tokens are acquired

      If tokens exceed capacity (would wait forever)

      await limiter.waitForTokens(10);
      // 10 tokens acquired
    • Calculates the time in milliseconds until the specified tokens are available.

      Parameters

      • tokens: number = 1

        Number of tokens needed (default: 1)

      Returns number

      Time in milliseconds until tokens are available, 0 if already available