Giter VIP home page Giter VIP logo

bucket4csharp's Introduction

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.