Table of Contents

Class RateLimitPolicy

Namespace
LMKit.Agents.Resilience
Assembly
LM-Kit.NET.dll

A resilience policy that limits the rate of operations.

Uses a token bucket algorithm to enforce rate limits, allowing bursts while maintaining an average rate.

public sealed class RateLimitPolicy : IResiliencePolicy
Inheritance
RateLimitPolicy
Implements
Inherited Members
Extension Methods

Examples

var rateLimit = new RateLimitPolicy(
    permitsPerSecond: 10,
    maxBurst: 20);

foreach (var input in inputs)
{
    var result = await rateLimit.ExecuteAsync(ct => agent.RunAsync(input, ct));
}

Constructors

RateLimitPolicy(double, int)

Initializes a new instance of the RateLimitPolicy class.

Properties

AvailablePermits

Gets the current available permits.

MaxBurst

Gets the maximum burst size.

Name

Gets the name of this policy.

PermitsPerSecond

Gets the permits per second.

Methods

ExecuteAsync<T>(Func<ResilienceContext, CancellationToken, Task<T>>, ResilienceContext, CancellationToken)

Executes an action with this policy and context.

ExecuteAsync<T>(Func<CancellationToken, Task<T>>, CancellationToken)

Executes an action with this policy applied.

GetEstimatedWaitTime()

Gets the estimated wait time for the next permit.

OnRateLimited(Action<TimeSpan>)

Sets a callback for when rate limiting occurs.

TryAcquire()

Tries to acquire a permit without waiting.

WithMaxWait(TimeSpan)

Sets the maximum time to wait for a permit.