Mohammad Neamul Islam
Senior Software Engineer
Bangla
English
Hindi
  • ASP .NET CORE
  • SQL & MYSQL & PostgreSQL & LinQ
  • Angular
  • C#, ASP.Net MVC , Entityframework
  • GIT, SVN
  • WebAPI, SignalR, Autofac, Hangfire
  • Amazon EC2, Amazon S3, Amazon RDS, Amazon IAM, Amazon VPC

Rate limiting middleware in ASP.NET Core

ASP .NET CORE

.NET 7 provides a rate limiter middleware for setting rate limiting policies. Web apps can configure these policies in Program.cs and then use them with API endpoints.

There are a several different rate limiter options, including:

  1. Fixed window
  2. Sliding window
  3. Token bucket
  4. Concurrency

Looking at the fixed window option in more detail, it uses a fixed amount of time to limit requests. Once the time has elapsed, the request limit is reset.

In the example below, we've created a TestPolicy fixed window limiter. The policy permits one request every 12 seconds.

We've also set a queue limit of 3, meaning that 3 requests will be left hanging until they can be processed. If more than 3 requests are queued, subsequent requests return a 503 Service Unavailable error.

To use this policy with an API endpoint, the EnableRateLimiting attribute can be assigned to it. It expects the policy name as a parameter of that attribute. As the policy is called TestPolicy, that's the parameter that we will pass into the attribute.

 

The [EnableRateLimiting] and [DisableRateLimiting] attributes can be applied to a Controller, action method, or Razor Page.

 

The [DisableRateLimiting] attribute disables rate limiting to the Controller, action method, or Razor Page regardless of named rate limiters or global limiters applied.

 

 

  • Publish Date:
    Dec. 25, 2022
  • Author:
  • Category:
    ASP .NET CORE

Similar posts

blog post
Rate limiting middleware in ASP.NET Core
.NET 7 provides a rate limiter middleware for setting rate limiting policies. Web apps can configure these policies in Program.cs and then use them with API endpoints.
Read more
blog post
Why is ASP.NET Core 11x faster than Node.js?
There are 1000 reasons why, but in short: .NET 7 got about 1000 performance-impacting PRs across the runtime, core libraries, ASP.NET Core, Windows Forms, Entity Framework and beyond. Here's a quick summary of what got improved: 𝗝𝗜𝗧 𝗖𝗼𝗺𝗽𝗶𝗹𝗲𝗿: Superior assembly code dumping, inlining, and vectorization. 𝗚𝗮𝗿𝗯𝗮𝗴𝗲 𝗖𝗼𝗹𝗹𝗲𝗰𝘁𝗼𝗿: Enhanced memory management with reduced fragmentation. 𝗡𝗮𝘁𝗶𝘃𝗲 𝗔𝗢𝗧: Improved startup time & reduced memory usage.
Read more
blog post
.𝐍𝐄𝐓 8 𝐃𝐚𝐭𝐚𝐀𝐧𝐧𝐨𝐭𝐚𝐭𝐢𝐨𝐧
.𝐍𝐄𝐓 8 𝐃𝐚𝐭𝐚𝐀𝐧𝐧𝐨𝐭𝐚𝐭𝐢𝐨𝐧 𝐚𝐭𝐭𝐫𝐢𝐛𝐮𝐭𝐞𝐬. Data annotations are basically used for model validation. These are the attributes that can be applied to the members of the class to specify some validation rules. In this blog, we will discuss one of the enhancements in data annotation for the class member validation in .NET 8.
Read more
© 2024 Mohammad Neamul Islam

Mohammad Neamul Islam