https://github.com/soenneker/soenneker.utils.ratelimiting.executor
A thread-safe utility designed to manage the rate at which tasks are executed, ensuring that they are not run more frequently than a specified interval.
https://github.com/soenneker/soenneker.utils.ratelimiting.executor
api csharp dotnet executor limit limiting rate ratelimiting ratelimitingexecutor util utils
Last synced: 28 days ago
JSON representation
A thread-safe utility designed to manage the rate at which tasks are executed, ensuring that they are not run more frequently than a specified interval.
- Host: GitHub
- URL: https://github.com/soenneker/soenneker.utils.ratelimiting.executor
- Owner: soenneker
- License: mit
- Created: 2024-10-22T01:35:10.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2026-04-14T20:11:07.000Z (3 months ago)
- Last Synced: 2026-04-14T21:30:38.655Z (3 months ago)
- Topics: api, csharp, dotnet, executor, limit, limiting, rate, ratelimiting, ratelimitingexecutor, util, utils
- Language: C#
- Homepage: https://soenneker.com
- Size: 941 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Security: .github/SECURITY.md
Awesome Lists containing this project
README
[](https://www.nuget.org/packages/soenneker.utils.ratelimiting.executor/)
[](https://github.com/soenneker/soenneker.utils.ratelimiting.executor/actions/workflows/publish-package.yml)
[](https://www.nuget.org/packages/soenneker.utils.ratelimiting.executor/)
[](https://github.com/soenneker/soenneker.utils.ratelimiting.executor/actions/workflows/codeql.yml)
#  Soenneker.Utils.RateLimiting.Executor
### A thread-safe utility designed to manage the rate at which tasks are executed, ensuring they are not run more frequently than a specified interval.
`RateLimitingExecutor` is ideal for interacting with rate-limited APIs or throttling the execution of resource-intensive tasks.
### Sequential Execution
`Tasks`, `ValueTasks`, and `Actions` are executed one at a time. If the defined interval between executions has passed, the task runs immediately; otherwise, it waits until the interval elapses before proceeding.
?? Important Notes:
- This is not a background queue processor. Each method awaits the result of the asynchronous operation before continuing.
- Asynchronous methods will not block the calling thread, but synchronous methods will block execution until it completes.
### Want to use this with dependency injection?
Check out the singleton factory implementation: [Soenneker.Utils.RateLimiting.Factory](https://github.com/soenneker/soenneker.utils.ratelimiting.factory)
## Installation
```
dotnet add package Soenneker.Utils.RateLimiting.Executor
```
## Example: Executing a Loop of Tasks with Rate Limiting
Below is an example demonstrating how to use the RateLimitingExecutor to execute a series of tasks while maintaining a rate limit.
```csharp
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Soenneker.Utils.RateLimiting.Executor;
public class Program
{
public static async Task Main(string[] args)
{
var rateLimitingExecutor = new RateLimitingExecutor(TimeSpan.FromSeconds(2));
for (int i = 0; i < 5; i++)
{
await rateLimitingExecutor.Execute(async ct =>
{
Console.WriteLine($"Executing Task {i + 1} at {DateTime.Now:HH:mm:ss}");
await Task.Delay(100); // Simulate some work
});
}
}
}
```
### Console Output
```csharp
Executing Task 1 at 14:00:00
Executing Task 2 at 14:00:02
Executing Task 3 at 14:00:04
Executing Task 4 at 14:00:06
Executing Task 5 at 14:00:08
```