How to limit method usage per amount of time?

前端 未结 6 1511
南旧
南旧 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:49

    Assuming a rolling hour window:

    Maintain a list of when actions were done.

    Each time you want to do your action, remove all in the list not within the hour.

    If there are fewer than 1000 then do the action and add a record to your list.


    Assuming hourly:

    Create a proxy method and a variable that is incremented for every action, and reduced to zero on the hour.

    Do your action if the counter is < 1000.

提交回复
热议问题