Skip to content

david-acker/ThrottlingSample

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Download Throttling Sample

Middleware for throttling download speeds in ASP.NET Core applications

Table of Contents

Documentation

EnableDownloadThrottingAttribute

Enables download throttling for the target controllers or endpoints.

Parameters:

  • bytesPerSecond (int): The maximum allowed download speed in bytes per second.

Example:

[EnableDownloadThrottling(2_000_000)]
public async Task<IActionResult> GetWithThrottling()

DisableDownloadThrottingAttribute

Disables download throttling for the target controllers or endpoints.

Parameters: None

Example:

[DisableDownloadThrottling]
public async Task<IActionResult> GetWithoutThrottling()

UseDownloadThrottling

Adds middleware for throttling download speeds.

Parameters:

  • builder (IApplicationBuilder)

Example:

var builder = WebApplication.CreateBuilder(args);

var app = builder.Build();
app.UseDownloadThrottling();

Sample Application

Usage

Navigate to the sample directory and run dotnet run to start the sample application.

Testing

Optionally, run ./client-test.sh to test the download throttling functionality, after starting the sample application. If necessary, add execute permissions using chmod +x client-test.sh.

The test script uses curl to make several requests to the GET /minimal/throttled endpoint and prints the average download speed of each request (in bytes per second) to the console. The actual download speeds average 99,000 bytes (0.099 MBs) per second, and do not exceed the enforced limit of 0.1 MBs per second.

Endpoints

The sample includes the following endpoints, all of which return the sample CSV file included with the project.

Minimal

Throttled:

  • GET /minimal/throttled: limited to 0.1 MBs per second

Not Throttled:

  • GET /minimal/not-throttled

Controller

Throttled:

  • GET /controller/throttled: limited to 0.1 MBs per second
  • GET /controller/throttled-override: limited to 0.2 MBs per second

Not Throttled:

  • GET /controller/not-throttled

Future Enhancements

  • Add tests for the DownloadThrottling project.
  • Allow the download rate limits to be configured via appsettings.json.
  • Add PowerShell version of client-test.sh.