Limit the number of method executions per second
Task : to develop the ability to run a given number of operations per second.
Requirements :
As a result, I got a function (naturally as part of a separate class) that returns true or false (permission to execute).
The general principle is as follows. There is a collection ofDateTime fingerprints of time (in the comment they suggested that it would be more reasonable to take Stopwatch). When requesting permission, it checks to see if the number of requests in the last second exceeds the maximum specified value. If it exceeds, false is returned, otherwise a cast of the current time is added to the collection and the truth is returned.
Initially, I did everything based on sheets (List <>), but sheets have a dynamic data set, which means that they expand as needed when adding new elements. And you also need to delete. So I decided to use a collection of a given size. Therefore, I decided to abandon the sheet in favor of the array. As a result, I got the functionality of a circular collection based on an array, so in the end I made a circular collection.
The result was a pool of date prints of a given size based on a circular collection. The pool class contains the current item, which has a date stamp and a link to the next item. Initially, all dates have a default value.
When requesting permission, the next element from the current one is taken, and 1 second is added to its date. If the date is greater than the current date, then there is no space in the pool and the request returns false. Otherwise, the next one is set as the current element, a date cast is set in the new current element and true is returned.
Now about the threads. I haven’t come up with anything better than putting a lock on a permission function.
That's all.
For testing, I created a method that executes a given number of requests with a given number of threads. I launched it with the following parameters. The number of operations per second is 6, the number of threads is 4, the number of requests is 50.
Here are the results:

→ Link to github
From the screen you can see that the code worked correctly.
Requirements :
- The solution should work out as quickly as possible (otherwise it makes no sense)
- The solution must be thread safe.
As a result, I got a function (naturally as part of a separate class) that returns true or false (permission to execute).
The general principle is as follows. There is a collection of
Initially, I did everything based on sheets (List <>), but sheets have a dynamic data set, which means that they expand as needed when adding new elements. And you also need to delete. So I decided to use a collection of a given size. Therefore, I decided to abandon the sheet in favor of the array. As a result, I got the functionality of a circular collection based on an array, so in the end I made a circular collection.
The result was a pool of date prints of a given size based on a circular collection. The pool class contains the current item, which has a date stamp and a link to the next item. Initially, all dates have a default value.
When requesting permission, the next element from the current one is taken, and 1 second is added to its date. If the date is greater than the current date, then there is no space in the pool and the request returns false. Otherwise, the next one is set as the current element, a date cast is set in the new current element and true is returned.
Now about the threads. I haven’t come up with anything better than putting a lock on a permission function.
That's all.
For testing, I created a method that executes a given number of requests with a given number of threads. I launched it with the following parameters. The number of operations per second is 6, the number of threads is 4, the number of requests is 50.
Here are the results:
→ Link to github
From the screen you can see that the code worked correctly.