• Stars
    star
    278
  • Rank 148,454 (Top 3 %)
  • Language
    C#
  • License
    MIT License
  • Created over 8 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

C# rate limiting utility

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
...

More Repositories

1

Vue.D3.tree

Vue component to display tree based on D3.js layout.
Vue
868
star
2

draggable-example

vue.draggable example
Vue
510
star
3

Vue.Isotope

πŸ“± Vue component for isotope filter & sort magical layouts
JavaScript
345
star
4

Vue.resize

Vue directive to detect resize events with deboucing and throttling capacity.
JavaScript
327
star
5

vue-plotly

πŸ“ˆ vue wrapper for plotly.js
JavaScript
254
star
6

Vue.ImagesLoaded

Vue.js 2.0 directive to detect images loading
JavaScript
144
star
7

vue-cli-plugin-component

πŸ› οΈ vue-cli 3 plugin to create component
JavaScript
88
star
8

Vue.D3.sunburst

Vue sunburst component based on D3.js
Vue
62
star
9

ComponentFixture

πŸ› οΈInteractive sandox playground for vue components
Vue
51
star
10

Vue-Semantic-Modal

Vue modal component for Semantic-Ui no jquery
JavaScript
45
star
11

DiscogsClient

Discogs API C# Client
C#
41
star
12

CodeDependencyScanner

.Net assembly code depency inspector
JavaScript
37
star
13

ComposableAsync

Create, compose and inject asynchronous behaviors in .Net Framework and .Net Core.
C#
31
star
14

MVVM-for-awesomium

MVVM binding framework between C# ViewModel and awesomium HTLM-javascript view
C#
16
star
15

RestSharpHelper

Small library helper for RestSharp inclusing rate limit, OAuth, helper for async download among others
C#
7
star
16

Music.Cover.Finder

C# application that display music cover art
C#
7
star
17

MusicCollection

Music Library and Player
C#
6
star
18

Retlang

C#
5
star
19

vue-ajax-handler

Ultra minimal generic vue component to deal with ajax loading
JavaScript
2
star
20

datascience_detroit_bligth

Predicting Detroit Blight using Machine Learning
Jupyter Notebook
2
star
21

David-Desmaisons

1
star
22

tape-tests

Minimal repository to show problem with esm modules and windows
JavaScript
1
star
23

ScreenSaver

JavaScript
1
star
24

MovieExplorer

C#
1
star
25

ProxyDebugLogger

Create proxy that logs method call. For debug purpose.
JavaScript
1
star
26

MoreCollection

C# collection framework
C#
1
star