Skip to content

Rate Limiter

Rate limiting configuration to control the number of requests from clients. Apply policies to endpoints using the rate_limiter_policy annotation.

Overview

json
{
  "RateLimiterOptions": {
    "Enabled": false,
    "StatusCode": 429,
    "StatusMessage": "Too many requests. Please try again later.",
    "DefaultPolicy": null,
    "Policies": []
  }
}

Settings Reference

SettingTypeDefaultDescription
EnabledboolfalseEnable rate limiting.
StatusCodeint429HTTP status code returned when rate limit is exceeded.
StatusMessagestring"Too many requests. Please try again later."Response message when rate limit is exceeded.
DefaultPolicystringnullName of the default policy to apply to all endpoints.
Policiesarray[]List of rate limiting policies. Assign a policy to an endpoint using the rate_limiter_policy annotation.

Policy Types

Four policy types are available:

  • FixedWindow - Fixed time window rate limiting
  • SlidingWindow - Sliding time window rate limiting
  • TokenBucket - Token bucket algorithm
  • Concurrency - Concurrent request limiting

Fixed Window Policy

Limits requests within fixed time intervals.

json
{
  "Type": "FixedWindow",
  "Enabled": true,
  "Name": "fixed",
  "PermitLimit": 100,
  "WindowSeconds": 60,
  "QueueLimit": 10,
  "AutoReplenishment": true
}
SettingTypeDefaultDescription
Typestring-Must be "FixedWindow".
EnabledboolfalseEnable this policy.
Namestring-Policy name. Use this name with the rate_limiter_policy annotation to apply this policy to an endpoint.
PermitLimitint100Maximum requests allowed per window.
WindowSecondsint60Window duration in seconds.
QueueLimitint10Maximum queued requests when limit is reached.
AutoReplenishmentbooltrueAutomatically replenish permits.

See Fixed Window Limiter documentation.

Sliding Window Policy

Limits requests using a sliding time window with segments.

json
{
  "Type": "SlidingWindow",
  "Enabled": true,
  "Name": "sliding",
  "PermitLimit": 100,
  "WindowSeconds": 60,
  "SegmentsPerWindow": 6,
  "QueueLimit": 10,
  "AutoReplenishment": true
}
SettingTypeDefaultDescription
Typestring-Must be "SlidingWindow".
EnabledboolfalseEnable this policy.
Namestring-Policy name. Use this name with the rate_limiter_policy annotation to apply this policy to an endpoint.
PermitLimitint100Maximum requests allowed per window.
WindowSecondsint60Window duration in seconds.
SegmentsPerWindowint6Number of segments dividing the window.
QueueLimitint10Maximum queued requests when limit is reached.
AutoReplenishmentbooltrueAutomatically replenish permits.

See Sliding Window Limiter documentation.

Token Bucket Policy

Limits requests using the token bucket algorithm.

json
{
  "Type": "TokenBucket",
  "Enabled": true,
  "Name": "bucket",
  "TokenLimit": 100,
  "ReplenishmentPeriodSeconds": 10,
  "QueueLimit": 10,
  "AutoReplenishment": true
}
SettingTypeDefaultDescription
Typestring-Must be "TokenBucket".
EnabledboolfalseEnable this policy.
Namestring-Policy name. Use this name with the rate_limiter_policy annotation to apply this policy to an endpoint.
TokenLimitint100Maximum tokens in the bucket.
ReplenishmentPeriodSecondsint10How often tokens are added to the bucket.
QueueLimitint10Maximum queued requests when limit is reached.
AutoReplenishmentbooltrueAutomatically replenish tokens.

See Token Bucket Limiter documentation.

Concurrency Policy

Limits the number of concurrent requests.

json
{
  "Type": "Concurrency",
  "Enabled": true,
  "Name": "concurrency",
  "PermitLimit": 10,
  "QueueLimit": 5,
  "OldestFirst": true
}
SettingTypeDefaultDescription
Typestring-Must be "Concurrency".
EnabledboolfalseEnable this policy.
Namestring-Policy name. Use this name with the rate_limiter_policy annotation to apply this policy to an endpoint.
PermitLimitint10Maximum concurrent requests.
QueueLimitint5Maximum queued requests when limit is reached.
OldestFirstbooltrueProcess queued requests oldest first.

See Concurrency Limiter documentation.

Complete Example

Configuration with multiple policies:

json
{
  "RateLimiterOptions": {
    "Enabled": true,
    "StatusCode": 429,
    "StatusMessage": "Too many requests. Please try again later.",
    "DefaultPolicy": "bucket",
    "Policies": [
      {
        "Type": "FixedWindow",
        "Enabled": true,
        "Name": "fixed",
        "PermitLimit": 100,
        "WindowSeconds": 60,
        "QueueLimit": 10,
        "AutoReplenishment": true
      },
      {
        "Type": "SlidingWindow",
        "Enabled": true,
        "Name": "sliding",
        "PermitLimit": 100,
        "WindowSeconds": 60,
        "SegmentsPerWindow": 6,
        "QueueLimit": 10,
        "AutoReplenishment": true
      },
      {
        "Type": "TokenBucket",
        "Enabled": true,
        "Name": "bucket",
        "TokenLimit": 100,
        "ReplenishmentPeriodSeconds": 10,
        "QueueLimit": 10,
        "AutoReplenishment": true
      },
      {
        "Type": "Concurrency",
        "Enabled": true,
        "Name": "concurrency",
        "PermitLimit": 10,
        "QueueLimit": 5,
        "OldestFirst": true
      }
    ]
  }
}

Next Steps

  • Server & SSL - Configure HTTPS and Kestrel web server
  • CORS - Configure Cross-Origin Resource Sharing

Released under the MIT License.