I'm working on an application that interfaces with the Microsoft Academic Search API, and through the course of some of my very early testing ran into an issue with the API blocking calls. Upon further inspection it appeared this was due to my application
surpassing some rate limiting threshold despite building in functionality to conform to the 200 queries per minute limit.
Being that I'm just starting to work with the API I'm wondering what I should do to ensure that I don't surpass a limit other than the advertised 200 queries/minute threshold, such as some sort of daily aggregate total.
Any guidance would be much appreciated.
The code to accomplish my application-side rate limiting is as follows:
public RateLimit()
{
_watch = new Stopwatch();
_requests = 0;
}
public void AddRequest()
{
if (!_watch.IsRunning)
{
_watch.Start();
}
_requests++;
}
public void WaitIfLimited()
{
if (_requests >= RequestsPerInterval && _watch.Elapsed <= RequestInterval)
{
TimeSpan WaitSpan = RequestInterval - _watch.Elapsed;
Console.WriteLine("Hit rate limit wall, waiting {0} before continuing.", WaitSpan);
Thread.Sleep(WaitSpan);
_watch.Restart();
_requests = 0;
}
else if (_watch.Elapsed > RequestInterval)
{
Console.WriteLine("Restting rate limit.");
_requests = 0;
_watch.Restart();
}
}