Skip to content

jamesgober/dotnet-rate-limiter

Repository files navigation

dotnet-rate-limiter

NuGet Downloads License CI


High-performance rate limiting for .NET APIs. Supports token bucket, sliding window, fixed window, and concurrency limiting — with per-client and per-endpoint policies, pluggable storage for distributed state, and ASP.NET Core middleware integration.

Features

  • Token Bucket — Smooth rate limiting with configurable burst capacity and refill rate
  • Sliding Window — Accurate request counting over rolling time windows
  • Fixed Window — Simple time-sliced rate limiting with automatic reset
  • Concurrency Limiter — Cap simultaneous in-flight requests per client or endpoint
  • Per-Client Policies — Rate limit by IP, API key, user ID, or custom partition key
  • Per-Endpoint Policies — Different limits for different routes via [RateLimit] / [DisableRateLimiting]
  • Extensible Storage — In-memory by default; implement IRateLimitStore for Redis or other backends
  • Retry-After Headers — Automatic Retry-After and X-RateLimit-* response headers
  • Middleware — Drop-in ASP.NET Core middleware with services.AddRateLimiting()

Installation

dotnet add package JG.RateLimiter

Quick Start

using JG.RateLimiter;
using JG.RateLimiter.Policies;

builder.Services.AddRateLimiting(options =>
{
    options.AddPolicy("api", new TokenBucketPolicy
    {
        PermitLimit = 100,
        Window = TimeSpan.FromMinutes(1),
        BurstLimit = 20,
        PartitionBy = context => context.Connection.RemoteIpAddress?.ToString()
    });
});

app.UseRouting();
app.UseRateLimiting();
app.MapControllers();

Documentation

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

Licensed under the Apache License 2.0. See LICENSE for details.


Ready to get started? Install via NuGet and check out the API reference.

About

Rate limiting middleware for ASP.NET Core. Token bucket, sliding window, and fixed window algorithms. Per-client and per-endpoint policies with automatic headers.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages