Bucket4Csharp.Core 0.1.0

dotnet add package Bucket4Csharp.Core --version 0.1.0
                    
NuGet\Install-Package Bucket4Csharp.Core -Version 0.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Bucket4Csharp.Core" Version="0.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Bucket4Csharp.Core" Version="0.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Bucket4Csharp.Core" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Bucket4Csharp.Core --version 0.1.0
                    
#r "nuget: Bucket4Csharp.Core, 0.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Bucket4Csharp.Core@0.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Bucket4Csharp.Core&version=0.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Bucket4Csharp.Core&version=0.1.0
                    
Install as a Cake Tool

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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