Answer the question
In order to leave comments, you need to log in
How to perform some action no more than N times per unit of time?
The database contains information about the user, an external event triggers a handler that performs some action on the user.
How to limit the number of actions performed on the user per unit of time?
For example, suppose we have a limit of no more than 200 actions in one hour for one user.
How to implement this?
Answer the question
In order to leave comments, you need to log in
Save for the user a timestamp and the number of actions during the current hour. Or just a timestamp, if it automatically determines the number of operations (if only one operation per minute is allowed, then there will be no more than 60 operations per hour and the counter will not be needed).
If events arrive often enough, then write in the mictorime(TRUE) field if processing is successful.
Before processing, check this field and the current microtime(TRUE) and discard the event if the calculated timeout has not yet expired. For 200 events per minute, the timeout will be 60/200.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question