Class SlidingWindowThrottle
Efficiently calculates the throttling rate over a number of seconds. The algorithm implements a variation of token bucket algorithm that doesn't require to add tokens to the bucket on a timer but rather it maintains a cirtular buffer of tokens with resolution of 1/BucketsPerSec. The Add() function is used to add items to a bucket associated with the timestamp passed as the first argument to the function. The Sum() returns the total number of items over the given interval of seconds. The items automatically expire when the time moves on the successive invocations of the Add() method.
Inherited Members
Namespace: NFX.Throttling
Assembly: NFX.dll
Syntax
public class SlidingWindowThrottle : Throttle, IConfigurable, IThrottle, IApplicationComponent, IDisposable
Constructors
SlidingWindowThrottle()
Declaration
public SlidingWindowThrottle()
SlidingWindowThrottle(String, Int32, Int32, Int32, String)
Constructor
Declaration
public SlidingWindowThrottle(string name, int throttleLimit, int throttleInterval, int bucketsPerSecond = 5, string unit = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | name | Name of this instance |
System.Int32 | throttleLimit | Throttling limit per throttleInterval |
System.Int32 | throttleInterval | Throttling interval in number of seconds |
System.Int32 | bucketsPerSecond | Number of buckets per second to calculate internal running sum. The greater the value the more precise the avg calculation is and the longer it takes to calculate |
System.String | unit | Unit of measurement |
Properties
Avg
Return current running average over the throttling interval
Declaration
public double Avg { get; }
Property Value
Type | Description |
---|---|
System.Double |
BucketsPerSec
Number of measurement buckets per second. The greater the number the more accurate throttle rate is calculated and the slower the calculation gets
Declaration
[Config("$buckets-per-sec", 8)]
public int BucketsPerSec { get; }
Property Value
Type | Description |
---|---|
System.Int32 |
Interval
Return throttling interval in seconds
Declaration
[Config("$interval")]
public int Interval { get; }
Property Value
Type | Description |
---|---|
System.Int32 |
Limit
Return throttle limit over Interval
Declaration
[Config("$limit")]
public double Limit { get; }
Property Value
Type | Description |
---|---|
System.Double |
Sum
Return current running sum over the throttling interval
Declaration
public double Sum { get; }
Property Value
Type | Description |
---|---|
System.Double |
Methods
Destructor()
Declaration
protected override void Destructor()
Overrides
DoConfigure(IConfigSectionNode)
Declaration
protected override void DoConfigure(IConfigSectionNode node)
Parameters
Type | Name | Description |
---|---|---|
IConfigSectionNode | node |
Overrides
Reset()
Reset the internal state of the throttling strategy
Declaration
public override void Reset()
Overrides
ToString(DateTime)
Dump the internal state to string
Declaration
public string ToString(DateTime time)
Parameters
Type | Name | Description |
---|---|---|
System.DateTime | time | Time for which to dump internal state to string |
Returns
Type | Description |
---|---|
System.String |