Table of Contents

Class PercentageThrottlingAlgorithm

Namespace
RateLimitHeaders.Throttling
Assembly
RateLimitHeaders.dll

A simple percentage-based throttling algorithm that begins throttling when the remaining quota falls below a configurable threshold.

public sealed class PercentageThrottlingAlgorithm : IThrottlingAlgorithm
Inheritance
PercentageThrottlingAlgorithm
Implements
Inherited Members

Examples

Using with default settings:

services.AddHttpClient("MyApi")
    .AddRateLimitAwareHandler(options =>
    {
        options.ThrottlingAlgorithm = new PercentageThrottlingAlgorithm();
    });

Using with custom settings (more aggressive throttling):

var algorithm = new PercentageThrottlingAlgorithm(
    threshold: 0.2,              // Start throttling at 20% remaining
    factor: 2.0,                 // Double the calculated delay
    maxDelay: TimeSpan.FromSeconds(10)); // Cap at 10 seconds

services.AddHttpClient("MyApi")
    .AddRateLimitAwareHandler(options =>
    {
        options.ThrottlingAlgorithm = algorithm;
    });

Remarks

This algorithm calculates delay using the formula:

if (remaining / quota < threshold) {
    delay = (threshold - remainingPct) * resetSeconds * factor
    delay = min(delay, maxDelay)
}

Example with default settings (10% threshold, 1.0 factor, 5s max):

  • At 8% remaining with 60s reset: delay = (0.10 - 0.08) * 60 * 1.0 = 1.2s
  • At 5% remaining with 60s reset: delay = (0.10 - 0.05) * 60 * 1.0 = 3.0s
  • At 2% remaining with 60s reset: delay = (0.10 - 0.02) * 60 * 1.0 = 4.8s

Constructors

PercentageThrottlingAlgorithm()

Initializes a new instance with default settings.

public PercentageThrottlingAlgorithm()

PercentageThrottlingAlgorithm(double, double, TimeSpan)

Initializes a new instance with the specified settings.

public PercentageThrottlingAlgorithm(double threshold, double factor, TimeSpan maxDelay)

Parameters

threshold double

The quota percentage threshold below which throttling begins (0.0 to 1.0). Default is 0.1 (10%).

factor double

A multiplier applied to the calculated delay. Higher values result in more aggressive throttling. Default is 1.0.

maxDelay TimeSpan

The maximum delay that can be applied. Prevents excessive delays when quota is nearly exhausted. Default is 5 seconds.

Exceptions

ArgumentOutOfRangeException

Thrown when threshold is not between 0.0 and 1.0, factor is negative, or maxDelay is negative.

Fields

DefaultFactor

The default delay factor.

public const double DefaultFactor = 1

Field Value

double

DefaultMaxDelay

The default maximum delay (5 seconds).

public static readonly TimeSpan DefaultMaxDelay

Field Value

TimeSpan

DefaultThreshold

The default threshold (10% of quota remaining).

public const double DefaultThreshold = 0.1

Field Value

double

Properties

Factor

Gets the delay factor multiplier.

public double Factor { get; }

Property Value

double

MaxDelay

Gets the maximum delay that can be applied.

public TimeSpan MaxDelay { get; }

Property Value

TimeSpan

Threshold

Gets the threshold below which throttling begins.

public double Threshold { get; }

Property Value

double

Methods

Evaluate(RateLimitInfo)

Evaluates the current rate limit state and determines whether throttling is needed.

public ThrottlingResult Evaluate(RateLimitInfo rateLimitInfo)

Parameters

rateLimitInfo RateLimitInfo

The parsed rate limit information from the most recent response.

Returns

ThrottlingResult

A ThrottlingResult indicating whether to throttle and for how long.