Does Retrofit or OkHttp limit number of parallel requests?

Viewed 419

I use RxJavaCallAdapterFactory.create() and Schedulers.io(), so Dispatcher isn't used and maxRequests and maxRequestsPerHost not a problem. Schedulers.io() creates infinite number of threads.

The problem is when I run 10 requests and then 1 more while previous not finished this 1 request takes longer time to start. If 10 requests finished the next request run immediately. When I have a several requests running I see in logs that Call.execute() is called and then request started only after 6 seconds. When there are no active requests, new request starts immediately. So is it limitation of android or some network libraries?

2 Answers

Yes, configure this in OkHttp’s Dispatcher.

Use OkHttp Dispatcher.

Dispatcher dispatcher = new Dispatcher(Executors.newFixedThreadPool(20));
dispatcher.setMaxRequests(20);
dispatcher.setMaxRequestsPerHost(1);

OkHttpClient okHttpClient = new OkHttpClient.Builder()
   .dispatcher(dispatcher)
   .build();

Retrofit retrofit = new Retrofit.Builder()
   .baseUrl(HttpUrl.get("https://example.com/"))
   .client(okHttpClient)
   .build();
Related