Logos DX
    Preparing search index...

    Function rateLimit

    • Rate limiter that restricts function calls to a specified number per time window.

      Implements a token bucket rate limiting algorithm that enforces limits by either throwing errors or waiting for a token before proceeding.

      Type Parameters

      • T extends Func

        The function type being rate limited

      Parameters

      • fn: T

        Function to apply rate limiting to

      • opts: RateLimitOptions<T> & { throws: false }

        Rate limiting configuration options or bucket options

      Returns (...args: Parameters<T>) => Promise<ReturnType<T>>

      Rate-limited version of the original function (async)

      // Throwing rate limiter with options
      const rateLimitedFn = rateLimit(fn, {
      maxCalls: 10,
      windowMs: 1000,
      throws: true,
      onLimitReached: (error) => {
      console.error('Rate limit exceeded:', error.message);
      }
      });

      for (let i = 0; i < 10; i++) {
      await rateLimitedFn();
      }

      // will throw an error
      await rateLimitedFn();
      // With an existing bucket (useful for persistence)
      const bucket = new RateLimitTokenBucket({
      capacity: 10,
      refillIntervalMs: 100,
      save: async (state) => redis.set('key', JSON.stringify(state)),
      load: async () => JSON.parse(await redis.get('key'))
      });

      const rateLimitedFn = rateLimit(fn, {
      bucket,
      throws: true
      });

      // When bucket.isSaveable is true, load() is called before each check
      // and save() is called after each successful consume
      await rateLimitedFn();
      // Waits for token if throws: false
      const rateLimitedWithoutThrowing = rateLimit(fn, {
      maxCalls: 10,
      windowMs: 1000,
      throws: false
      });

      for (let i = 0; i < 10; i++) {
      await rateLimitedWithoutThrowing();
      }

      // will wait for token, then call fn
      await rateLimitedWithoutThrowing();
    • Rate limiter that restricts function calls to a specified number per time window.

      Implements a token bucket rate limiting algorithm that enforces limits by either throwing errors or waiting for a token before proceeding.

      Type Parameters

      • T extends Func

        The function type being rate limited

      Parameters

      • fn: T

        Function to apply rate limiting to

      • opts: RateLimitOptions<T> & { throws?: true }

        Rate limiting configuration options or bucket options

      Returns (...args: Parameters<T>) => Promise<ReturnType<T>>

      Rate-limited version of the original function (async)

      // Throwing rate limiter with options
      const rateLimitedFn = rateLimit(fn, {
      maxCalls: 10,
      windowMs: 1000,
      throws: true,
      onLimitReached: (error) => {
      console.error('Rate limit exceeded:', error.message);
      }
      });

      for (let i = 0; i < 10; i++) {
      await rateLimitedFn();
      }

      // will throw an error
      await rateLimitedFn();
      // With an existing bucket (useful for persistence)
      const bucket = new RateLimitTokenBucket({
      capacity: 10,
      refillIntervalMs: 100,
      save: async (state) => redis.set('key', JSON.stringify(state)),
      load: async () => JSON.parse(await redis.get('key'))
      });

      const rateLimitedFn = rateLimit(fn, {
      bucket,
      throws: true
      });

      // When bucket.isSaveable is true, load() is called before each check
      // and save() is called after each successful consume
      await rateLimitedFn();
      // Waits for token if throws: false
      const rateLimitedWithoutThrowing = rateLimit(fn, {
      maxCalls: 10,
      windowMs: 1000,
      throws: false
      });

      for (let i = 0; i < 10; i++) {
      await rateLimitedWithoutThrowing();
      }

      // will wait for token, then call fn
      await rateLimitedWithoutThrowing();
    • Rate limiter that restricts function calls to a specified number per time window.

      Implements a token bucket rate limiting algorithm that enforces limits by either throwing errors or waiting for a token before proceeding.

      Type Parameters

      • T extends Func

        The function type being rate limited

      Parameters

      • fn: T

        Function to apply rate limiting to

      • opts: RateLimitBucketOptions<T> & { throws: false }

        Rate limiting configuration options or bucket options

      Returns (...args: Parameters<T>) => Promise<ReturnType<T>>

      Rate-limited version of the original function (async)

      // Throwing rate limiter with options
      const rateLimitedFn = rateLimit(fn, {
      maxCalls: 10,
      windowMs: 1000,
      throws: true,
      onLimitReached: (error) => {
      console.error('Rate limit exceeded:', error.message);
      }
      });

      for (let i = 0; i < 10; i++) {
      await rateLimitedFn();
      }

      // will throw an error
      await rateLimitedFn();
      // With an existing bucket (useful for persistence)
      const bucket = new RateLimitTokenBucket({
      capacity: 10,
      refillIntervalMs: 100,
      save: async (state) => redis.set('key', JSON.stringify(state)),
      load: async () => JSON.parse(await redis.get('key'))
      });

      const rateLimitedFn = rateLimit(fn, {
      bucket,
      throws: true
      });

      // When bucket.isSaveable is true, load() is called before each check
      // and save() is called after each successful consume
      await rateLimitedFn();
      // Waits for token if throws: false
      const rateLimitedWithoutThrowing = rateLimit(fn, {
      maxCalls: 10,
      windowMs: 1000,
      throws: false
      });

      for (let i = 0; i < 10; i++) {
      await rateLimitedWithoutThrowing();
      }

      // will wait for token, then call fn
      await rateLimitedWithoutThrowing();
    • Rate limiter that restricts function calls to a specified number per time window.

      Implements a token bucket rate limiting algorithm that enforces limits by either throwing errors or waiting for a token before proceeding.

      Type Parameters

      • T extends Func

        The function type being rate limited

      Parameters

      • fn: T

        Function to apply rate limiting to

      • opts: RateLimitBucketOptions<T> & { throws?: true }

        Rate limiting configuration options or bucket options

      Returns (...args: Parameters<T>) => Promise<ReturnType<T>>

      Rate-limited version of the original function (async)

      // Throwing rate limiter with options
      const rateLimitedFn = rateLimit(fn, {
      maxCalls: 10,
      windowMs: 1000,
      throws: true,
      onLimitReached: (error) => {
      console.error('Rate limit exceeded:', error.message);
      }
      });

      for (let i = 0; i < 10; i++) {
      await rateLimitedFn();
      }

      // will throw an error
      await rateLimitedFn();
      // With an existing bucket (useful for persistence)
      const bucket = new RateLimitTokenBucket({
      capacity: 10,
      refillIntervalMs: 100,
      save: async (state) => redis.set('key', JSON.stringify(state)),
      load: async () => JSON.parse(await redis.get('key'))
      });

      const rateLimitedFn = rateLimit(fn, {
      bucket,
      throws: true
      });

      // When bucket.isSaveable is true, load() is called before each check
      // and save() is called after each successful consume
      await rateLimitedFn();
      // Waits for token if throws: false
      const rateLimitedWithoutThrowing = rateLimit(fn, {
      maxCalls: 10,
      windowMs: 1000,
      throws: false
      });

      for (let i = 0; i < 10; i++) {
      await rateLimitedWithoutThrowing();
      }

      // will wait for token, then call fn
      await rateLimitedWithoutThrowing();