Search code examples
javaspring-bootnetflix-feignfeign

Does Feign client have native implementation of bottlenecking?


I am facing the following situation, which to my surprise, I couldn't find much documentation: There is a service which only provides a rest call for item details, by obtaining it 1 by 1. There are 1k+ items in total.

For responsiveness reasons, I would like to persist this data on my end, and not fetch it lazily.

In order for my API key to not be locked, I would like to limit my calls to X calls / second.

I could not find any support for this in the Feign documentation. Does anybody know if there is one? Or do you have any suggestions on how to go about this implementation?


Solution

  • There is no built in throttling capability in Feign, that is delegated to the underlying Client implementation. With that said, you can define your own client extending from one of the provided ones, Apache Http, OkHttp, and Ribbon.

    One solution is to extend the Client to use a ScheduledThreadPoolExecutor as outlined in this answer. Apache HttpClient: Limit total calls per second

    To use this with the provided ApacheHttpClient in Feign, you could extend it, providing your own implementation of the execute method to use the executor.

    public class ThrottledHttpClient extends ApacheHttpClient {
        // create a pool with one thread, you'll control the flow later.
        private final ExecutorService throttledQueue = Executors.newScheduledThreadPool(1);
    
        @Override
        public Response execute(Request request, Request.Options options) throws IOException {
            // use the executor
            ScheduledFuture<Response> future = throttledQueue.scheduleAtFixedRate(super.execute(), ....);
            return future.get()
    }
    

    Set the appropriate thread pool size, delay and fixed wait to achieve the throughput you desire.