Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connection pool and File handles

We use Retrofit/OkHttp3 for all network traffic from our Android application. So far everything seems to run quite smoothly.

However, we have now occasionally had our app/process run out of file handles.

  • Android allows for a max of 1024 file handles per process
  • OkHttp will create a new thread for each async call
  • Each thread created this way will (from our observation) be responsible for 3 new file handles (2 pipes and one socket).

We were able to debug this exactly, where each dispached async call using .enqueue() will lead to an increase of open file handles of 3.

The problem is, that the ConnectionPool in OkHttp seems to be keeping the connection threads around for much longer than they are actually needed. (This post talks of five minutes, though I haven't seen this specified anywhere.)

  • That means if you are quickly dispatching request, the connection pool will grow in size, and so will the number of file handles - until you reach 1024 where the app crashes.

I've seen that it is possible to limit the number of parallel calls with Dispatcher.setMaxRequests()(although it seems unclear whether this actually works, see here) - but that still doesn't quite address the issue with the open threads and file handles piling up.

How could we prevent OkHttp from creating too many file handles?

like image 772
fgysin Avatar asked Mar 22 '17 10:03

fgysin


People also ask

What are connection pools used for?

In software engineering, a connection pool is a cache of database connections maintained so that the connections can be reused when future requests to the database are required. Connection pools are used to enhance the performance of executing commands on a database.

What happens when connection pool is full?

If the maximum pool size has been reached and no usable connection is available, the request is queued. The pooler then tries to reclaim any connections until the time-out is reached (the default is 15 seconds). If the pooler cannot satisfy the request before the connection times out, an exception is thrown.

How do you set up a connection pool?

Click Resources > JDBC > Data Sources > data_source > [Additional Properties] Connection pool properties. Click Resources > JMS->Queue connection factories-> queue_connection_factory ->[Additional Properties] Connection pool.

How many connection pools should I have?

For optimal performance, use a pool with eight to 16 connections per node. For example, if you have four nodes configured, then the steady-pool size must be set to 32 and the maximum pool size must be 64.


1 Answers

I am answering my own question here to document this issue we had. It took us a while to figure this out and I think others might encounter this too and might be glad for this answer.


Our problem was that we created one OkHttpClient per request, as we used it's builder/interceptor API to configure some per-request parameters like HTTP headers or timeouts.

By default each OkHttpClient comes with its own connection pool, which of course blows up the number of connections/threads/file handles and prevents proper reuse in the pool.

Our solution

We solved the problem by manually creating a global ConnectionPool in a singleton, and then passing that to the OkHttpClient.Builder object which builds the actual OkHttpClient.

  • This still allows for per-request configuration using the OkHttpClient.Builder
  • Makes sure all OkHttpClient instances are still using a common connection pool.

We were then able to properly size the global connection pool.

like image 168
fgysin Avatar answered Oct 13 '22 15:10

fgysin