Skip to content

Commit

Permalink
fix fabric8io#4985: triggering the cleanup of the idle task
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins committed Mar 21, 2023
1 parent 4353107 commit 33d682e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### 6.6-SNAPSHOT

#### Bugs
* Fix #4985: triggering the immediate cleanup of the okhttp idle task

#### Improvements

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okhttp3.internal.Internal;
import okio.Buffer;
import okio.BufferedSink;
import okio.BufferedSource;
Expand All @@ -56,6 +57,7 @@
import java.io.PrintWriter;
import java.io.Reader;
import java.io.StringWriter;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.nio.ByteBuffer;
import java.util.Collections;
Expand Down Expand Up @@ -257,6 +259,7 @@ public void close() {
LOG.debug("Shutting down dispatcher {} at the following call stack: {}", this.httpClient.dispatcher(), stack);
}
ConnectionPool connectionPool = httpClient.connectionPool();

Dispatcher dispatcher = httpClient.dispatcher();
ExecutorService executorService = httpClient.dispatcher() != null ? httpClient.dispatcher().executorService() : null;

Expand All @@ -266,6 +269,21 @@ public void close() {

if (connectionPool != null) {
connectionPool.evictAll();

// begin hack to terminate the idle task, which is not necessary after 4.3.0 - https://github.com/square/okhttp/commit/bc3ad111ad01100a77846f7dc433b0c0f5b58dba
// to immediately clean it up, we need to notify the thread waiting on the ConnectionPool / RealConnectionPool
Object realConnectionPool = connectionPool;

try {
// 3.14+ holds a delegate to the real pool
Method method = Internal.class.getMethod("realConnectionPool", ConnectionPool.class);
realConnectionPool = method.invoke(Internal.instance, connectionPool);
} catch (Exception e) {
// could be 3.12
}
synchronized (realConnectionPool) {
realConnectionPool.notifyAll();
}
}

if (executorService != null) {
Expand Down

0 comments on commit 33d682e

Please sign in to comment.