Skip to content

Standardize sender shutdown implementations #8280

@jack-berg

Description

@jack-berg

Let's hold off on this. I just did an analysis of the shutdown of OkHttpHttpSender, OkHttpGrpcSender, and JdkHttpSender, and they're all different. Will open a separate issue to standardize (probably mirroring the logic in OkHttpGrpcSender which is the newest).

Originally posted by @jack-berg in #8276

public CompletableResultCode shutdown() {
client.dispatcher().cancelAll();
client.connectionPool().evictAll();
if (managedExecutor) {
ExecutorService executorService = client.dispatcher().executorService();
// Use shutdownNow() to interrupt idle threads immediately since we've cancelled all work
executorService.shutdownNow();
// Wait for threads to terminate in a background thread
CompletableResultCode result = new CompletableResultCode();
Thread terminationThread =
new Thread(
() -> {
try {
// Wait up to 5 seconds for threads to terminate
// Even if timeout occurs, we succeed since these are daemon threads
boolean terminated = executorService.awaitTermination(5, TimeUnit.SECONDS);
if (!terminated) {
logger.log(
Level.WARNING,
"Executor did not terminate within 5 seconds, proceeding with shutdown since threads are daemon threads.");
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
result.succeed();
}
},
"okhttp-shutdown");
terminationThread.setDaemon(true);
terminationThread.start();
return result;
}
return CompletableResultCode.ofSuccess();
}

@Override
public CompletableResultCode shutdown() {
client.dispatcher().cancelAll();
if (managedExecutor) {
client.dispatcher().executorService().shutdownNow();
}
client.connectionPool().evictAll();
return CompletableResultCode.ofSuccess();
}

@Override
public CompletableResultCode shutdown() {
if (managedExecutor) {
executorService.shutdown();
}
if (AutoCloseable.class.isInstance(client)) {
try {
AutoCloseable.class.cast(client).close();
} catch (Exception e) {
return CompletableResultCode.ofExceptionalFailure(e);
}
}
return CompletableResultCode.ofSuccess();
}

@Override
public CompletableResultCode shutdown() {
if (shutdownChannel) {
channel.shutdownNow();
}
return CompletableResultCode.ofSuccess();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions