Skip to content

Setup Rate limiting

Configuring and Managing Function Rate Limiting

This guide explains how to set up, activate, deactivate, and modify rate limits for individual Cloud Functions using the Flame Shield dashboard.

Setup Rate Limiting in your application

To begin with rate limiting, enable it from the dashboard.

This will provide you with a new tab, and an API key. Click Rate Limiting on the navigation bar, and take a copy of your API key - you'll need this to configure your functions:

Install rate limiting package in your application

Currently rate limiting is only supported for nodejs firebase function applications. Please get in touch if you'd like to use python

To get started, install the flamesshield SDK into your application

npm install flamesshield-sdk --save

Then, import the package:

import { rateLimit } from "flamesshield-sdk";

Then wrap your existing function in a rateLimit:

export const helloWorld = onRequest((request, response) => {
  return RateLimit(
    {
      app: admin.app(),
      maxCalls: 5,
      apiKey: "<APIKEY>",
      periodSeconds: 60,
      onSuccess: () => {
        logger.info("Hello logs!", {structuredData: true});
        response.send("Hello from Firebase!");     
      },
      onFailure: () => {
        logger.error("Rate limit exceeded!", {structuredData: true});
        response.status(429).send("Rate limit exceeded!");
      }
    }
  )  
});

Now, you have a basic rate limiting function setup. Make sure that the name matches the function name - e.g. helloWorld matches the export helloWorld.

Initial Configuration

  1. Navigate to Rate Limiting:

    • In the left-hand navigation menu, click on Rate Limiting.
    • This page displays cards for each detected Cloud Function eligible for rate limiting. Functions without a configured limit will show a Not Configured status tag.

    2. Set Requests Per Second (RPS): * Locate the card for the function you wish to rate limit (e.g., ext-notification-centre-newNotificationTrigger). * In the Requests per second input field on the function's card, enter the desired maximum RPS value. In the video, the user enters 5. 3. Enable Configuration: * Click the + Enable Rate Limiting button on the same card. 4. Confirm Configuration:

    • The card will update:
    • The status tag changes to Configured (Not active).
    • The input field is replaced with text indicating the activation threshold (e.g., Activate at: 5 rps).
    • Two new buttons appear: Manually Start Rate Limiting and Configure Rate Limiting.

Activating Rate Limiting

  1. Locate Configured Function:
  2. Find the card for the function that is Configured (Not active).
  3. Manually Start:
  4. Click the Manually Start Rate Limiting button.
  5. Confirm Activation:
  6. The card will update:
    • The status tag changes to Active (green background).
    • The activation threshold text (e.g., Activate at: 5 rps) remains visible.
    • The buttons are replaced with a single Stop Rate Limiting button.

Deactivating Rate Limiting

  1. Locate Active Function:
  2. Find the card for the function that is currently Active.
  3. Stop Limiting:
  4. Click the Stop Rate Limiting button.
  5. Confirm Deactivation:
  6. The card will revert to the Configured (Not active) state, displaying the activation threshold and the Manually Start Rate Limiting and Configure Rate Limiting buttons again.

Modifying or Removing Configuration

  1. Access Configuration:
  2. On a function card that is Configured (Not active), click the Configure Rate Limiting button.
  3. Configuration View:
  4. The card will display the configuration options:
    • Requests per second: An input field to change the RPS limit.
    • + Update configuration: Button to save changes.
    • Cancel: Button to discard changes and exit configuration view.
    • Remove Rate Limiting: (Red button) To completely remove the rate limit configuration for this function.
  5. Update, Remove, or Cancel:
  6. To change the limit: Enter a new value in the Requests per second field and click + Update configuration.
  7. To remove the limit: Click the red Remove Rate Limiting button (this action may require confirmation).
  8. To exit without changes: Click Cancel. (The video shows the user clicking Cancel).