Skip to content

UUF: Rate limiting: Multiple limiters, one policy: add example #36043

@wadepickett

Description

@wadepickett

Description

The following user quote is Transferred from UUF system:

"how to use two limiters in one policy for the same endpoint"

Problem
The article currently explains how to create chained limiters with PartitionedRateLimiter.CreateChained(), but doesn't provide a concrete example showing how to define and apply a policy that combines multiple limiter types (e.g., token bucket + concurrency) to a single endpoint.

[AI-assisted proposed solution follows, review carefully]

Proposed solution: Add example for using multiple limiters in one policy for the same endpoint
Add a small example in the "Create chained limiters" section showing a practical implementation. Something like this:

// Define a policy combining token bucket and concurrency limiters
builder.Services.AddRateLimiter(options =>
{
    options.AddPolicy("combined", httpContext =>
    {
        // Get user identity for partitioning
        string user = httpContext.User.Identity?.Name ?? "anonymous";
        
        // Create chained limiters (token bucket + concurrency)
        return PartitionedRateLimiter.CreateChained(
            // Token bucket limiter (rate over time)
            PartitionedRateLimiter.Create<HttpContext, string>(httpContext =>
                RateLimitPartition.GetTokenBucketLimiter(
                    partitionKey: user,
                    factory: _ => new TokenBucketRateLimiterOptions
                    {
                        TokenLimit = 100,
                        QueueProcessingOrder = QueueProcessingOrder.OldestFirst,
                        QueueLimit = 5,
                        ReplenishmentPeriod = TimeSpan.FromSeconds(10),
                        TokensPerPeriod = 10,
                        AutoReplenishment = true
                    })),
            
            // Concurrency limiter (simultaneous requests)
            PartitionedRateLimiter.Create<HttpContext, string>(httpContext =>
                RateLimitPartition.GetConcurrencyLimiter(
                    partitionKey: user,
                    factory: _ => new ConcurrencyLimiterOptions
                    {
                        PermitLimit = 5,
                        QueueProcessingOrder = QueueProcessingOrder.OldestFirst,
                        QueueLimit = 2
                    }))
        );
    });
});

// Apply the combined policy to an endpoint
app.MapGet("/api/resource", () => "This endpoint uses multiple limiters")
   .RequireRateLimiting("combined");_

Page URL

https://learn.microsoft.com/en-us/aspnet/core/performance/rate-limit?view=aspnetcore-9.0

Content source URL

https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/performance/rate-limit.md

Document ID

bb96b20f-0991-d6de-4d98-192cd469460f

Platform Id

a32d8e13-92af-9a53-cfa1-45862b1df67c

Article author

@wadepickett

Metadata

  • ID: bb96b20f-0991-d6de-4d98-192cd469460f
  • PlatformId: a32d8e13-92af-9a53-cfa1-45862b1df67c
  • Service: aspnet-core
  • Sub-service: performance

Related Issues

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions