{"id":23034271,"url":"https://github.com/rnelson/aspnetcore-slow-down","last_synced_at":"2025-04-02T22:20:33.114Z","repository":{"id":266926981,"uuid":"891649612","full_name":"rnelson/aspnetcore-slow-down","owner":"rnelson","description":null,"archived":false,"fork":false,"pushed_at":"2025-03-12T02:28:25.000Z","size":1319,"stargazers_count":0,"open_issues_count":3,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-12T03:27:36.731Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://github.com/nearform/aspnetcore-slow-down","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rnelson.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-11-20T17:52:23.000Z","updated_at":"2025-03-12T02:26:15.000Z","dependencies_parsed_at":"2024-12-23T02:30:12.979Z","dependency_job_id":"f4bd108a-58d5-4d02-8d6c-f8f55314c093","html_url":"https://github.com/rnelson/aspnetcore-slow-down","commit_stats":null,"previous_names":["rnelson/aspnetcore-slow-down"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rnelson%2Faspnetcore-slow-down","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rnelson%2Faspnetcore-slow-down/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rnelson%2Faspnetcore-slow-down/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rnelson%2Faspnetcore-slow-down/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rnelson","download_url":"https://codeload.github.com/rnelson/aspnetcore-slow-down/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246900253,"owners_count":20852017,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-15T16:31:05.778Z","updated_at":"2025-04-02T22:20:33.092Z","avatar_url":"https://github.com/rnelson.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SlowDown Middleware\n\n![Build status](https://github.com/rnelson/aspnetcore-slow-down/actions/workflows/dotnet.yml/badge.svg) ![License](https://img.shields.io/github/license/rnelson/aspnetcore-slow-down)\n\nA slow-down middleware for ASP.NET Core.\n\n## Installation\n\n**This package is not yet available on NuGet. When it is, you will be able to install the package with the following:**\n\n```bash\ndotnet add package Libexec.AspNetCore.SlowDown\n```\n\n## Usage\n\n### Adding the middleware\n\n```csharp\nusing Libexec.AspNetCore.SlowDown;\n\nvar builder = WebApplication.CreateBuilder(args);\n\n// Add HybridCache.\n#pragma warning disable EXTEXP0018\nbuilder.Services.AddHybridCache();\n#pragma warning restore EXTEXP0018\n\n/* Add your other services */\n\n// Add SlowDown middleware to the DI container.\nbuilder.Services.AddSlowDown();\n\nvar app = builder.Build();\n\n/* Add your other configuration */\n\n// Enable the middleware.\napp.UseSlowDown();\n\napp.Run();\n```\n\nThe response will have some additional headers:\n\n| Header                  | Description                                                                                      |\n| ----------------------- |--------------------------------------------------------------------------------------------------|\n| `x-slow-down-limit`     | How many requests in total the client can make until the server starts to slow down the response |\n| `x-slow-down-remaining` | How many requests remain to the client in the `TimeWindow`                                       |\n| `x-slow-down-delay`     | How much delay (in milliseconds) has been applied to this request                                |\n\n## Configuration\n\n| Name                     | Type     | Default Value   | Description                                                                                                                                                                                                                                    |\n|--------------------------|----------|-----------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `SlowDownEnabled`        | bool     | `true`          | Flag to enable or disable the middleware.                                                                                                                                                                                                      |\n| `Delay`                  | int      | `1000`          | Base unit of time delay applied to requests. It is expressed in milliseconds. Set to `0` to disable delaying.                                                                                                                                  |\n| `DelayAfter`             | int      | `5`             | Number of requests received during `TimeWindow` before starting to delay responses. Set to `0` to disable delaying.                                                                                                                            |\n| `MaxDelay`               | int      | `int.MaxValue`  | The maximum value of delay that a request has after many consecutive attempts. It is an important option for the server when it is running behind a load balancer or reverse proxy, and has a request timeout. Set to `0` to disable delaying. |\n| `TimeWindow`             | int      | `30000`         | The duration of the time window during which request counts are kept in memory. It is expressed in milliseconds. Set to `0` to disable delaying.                                                                                               |\n| `AddHeaders`             | bool     | `true`          | Flag to add custom headers `x-slow-down-limit`, `x-slow-down-remaining`, `x-slow-down-delay` for all server responses.                                                                                                                         |\n| `KeyGenerator`           | delegate | (req) =\u003e req.ip | Function used to generate keys to uniquely identify requests coming from the same user                                                                                                                                                         |\n| `OnLimitReached`         | delegate | `null`          | Function that gets called the first time the limit is reached within `TimeWindow`.                                                                                                                                                             |\n| `SkipFailedRequests`     | bool     | `false`         | When `true`, failed requests (status \u003e= 400) won't be counted.                                                                                                                                                                                 |\n| `SkipSuccessfulRequests` | bool     | `false`         | When `true`, successful requests (status \u003c 400) won't be counted.                                                                                                                                                                              |\n| `Skip`                   | delegate | `null`          | Function used to skip requests. Returning `true` from the function will skip limiting for that request.                                                                                                                                        |\n| `CacheTimeout`           | int      | `5000`          | Timeout, in ms, for cache lookups. If exceeded, the request is not delayed.                                                                                                                                                                    |\n\n## Configuration examples\n\n### Configuring in code\n\nYou can configure the middleware as part of the `AddSlowDown()` call. As delegates cannot be configured in JSON, this is how you'll customize any of the delegates.\n\n```csharp\n// Add SlowDown middleware to the DI container.\nbuilder.Services.AddSlowDown(options =\u003e\n{\n    options.OnLimitReached = request =\u003e\n    {\n        // When we're limited, add a silly header into the response.\n        request.HttpContext.Response.Headers[\"X-SlowDown-OnLimitReached\"] = \"Hi!\";\n    };\n    \n    // Change the `DelayAfter` value to 6 requests\n    options.DelayAfter = 6;\n});\n```\n\n### Configuring in `appsettings.json`\n\nBoolean and numeric values can be configured in your `appsettings.json` file, nested underneath a `SlowDown` section. For example:\n\n```json\n{\n  \"SlowDown\": {\n    \"Delay\": 5000,\n    \"DelayAfter\": 50,\n    \"MaxDelay\": 60000,\n    \"TimeWindow\": 30000\n  }\n}\n```\n\n## Example\n\nA delay specified via the `Delay` option will be applied to requests coming from the same IP address (by default) when more than `DelayAfter` requests are received within the time specified in the `TimeWindow` option.\n\nConsider the following configuration:\n\n+ `Delay`: `10000` (10s)\n+ `DelayAfter`: `10`\n+ `MaxDelay`: `100000` (100s)\n\nThe following is an example of hitting an API for 10 minutes the result of hitting the API will look like:\n\n- 1st request - no delay\n- 2nd request - no delay\n- 3rd request - no delay\n- `...`\n- 10th request - no delay\n- 11th request - 10 seconds delay\n- 12th request - 20 seconds delay\n- 13th request - 30 seconds delay\n- `...`\n- 20th request - 100 seconds delay\n- 21st request - 100 seconds delay\\*\n\nAfter 10 minutes without hitting the API the results will be:\n\n- 21st request - no delay\n- 22nd request - no delay\n- `...`\n- 30th request - no delay\n- 31st request - 10 seconds delay\n\nDelay remains the same because the value of `MaxDelay` option is `100000`.\n\n## License\n\n`Libexec.AspNetCore.SlowDown` is released under the MIT license. Contains code written by Nearform Ltd. released under the MIT license.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frnelson%2Faspnetcore-slow-down","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frnelson%2Faspnetcore-slow-down","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frnelson%2Faspnetcore-slow-down/lists"}