Skip to content

Commit

Permalink
Close Stackdriver client after closing registry (#4358)
Browse files Browse the repository at this point in the history
Do not close the client before `close` has finished as `close` publishes the metrics one last time. Close the client upon start if necessary so that multiple stop/start cycles would not leak clients.

Fixes gh-4353

---------

Co-authored-by: Dimo Velev <dimo.velev1_ext@payback.net>
  • Loading branch information
dimovelev and pb-dimo committed Jan 31, 2024
1 parent 23ca2fc commit f53d61a
Showing 1 changed file with 49 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public void start(ThreadFactory threadFactory) {
logger.error("unable to start stackdriver, service settings are not available");
}
else {
shutdownClientIfNecessary(true);
try {
this.client = MetricServiceClient.create(metricServiceSettings);
super.start(threadFactory);
Expand All @@ -138,11 +139,57 @@ public void start(ThreadFactory threadFactory) {

@Override
public void stop() {
if (client != null)
client.shutdownNow();
super.stop();
}

@Override
public void close() {
try {
super.close();
}
finally {
shutdownClientIfNecessary(false);
}
}

protected void shutdownClientIfNecessary(final boolean quietly) {
if (client != null) {
if (!client.isShutdown()) {
try {
client.shutdownNow();
final boolean terminated = client.awaitTermination(10, TimeUnit.SECONDS);
if (!terminated) {
logger.warn("The metric service client failed to terminate within the timeout");
}
}
catch (final RuntimeException e) {
if (quietly) {
logger.warn("Failed to shutdown the metric service client", e);
}
else {
throw e;
}
}
catch (final InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
}
try {
client.close();
}
catch (final RuntimeException e) {
if (quietly) {
logger.warn("Failed to close metric service client", e);
}
else {
throw e;
}
}
client = null;
}
}

@Override
protected void publish() {
if (client == null) {
Expand Down

0 comments on commit f53d61a

Please sign in to comment.