Skip to content

rschili/RSFlowControl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RSFlowControl

Scope

This package contains some utility classes that have zero dependencies and are concerned with flow control. When I implemented these, I could not find a simple availeble solution that fits my needs, so I implemented one.

LeakyBucket

This class is basically a rate limiter following the leaky bucket algorithm. It does not have to be disposed, uses minimal locking while being threadsafe.

using RSFlowControl;
int capacity = 10;
int maxLeaksPerHour = 600;

var rateLimiter = new LeakyBucket(capacity, maxLeaksPerHour);
if (rateLimiter.Leak())
    // Do work

ProbabilityRamp

Probabilistic rate limiting/triggering

I use this one to occasionally make a bot trigger a reaction. The idea is that there is a chance to trigger, which increases over time, and when it does trigger, the timer is reset back to its initial value.

using RSFlowControl;

// 5% chance initially, ramping to 40% over 2 minutes
var ramp = new ProbabilityRamp(0.05, 0.4, TimeSpan.FromMinutes(2));
if (ramp.Check())
    // Do work

// Get current probability without checking
double currentProb = ramp.CurrentChance;

// Reset the timer
ramp.Reset();

About

.NET basic library for rate limiting and similar things

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •