ManagedCode.Orleans.RateLimiting.Client
0.0.3
Prefix Reserved
See the version list below for details.
dotnet add package ManagedCode.Orleans.RateLimiting.Client --version 0.0.3
NuGet\Install-Package ManagedCode.Orleans.RateLimiting.Client -Version 0.0.3
<PackageReference Include="ManagedCode.Orleans.RateLimiting.Client" Version="0.0.3" />
paket add ManagedCode.Orleans.RateLimiting.Client --version 0.0.3
#r "nuget: ManagedCode.Orleans.RateLimiting.Client, 0.0.3"
// Install ManagedCode.Orleans.RateLimiting.Client as a Cake Addin #addin nuget:?package=ManagedCode.Orleans.RateLimiting.Client&version=0.0.3 // Install ManagedCode.Orleans.RateLimiting.Client as a Cake Tool #tool nuget:?package=ManagedCode.Orleans.RateLimiting.Client&version=0.0.3
Orleans.RateLimiting
Orleans.RateLimiting is a library for Microsoft Orleans that provides a set of rate limiting algorithms for controlling the flow of requests in your distributed applications. It is designed to be easy to use and to integrate with your Orleans-based applications seamlessly. With Orleans.RateLimiting, you can ensure your applications handle a safe number of requests without the risk of overloading your system resources. RateLimiting on learn.microsoft.com and devblogs.microsoft.com
Features
- Supports 4 types of rate limiting algorithms:
- Fixed Window Rate Limiter
- Concurrency Limiter
- Sliding Window Rate Limiter
- Token Bucket Rate Limiter
- Easy integration with Microsoft Orleans
- Configurable rate limiting options
- Comprehensive documentation and examples
Installation
You can install Orleans.RateLimiting via NuGet Package Manager:
// for Client
Install-Package anagedCode.Orleans.RateLimiting.Client
// for Server
Install-Package anagedCode.Orleans.RateLimiting.Server
then add the following to your SiloHostBuilder
or ClientBuilder
:
// for Client
clientBuilder.AddOrleansRateLimiting();
// for Server
siloBuilder.AddOrleansRateLimiting();
Also if you would like to use incoming filter and Attributes, you have to add default options for Limiter:
//Add default options and IncomingFilter
siloBuilder.AddOrleansConcurrencyLimiter(options =>
{
options.PermitLimit = 10;
options.QueueLimit = 15;
});
//Add default options and IncomingFilter
siloBuilder.AddOrleansFixedWindowRateLimiter(options =>
{
options.PermitLimit = 10;
options.QueueLimit = 15;
options.Window = TimeSpan.FromSeconds(1);
});
//Add default options and IncomingFilter
siloBuilder.AddOrleansSlidingWindowRateLimiter(options =>
{
options.PermitLimit = 10;
options.QueueLimit = 15;
options.Window = TimeSpan.FromSeconds(1);
options.SegmentsPerWindow = 2;
});
//Add default options and IncomingFilter
siloBuilder.AddOrleansTokenBucketRateLimiter(options =>
{
options.TokenLimit = 10;
options.QueueLimit = 15;
options.TokensPerPeriod = 2;
options.ReplenishmentPeriod = TimeSpan.FromSeconds(1);
});
Usage
To use Orleans.RateLimiting in your application, first configure the desired rate limiter:
var rateLimiter = _client.GetConcurrencyLimiter("test");
await rateLimiter.Configure(new ConcurrencyLimiterOptions
{
PermitLimit = permit,
QueueLimit = permit * 2,
QueueProcessingOrder = QueueProcessingOrder.OldestFirst
});
Then, acquire a lease before making a request:
await using var lease = await rateLimiter.AcquireAsync();
if (lease.IsAcquired)
{
// do something
}
else
{
Console.WriteLine(lease.Reason); // reason why the lease was not acquired
Console.WriteLine(lease.RetryAfter); //TimeSpan to wait before retrying
}
The following rate limiters are provided as extensions for IGrainFactory
and IClusterClient
- Fixed Window Rate Limiter
var fixedWindowRateLimiter = _factory.GetFixedWindowRateLimiter("key");
- Concurrency Limiter
var concurrencyLimiter = _factory.GetConcurrencyLimiter("key");
- Sliding Window Rate Limiter
var slidingWindowRateLimiter = _factory.GetSlidingWindowRateLimiter("key");
- Token Bucket Rate Limiter
var tokenBucketRateLimiter = _factory.GetTokenBucketRateLimiter("key");
Attrubutes
You can use attributes to decorate your grain methods and apply rate limiting to them. Make sure you check configuration section for default options.
public class TestFixedWindowRateLimiterGrain : Grain, ITestFixedWindowRateLimiterGrain
{
[FixedWindowRateLimiter] //GrainId as key, default options
public async Task<string> Do()
{
await Task.Delay(TimeSpan.FromSeconds(5));
return "Do";
}
[FixedWindowRateLimiter(KeyType.Key, "go")] //String as Key, default options
public async Task<string> Go()
{
await Task.Delay(TimeSpan.FromSeconds(5));
return "Go";
}
[FixedWindowRateLimiter(KeyType.GrainType, permitLimit:2, queueLimit:1)] //GrainType as Key, custom options, some of them are default (check Attribute)
public async Task<string> Take()
{
await Task.Delay(TimeSpan.FromSeconds(5));
return "Take";
}
}
Contributing
We welcome contributions to Orleans.RateLimiting! Feel free to submit issues, feature requests, and pull requests on the GitHub repository.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net7.0
- ManagedCode.Orleans.RateLimiting.Core (>= 0.0.3)
- Microsoft.Extensions.DependencyInjection (>= 7.0.0)
- Microsoft.Orleans.Client (>= 7.1.1)
- System.Threading.RateLimiting (>= 7.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.