API rate limiting with Completablefuture for async processing

Viewed 529

I am trying to handle the API rate limiting. I found that using ratelimiter I can block the usage of resources. Hence, in the following code I am trying to have only two threads until the batch (size) is completed processing.

import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicInteger;
import com.google.common.util.concurrent.RateLimiter;

public class thenapply {
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        List<Integer> list = Arrays.asList(1, 2, 3);
        final AtomicInteger sum = new AtomicInteger(0);
        final RateLimiter rateLimiter = RateLimiter.create(2.0); // rate is "2 permits per second"
        int size = list.size();
        CountDownLatch latch = new CountDownLatch(size);
        for (int i = 0; i < size; i++) {
            final int finali = i;
            rateLimiter.acquire(); // should wait till I get 2 requests
            CompletableFuture.runAsync(() -> {
                int sq= newdoc(list.get(finali));
                sum.addAndGet(sq);
                latch.countDown();
            });
        }
        latch.await();
        System.out.println(sum);
    }

    public static int newdoc(int val){
        return val*val;
    }
}
  1. In real time when we are trying to use the service, the end-user will be trying to call the service and if he calls it multiple times, we are throwing some error. How can we handle that using the delay. So basically, currently I have two requests using ratelimiter after that no request will be handled. how can I queue them with a delay?

  2. When will ratelimiter.acquire() be free to give resources? If after the processing of two requests, then what about my latch.countDown as it is after the for loop?

0 Answers
Related