Skip to content

fluxninja/aperture-csharp

Repository files navigation

🚀 Just launched v1 of managed rate limiting service
Support us with your feedback and questions on Product Hunt or Discord

FluxNinja Aperture
Documentation Reference Slack Community Build Status

Rate Limiting for C# Applications

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

Refer documentation for more details.

Usage

Install

Run the command below to install the SDK:

dotnet add package ApertureSDK --version 2.23.1

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.

var sdk = ApertureSdk
    .Builder()
    .SetAddress("ORGANIZATION.app.fluxninja.com:443")
    .SetAgentApiKey("API_KEY")
    .Build();

Flow Functionality

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

// do some business logic to collect labels
var labels = new Dictionary<string, string>();
labels.Add("userId", "some_user_id");
labels.Add("userTier", "gold");
labels.Add("priority", "100");

var rampMode = false;
var flowTimeout = TimeSpan.FromSeconds(5);
var pms = new FeatureFlowParams(
    "featureName",
    labels,
    rampMode,
    flowTimeout,
    new Grpc.Core.CallOptions(),
    "test",
    new RepeatedField<string> { "test" });

var flow = sdk.StartFlow(pms);

if (flow.ShouldRun())
{
    // do actual work
    Thread.Sleep(2000);
    SimpleHandlePath((int)HttpStatusCode.OK, "Hello world!", response);
}
else
{
    // handle flow rejection by Aperture Agent
    flow.SetStatus(FlowStatus.Error);
    SimpleHandlePath(flow.GetRejectionHttpStatusCode(), "REJECTED!", response);
}

var endResponse = flow.End();
if (endResponse.Error != null)
{
    // handle end failure
    log.Error("Failed to end flow: {e}", endResponse.Error);
}
else if (endResponse.FlowEndResponse != null)
{
    // handle end success
    log.Info("Ended flow with response: " + endResponse.FlowEndResponse.ToString());
}

The above code snippet is making StartFlow calls to Aperture. For this call, it is important to specify the control point (featureName 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.