Skip to main content
The Polly.RateLimiting package provides rate limiting capabilities using the .NET System.Threading.RateLimiting library, allowing you to control the rate of executions through your resilience pipelines.

Installation

Extension Methods

AddRateLimiter

Adds a rate limiter to the resilience pipeline.
TBuilder
required
The builder instance
RateLimiterStrategyOptions
required
The rate limiter strategy options
Returns: The builder instance with the rate limiter added. Example:

AddRateLimiter (with RateLimiter instance)

Adds a rate limiter instance directly to the pipeline.
TBuilder
required
The builder instance
RateLimiter
required
The rate limiter to use
Example:

AddConcurrencyLimiter

Adds a concurrency limiter to the pipeline.
TBuilder
required
The builder instance
int
required
Maximum number of permits that can be leased concurrently
int
default:"0"
Maximum number of permits that can be queued concurrently
Example:

AddConcurrencyLimiter (with options)

Adds a concurrency limiter with detailed options.
TBuilder
required
The builder instance
ConcurrencyLimiterOptions
required
The concurrency limiter options
Example:

Configuration Classes

RateLimiterStrategyOptions

Options for configuring the rate limiter strategy.

Properties

RateLimiter A delegate that produces a RateLimitLease. If null, the strategy will use a ConcurrencyLimiter created with DefaultRateLimiterOptions.
  • Type: Func<RateLimiterArguments, ValueTask<RateLimitLease>>?
  • Default: null
DefaultRateLimiterOptions Options for the default concurrency limiter used when RateLimiter is null.
  • Type: ConcurrencyLimiterOptions
  • Default: PermitLimit = 1000, QueueLimit = 0
OnRejected An event raised when execution is rejected by the rate limiter.
  • Type: Func<OnRateLimiterRejectedArguments, ValueTask>?
  • Default: null
Example:

RateLimiterArguments

Arguments passed to the rate limiter delegate.
Example:

OnRateLimiterRejectedArguments

Arguments passed to the OnRejected callback.

Properties

Context The resilience context associated with the execution.
  • Type: ResilienceContext
Lease The lease that was rejected by the rate limiter (has no permits).
  • Type: RateLimitLease
Example:

Exceptions

RateLimiterRejectedException

Exception thrown when a rate limiter rejects an execution.

Properties

RetryAfter The amount of time to wait before retrying again. Retrieved from the RateLimitLease metadata.
  • Type: TimeSpan?
  • Default: null

Constructors

Example:

Usage Examples

Basic Concurrency Limiting

Custom Rate Limiter

Rate Limiting with Rejection Handling

Per-User Rate Limiting

Combining with Dependency Injection

See Also