RequestGuard 1.0.4
dotnet add package RequestGuard --version 1.0.4
NuGet\Install-Package RequestGuard -Version 1.0.4
<PackageReference Include="RequestGuard" Version="1.0.4" />
<PackageVersion Include="RequestGuard" Version="1.0.4" />
<PackageReference Include="RequestGuard" />
paket add RequestGuard --version 1.0.4
#r "nuget: RequestGuard, 1.0.4"
#:package RequestGuard@1.0.4
#addin nuget:?package=RequestGuard&version=1.0.4
#tool nuget:?package=RequestGuard&version=1.0.4
RequestGuard
Rate Limiting, Throttling, and Concurrency Management for Your API in a Flexible Way!
RequestGuard is a powerful package that provides multiple options to handle rate limiting, concurrency, and throttling in a flexible and efficient way. This package works seamlessly with Redis, ensuring that it can be used across multiple instances without relying on in-memory solutions.
Whether you're handling high traffic or ensuring fair usage, RequestGuard gives you the tools to manage these challenges effortlessly.
🚀 Features
- Redis-backed: Works with Redis, making it suitable for distributed environments and multiple instances.
- Flexible Options: Apply rate limiting, throttling, and concurrency handling globally, per endpoint, or using attributes.
- Multitenant Support: Automatically handles rate limiting and throttling per tenant based on request headers.
- Customizable Responses: Easily configure response messages and behavior for each of the options.
🛠 How to Use
How to Use RequestGuard
To get started with the RequestGuard package in your ASP.NET Core application, follow the steps below:
Step 1: Add the Service to the Container
In your Program.cs (or Startup.cs for older versions), you need to register the services from the RequestGuard package by calling AddRequestGuards. This will bind all your configuration options from appsettings.json and register necessary services.
builder.Services.AddRequestGuards(builder.Configuration); // Registers all the options and services from the package
Step 2: Register the Middleware You Want to Use
Next, decide which type of guard you want to apply and register it within your middleware pipeline. You can choose between Concurrency, Rate Limiting, and Throttling , or all of them.
1 - To apply concurrency control:
app.UseConcurrencyGuard(); // Adds the concurrency control middleware
2 - To apply rate limiting
app.UseRateLimitGuard();
3 - To apply throttling
app.UseThrottlingGuard();
You can configure RequestGuard in your project by adding the following settings to your appsettings.json file:
appsettings.json Example
{
"RequestGuard": {
"RedisOptions": {
"URL": "localhost:6379",
"Prefix": "my-api"
},
"RateLimitOptions": {
"TimeFrameMs": 1000,
"MaxRequests": 1,
"ResponseMessage": "Too many requests, please try again later",
"UseJsonResponse": true,
"Global": true,
"Endpoints": [
{
"Path": "/api/someendpoint",
"TimeFrameMs": 5000,
"MaxRequests":5
},
{
"Path": "/api/anotherendpoint",
"TimeFrameMs": 3000,
"MaxRequests":5
}
]
},
"ThrottlingOptions": {
"TimeFrameMs": 1000,
"ResponseMessage": "Too many requests, Please Try again later",
"UseJsonResponse": false,
"Global": true,
"Endpoints": [
{
"Path": "/api/someendpoint",
"TimeFrameMs": 5000
},
{
"Path": "/api/anotherendpoint",
"TimeFrameMs": 3000
}
]
},
"ConcurrencyOptions": {
"TimeFrameMs": 1000,
"ResponseMessage": "Concurrent request detected customized",
"UseJsonResponse": true,
"Global": true,
"Endpoints": [
{
"Path": "/api/someendpoint",
"TimeFrameMs": 5000
},
{
"Path": "/api/anotherendpoint",
"TimeFrameMs": 3000
}
]
},
"MultiTenantOptions": {
"MultiTenant": true,
"MultiTenantHeader": "Tenant-ID"
}
}
}
Explanation of Options
RedisOptions: Configure the Redis connection settings.
- URL: The Redis server address.
- Prefix: A custom prefix to avoid naming collisions in Redis.
RateLimitOptions: Define rate limiting settings.
- TimeFrameMs: The time frame for rate limiting (in milliseconds).
- MaxRequests: The maximum number of requests allowed within the time frame.
- ResponseMessage: The message returned when rate limit is exceeded.
- UseJsonResponse: If true, returns a JSON response on rate limit exceeded.
- Global: If true, applies the rate limit globally.
- Endpoints: Specific rate limits for individual endpoints.
ThrottlingOptions: Define throttling settings.
- TimeFrameMs: The time frame for throttling (in milliseconds).
- ResponseMessage: The message returned when throttling is triggered.
- UseJsonResponse: If true, returns a JSON response on throttling.
- Global: If true, applies the throttling globally.
- Endpoints: Specific throttling settings for individual endpoints.
ConcurrencyOptions: Define concurrency settings.
- TimeFrameMs: The time frame for concurrency control (in milliseconds).
- ResponseMessage: The message returned when concurrency is detected.
- UseJsonResponse: If true, returns a JSON response on concurrency.
- Global: If true, applies concurrency control globally.
- Endpoints: Specific concurrency settings for individual endpoints.
MultiTenantOptions: Enables multitenant support.
- MultiTenant: Set to true to enable multitenancy.
- MultiTenantHeader: The header name that contains the tenant ID (e.g.,
Tenant-ID).
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- Microsoft.AspNetCore.Http.Abstractions (>= 2.3.0)
- Microsoft.AspNetCore.Mvc.Core (>= 2.3.0)
- Microsoft.Extensions.Caching.StackExchangeRedis (>= 9.0.2)
- Microsoft.Extensions.Configuration (>= 9.0.2)
- Microsoft.Extensions.Configuration.Binder (>= 9.0.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.