This is quite fine in my opinion. Except that, there is a bug in this code. This is because what if each request is done more than a second after one another? It will never go inside that if
block. Thus, some kind of memory leak because the _requests
will grow larger over time and possibly never be cleared if my scenario above always happens.
Example:
for (int i = 0; i < 100; i++)
{
RateLimiter.CheckLimiter();
Thread.Sleep(2000);
}
What you can do is to remove entries in your _requests
that are exceeding the 1 second rule like adding this line at the end of your method.
if (_requests.Count != 0)
{
//remove irrelevant/expired entries
_requests.RemoveAll(date => (DateTime.Now - date).TotalMilliseconds >= _perMillisecond);
}