2.11.0 ā€¢ Published 4 months ago

@fluxninja/aperture-js v2.11.0

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
4 months ago

Rate Limiting for JavaScript Applications

The aperture-js SDK provides an easy way to integrate your JavaScript applications with FluxNinja Aperture. It allows flow control functionality on fine-grained features inside service code.

Refer documentation for more details.

Usage

Install SDK

Run the command below to install the SDK:

npm install @fluxninja/aperture-js

Create Aperture Client

The next step is to create an Aperture Client instance, for which, the address of the organization created in Aperture Cloud and API key are needed. You can locate both these details by clicking on the Aperture tab in the sidebar menu of Aperture Cloud.

import { ApertureClient } from "@fluxninja/aperture-js";

// Create aperture client
export const apertureClient = new ApertureClient({
  address: "ORGANIZATION.app.fluxninja.com:443",
  apiKey: "API_KEY",
});

Flow Functionality

The created instance can then be used to start a flow:

async function handleRequestRateLimit(req: Request, res: Response) {
  // Start a flow by passing control point and business labels
  const flow = await apertureClient.startFlow("awesomeFeature", {
    labels: {
      limit_key: "some_user_id",
    },
    grpcCallOptions: {
      deadline: Date.now() + 300, // ms
    },
  });

  if (flow.shouldRun()) {
    // Add business logic to process incoming request
    console.log("Request accepted. Processing...");
    const resString = "foo";
    res.send({ message: resString });
  } else {
    console.log("Request rate-limited. Try again later.");
    // Handle flow rejection
    flow.setStatus(FlowStatus.Error);
    res.status(429).send({ message: "Too many requests" });
  }

  flow.end();
}

The above code snippet is making startFlow calls to Aperture. For this call, it is important to specify the control point (awesomeFeature in the example) and business labels that will be aligned with the policy created in Aperture Cloud. For request prioritization use cases, it's important to set a higher gRPC deadline. This parameter specifies the maximum duration a request can remain in the queue. For each flow that is started, a shouldRun decision is made, determining whether to allow the request into the system or to rate limit it. In this example, we only see log returns, but in a production environment, actual business logic can be executed when a request is allowed. It is important to make the end call made after processing each request, to send telemetry data that would provide granular visibility for each flow.

For more context on using the Aperture JavaScript SDK to set feature control points, refer to the example app available in the repository.

API Reference

@fluxninja/aperture-js

Table of contents

Enumerations

Classes

Interfaces

2.11.0

4 months ago

2.10.1

4 months ago

2.10.0

4 months ago

2.9.1

4 months ago

2.9.0

5 months ago

2.8.2

5 months ago

2.8.1

5 months ago

2.8.0

5 months ago

2.7.0

5 months ago

2.6.1

5 months ago

2.6.0

5 months ago

2.5.0

6 months ago

2.4.4

6 months ago

2.4.2

6 months ago

2.4.1

6 months ago

2.3.6

6 months ago

2.3.5

6 months ago

2.3.4

6 months ago

2.3.3

6 months ago

2.3.2

6 months ago

2.3.0

7 months ago

2.2.0

7 months ago

2.1.6

7 months ago

2.1.5

7 months ago

2.1.4

7 months ago

2.1.3

7 months ago

2.1.2

7 months ago

2.1.1

8 months ago

2.1.0

8 months ago

2.0.21

8 months ago

2.0.20

8 months ago

2.0.19

8 months ago

2.0.18

8 months ago

2.0.17

8 months ago

2.0.16

8 months ago

2.0.15

8 months ago

2.0.14

8 months ago

2.0.13

8 months ago

2.0.12

8 months ago

2.0.11

8 months ago

2.0.10

8 months ago

2.0.9

8 months ago

2.0.8

8 months ago

2.0.7

8 months ago

2.0.6

8 months ago

2.0.5

8 months ago

2.0.4

9 months ago

2.0.3

9 months ago

2.0.2

9 months ago

2.0.1

9 months ago

2.0.0

9 months ago

1.1.0

9 months ago

1.0.1

9 months ago

1.0.0

9 months ago

2.4.0

9 months ago