Class RateGate
- Namespace
- Sportradar.LiveData.Sdk.Services.RateLimiter
- Assembly
- Sportradar.LiveData.Sdk.dll
Used to control the rate of some occurrence per unit of time. Adapted from http://www.pennedobjects.com/2010/10/better-rate-limiting-with-dot-net/
public class RateGate : IRateGate, IDisposable
- Inheritance
-
RateGate
- Implements
- Inherited Members
Remarks
To control the rate of an action using a RateGate, code should simply call WaitToProceed(TimeSpan, int) prior to performing the action. WaitToProceed(TimeSpan, int) will block the current thread until the action is allowed based on the rate limit.
This class is thread safe. A single RateGate instance may be used to control the rate of an occurrence across multiple threads.
Constructors
RateGate(string, int, TimeSpan, int)
Initializes a RateGate with a rate of occurrences
per time_unit.
public RateGate(string name, int occurrences, TimeSpan time_unit, int granularity = 1)
Parameters
namestringoccurrencesintNumber of occurrences allowed per unit of time.
time_unitTimeSpanLength of the time unit.
granularityintGranularity of the gate (e.g. 100:1)
Exceptions
- ArgumentOutOfRangeException
If
occurrencesortime_unitis negative.
Fields
m_exit_times
public readonly Queue<int> m_exit_times
Field Value
Properties
Name
public string Name { get; }
Property Value
Occurrences
Number of occurrences allowed per unit of time.
public int Occurrences { get; }
Property Value
TimeUnitMilliseconds
The length of the time unit, in milliseconds.
public int TimeUnitMilliseconds { get; }
Property Value
Methods
Dispose()
Releases unmanaged resources held by an instance of this class.
public void Dispose()
Dispose(bool)
Releases unmanaged resources held by an instance of this class.
protected virtual void Dispose(bool is_disposing)
Parameters
is_disposingboolWhether this object is being disposed.
~RateGate()
protected ~RateGate()
Peek()
public TimeSpan Peek()
Returns
WaitToProceed(int)
Blocks the current thread indefinitely until allowed to proceed.
Number of occurencespublic void WaitToProceed(int count)
Parameters
countint
WaitToProceed(TimeSpan, int)
Blocks the current thread until allowed to proceed or until the specified timeout elapses.
public bool WaitToProceed(TimeSpan timeout, int count)
Parameters
Returns
- bool
true if the thread is allowed to proceed, or false if timed out