Skip to content

sbalant/RateLimiter

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RateLimiter

build NuGet Badge MIT License

C# client-side rate limiting utility.

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 asynchroneous: lower resource usage than thread sleep
  • Cancellable via CancellationToken
  • Thread safe so you can share time contraints object to rate limit diferent threads using the same resource
  • Composable: ability to compose diferent rate limits in one constraint

Sample usage

Basic

    //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.Perform(ConsoleIt);
    }       
    
    ....
    private Task ConsoleIt()
    {
        Trace.WriteLine(string.Format("{0:MM/dd/yyy HH:mm:ss.fff}", DateTime.Now));
        return Task.FromResult(0);
    }

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

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.Perform(ConsoleIt);
    }       

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

About

C# rate limiting utility

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%