public void GetDelayTest() { var limit = new RateLimit(); // Test passing 0 for the request per hour. Assert.AreEqual(0, limit.GetDelay(0)); // No web calls yet. Assert.AreEqual(0, limit.GetDelay(2000)); // Test being under the limit. limit.AddApiCall(); Thread.Sleep(500); limit.AddApiCall(); Assert.AreEqual(0, limit.GetDelay(2000)); // Test being over the limit. Thread.Sleep(500); limit.AddApiCall(); Thread.Sleep(500); limit.AddApiCall(); Thread.Sleep(500); limit.AddApiCall(); Thread.Sleep(500); limit.AddApiCall(); Assert.AreNotEqual(0, limit.GetDelay(2000)); }
public async static Task HandleRateLimit() { int delayInMilliseconds = _apiRateLimit.GetDelay(MtgApiController.RatelimitLimit); if (delayInMilliseconds > 0) { await Task.Delay(delayInMilliseconds); } _apiRateLimit.AddApiCall(); }
public async static Task HandleRateLimit() { await _semaphoreSlim.WaitAsync().ConfigureAwait(false); try { int delayInMilliseconds = _apiRateLimit.GetDelay(RatelimitLimit); if (delayInMilliseconds > 0) { await Task.Delay(delayInMilliseconds).ConfigureAwait(false); } _apiRateLimit.AddApiCall(); } finally { _semaphoreSlim.Release(); } }