square / okhttp

Square’s meticulous HTTP client for the JVM, Android, and GraalVM.

Home Page:https://square.github.io/okhttp/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ConnectionPool cannot be cleared by force

Bruc3Stark opened this issue · comments

I want to clean up okhttp connection pool by using the following code:
client.dispatcher().executorService().shutdown(); client.connectionPool().evictAll();
However, the connectionCount still did not decrease.
Is there any methods to clean up connection pool by force, instead of just waiting for the connections to become idle?

Try this:

client.dispatcher().cancelAll(); 
client.connectionPool().evictAll();

cancelAll() isn’t immediate unfortunately, so it'll take a few milliseconds before the connections become eligible for eviction. If you need that you could use EventListener to tell you when connections are released.

@swankjesse I don't think EventListener gives you Connection events, only Call level as far as they relate to acquiring a connection.

@yschimke yep! But if you need a signal when clearing the pool will be useful, after a connection is released by a call is the right time to do it.

Try this:

client.dispatcher().cancelAll(); 
client.connectionPool().evictAll();

Thanks a lot.