Code Monkey home page Code Monkey logo

ratelimiter's Introduction

RateLimiter

build codecov NuGet Badge MIT License

C# client-side rate limiting utility.

http://david-desmaisons.github.io/RateLimiter/

Motivation

The initial motivation was to create helper to respect Web Services rate limit in client application. However this helper can also be also in other scenarios where you need to temporally limit the usage of one shared resource.

Features

  • Easy to use
  • Fully asynchronous: lower resource usage than thread sleep
  • Cancellable via CancellationToken
  • Thread safe so you can share time constraints object to rate limit different threads using the same resource
  • Composable: ability to compose different rate limits in one constraint

Installation

Install-Package RateLimiter -Version 2.2.0

Sample usage

Basic

RateLimiters are awaitable: the code executed after the await will respect the time constraint:

    using ComposableAsync;

    // Create Time constraint: max five times by second
    var timeConstraint = TimeLimiter.GetFromMaxCountByInterval(5, TimeSpan.FromSeconds(1));

    // Use it
    for(int i=0; i<1000; i++)
    {
        await timeConstraint;
        Trace.WriteLine(string.Format("{0:MM/dd/yyy HH:mm:ss.fff}", DateTime.Now));
    }

Output

05/23/2016 00:14:44.791
05/23/2016 00:14:44.958
05/23/2016 00:14:44.959
05/23/2016 00:14:44.959
05/23/2016 00:14:44.960
05/23/2016 00:14:45.959
05/23/2016 00:14:45.960
05/23/2016 00:14:45.961
05/23/2016 00:14:45.961
05/23/2016 00:14:45.962
05/23/2016 00:14:46.973
...

As http DelegatingHandler

    using System.Net.Http;

    //...
    var handler = TimeLimiter
            .GetFromMaxCountByInterval(60, TimeSpan.FromMinutes(1))
            .AsDelegatingHandler();
    var Client = new HttpClient(handler)

With cancellation token:

    // Create Time constraint: max three times by second
    var timeConstraint = TimeLimiter.GetFromMaxCountByInterval(3, TimeSpan.FromSeconds(1));
    var cancellationSource = new CancellationTokenSource(1100);

    // Use it
    while(true)
    {
        await timeConstraint.Enqueue(ConsoleIt, cancellationSource.Token);
    }
    
    //....
    private static void ConsoleIt()
    {
        Trace.WriteLine(string.Format("{0:MM/dd/yyy HH:mm:ss.fff}", DateTime.Now));
    }

Output

07/07/2019 18:09:35.645
07/07/2019 18:09:35.648
07/07/2019 18:09:35.648
07/07/2019 18:09:36.649
07/07/2019 18:09:36.650
07/07/2019 18:09:36.650

Composed

    // Create first constraint: max five times by second
    var constraint = new CountByIntervalAwaitableConstraint(5, TimeSpan.FromSeconds(1));
    
    / /Create second constraint: one time each 100 ms
    var constraint2 = new CountByIntervalAwaitableConstraint(1, TimeSpan.FromMilliseconds(100));
    
    // Compose the two constraints
    var timeConstraint = TimeLimiter.Compose(constraint, constraint2);

    // Use it
    for(int i=0; i<1000; i++)
    {
        await timeConstraint;
        Trace.WriteLine(string.Format("{0:MM/dd/yyy HH:mm:ss.fff}", DateTime.Now));
    }       

Output

05/21/2016 23:52:48.573
05/21/2016 23:52:48.682
05/21/2016 23:52:48.809
05/21/2016 23:52:48.922
05/21/2016 23:52:49.024
05/21/2016 23:52:49.575
05/21/2016 23:52:49.685
05/21/2016 23:52:49.810
05/21/2016 23:52:49.942
...

ratelimiter's People

Contributors

david-desmaisons avatar nihlus avatar mscrivo avatar firenero avatar

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.