Rate limiting library for Azure Functions
$ dotnet add package ThrottlingTroll.AzureFunctionsRate limiting/throttling middleware for Azure Functions. Can be used both in InProc and Isolated projects, both with "classic" HTTP-triggered Functions and with ASP.Net Core Integration.
Install from NuGet:
dotnet add package ThrottlingTroll.AzureFunctions
Make sure you call one or another form of .UseThrottlingTroll() method at startup:
var builder = new HostBuilder();
// .....
builder.ConfigureFunctionsWebApplication((builderContext, workerAppBuilder) => {
workerAppBuilder.UseThrottlingTroll();
// .....
});
.AddThrottlingTroll() method to add ThrottlingTroll to your DI container at startup:[assembly: WebJobsStartup(typeof(ThrottlingTrollSampleInProcFunction.Startup.Startup))]
namespace ThrottlingTrollSampleInProcFunction.Startup
{
public class Startup : IWebJobsStartup
{
public void Configure(IWebJobsBuilder builder)
{
builder.Services.AddThrottlingTroll(options =>
{
// In InProc Functions config sections loaded from host.json have the "AzureFunctionsJobHost:" prefix in their names
options.ConfigSectionName = "AzureFunctionsJobHost:ThrottlingTrollIngress";
// .....
});
}
}
}
Add InProc-specific implementation of IHttpRequestProxy to your project.
Wrap your Functions with .WithThrottlingTroll() method, like this:
public class MyFunctions
{
private readonly IThrottlingTroll _thtr;
public Functions(IThrottlingTroll thtr)
{
this._thtr = thtr;
}
[FunctionName("MyFunc")]
public Task<IActionResult> MyFunc([HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequest req)
=> this._thtr.WithThrottlingTroll(new InProcHttpRequestProxy(req),
async ctx =>
{
// Your code goes here ...
return (IActionResult)new OkObjectResult("OK");
},
async ctx =>
{
return (IActionResult)new StatusCodeResult((int)HttpStatusCode.TooManyRequests);
});
}
Sample Azure Functions projects (both InProc and Isolated) are located in this separate repo.