Method throttling.
I want to throttle the calls to a method N number of times per second. If the number of calls to the method exceeds N then wait for the required time and call the method again.
To achieve this create a method invocation controller and it should satisfy below requirements
Requirements
- If the number of calls to the method exceeds N times per second then it should return the required amount of time to wait for next invocation.
- If the number of calls to the method doesn’t exceeds N times per second then it should return a zero wait time.
- Method invocation controller should work in a multithreaded environment.
Implementation details
The method invocation controller will store the first invocation time in a member variable and when the number of invocation reaches N, it will check whether the difference between current time and first invocation crossed 1s. If the time difference is less than 1s, the controller will return the required wait time otherwise it will return a zero wait time.
Method invocation controller interface
Method invocation controller implementation
Test method