Bucket4Csharp.Core
0.1.0
dotnet add package Bucket4Csharp.Core --version 0.1.0
NuGet\Install-Package Bucket4Csharp.Core -Version 0.1.0
<PackageReference Include="Bucket4Csharp.Core" Version="0.1.0" />
<PackageVersion Include="Bucket4Csharp.Core" Version="0.1.0" />
<PackageReference Include="Bucket4Csharp.Core" />
paket add Bucket4Csharp.Core --version 0.1.0
#r "nuget: Bucket4Csharp.Core, 0.1.0"
#:package Bucket4Csharp.Core@0.1.0
#addin nuget:?package=Bucket4Csharp.Core&version=0.1.0
#tool nuget:?package=Bucket4Csharp.Core&version=0.1.0
Bucket4Csharp
A port of the Bucket4J library https://github.com/vladimir-bukhtoyarov/bucket4j to C#. Trying to port the functionality and algorithms while keeping the syntax more C#'py (instead of GetX use a property called X).
Usage
The basic usage is done with the IBucket interface which has a static CreateBuilder method.
You can call this method to create a bucket builder which will create a LockFreeBucket instance:
var bucketBuilder = IBucket.CreateBuilder(); //create the builder.
var newBucket = bucketBuilder.AddLimit(Bandwidth.Simple(100, TimeSpan.FromHours(1))).Build(); //100 requests every hour max.
You can then use the bucket as simple as:
var result = newBucket.TryConsume(1).
If there's an available token it will return true. If the rate limit is reached it will return false.
You can also cast it to an ISchedulingBucket if you want to wait until there's an available token using the async/await, and ConsumeAsync.
For example:
public async Task SchedulingBucketExample()
{
var limit = Bandwidth.Simple(1, TimeSpan.FromSeconds(30));
var bucket = IBucket.CreateBuilder()
.AddLimit(limit)
.Build() as ISchedulingBucket;
if(bucket != null)
{
var count = 0;
var cancellationTokenSource = new CancellationTokenSource();
cancellationTokenSource.CancelAfter(TimeSpan.FromSeconds(240));
while (count < 10)
{
await bucket.ConsumeAsync(1, cancellationTokenSource.Token);
Debug.WriteLine($"{DateTime.Now:T}");
count++;
}
}
This method will run only every 30 seconds as it will be rate limited by the bucket.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net6.0
- Microsoft.Extensions.Logging.Abstractions (>= 6.0.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Bucket4Csharp.Core:
| Package | Downloads |
|---|---|
|
OpenStockApi.Core
Contains the main definitions used to interface with the API and SignalR components of the StockDrops API/Hubs |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.1.0 | 1,048 | 1/14/2022 |