Table of Contents

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

name string
occurrences int

Number of occurrences allowed per unit of time.

time_unit TimeSpan

Length of the time unit.

granularity int

Granularity of the gate (e.g. 100:1)

Exceptions

ArgumentOutOfRangeException

If occurrences or time_unit is negative.

Fields

m_exit_times

public readonly Queue<int> m_exit_times

Field Value

Queue<int>

Properties

Name

public string Name { get; }

Property Value

string

Occurrences

Number of occurrences allowed per unit of time.

public int Occurrences { get; }

Property Value

int

TimeUnitMilliseconds

The length of the time unit, in milliseconds.

public int TimeUnitMilliseconds { get; }

Property Value

int

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_disposing bool

Whether this object is being disposed.

~RateGate()

protected ~RateGate()

Peek()

public TimeSpan Peek()

Returns

TimeSpan

WaitToProceed(int)

Blocks the current thread indefinitely until allowed to proceed.

Number of occurences
public void WaitToProceed(int count)

Parameters

count int

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

timeout TimeSpan

Timeout.

count int

Number of times we should wait (based on granularity)

Returns

bool

true if the thread is allowed to proceed, or false if timed out