How to limit method usage per amount of time?

前端 未结 6 1525
南旧
南旧 2020-12-31 08:12

It has to be trivial, but I just cannot get through it. I have to limit amount of tasks (let\'s say connections, emails sent or clicks in the button) per amount of time. So

6条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-31 08:43

    You can create a persistent counter for every user. Every time you receive a request (for sending an email) you need to check the value of the counter and the date of the counter creation.

    • If the count is greater than the limit you refuse the request
    • If the date is older than an hour you reset the counter and set the new creation date
    • If the date is correct and the count is under the limit you increase the counter

    Only in the last two cases the request is executed.

提交回复
热议问题