Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rate control in java

I am looking for a good solution or probably an API to solve the following problem:

  • My application does a task in a loop, for example it sends e-mails etc. I need to limit the average rate of messages to for example 100 messages per second or 1000 messages per last minute ...

No I am looking for an algorithm or an API which does exactly this task.

like image 707
Literadix Avatar asked Dec 09 '22 06:12

Literadix


2 Answers

You can use a ScheduledExecutorService to schedule tasks for a given period of time.

For example, to schedule 100 tasks per second you can say:

ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(nThreads);
scheduler.scheduleAtFixedRate(mailSender, 0, 10, TimeUnit.MILLISECONDS);

Obviously, you need to track how many tasks have executed and turn off the scheduler after the job is done.

like image 55
dm3 Avatar answered Dec 14 '22 22:12

dm3


Token bucket algorithm is very easy to implement and use yet very powerful. You can control the throughput at runtime and queue some requests to handle peeks.

like image 21
Tomasz Nurkiewicz Avatar answered Dec 15 '22 00:12

Tomasz Nurkiewicz