Skip to content

Commit a5c9859

Browse files
committed
close the client upon start if necessary so that multiple stop/start cycles would not leak clients
1 parent bf45f15 commit a5c9859

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

implementations/micrometer-registry-stackdriver/src/main/java/io/micrometer/stackdriver/StackdriverMeterRegistry.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ public void start(ThreadFactory threadFactory) {
125125
logger.error("unable to start stackdriver, service settings are not available");
126126
}
127127
else {
128+
shutdownClientIfNecessary(true);
128129
try {
129130
this.client = MetricServiceClient.create(metricServiceSettings);
130131
super.start(threadFactory);
@@ -147,9 +148,40 @@ public void close() {
147148
super.close();
148149
}
149150
finally {
150-
if (client != null) {
151-
client.shutdownNow();
151+
shutdownClientIfNecessary(false);
152+
}
153+
}
154+
155+
protected void shutdownClientIfNecessary(final boolean quietly) {
156+
if (client != null) {
157+
if (!client.isShutdown()) {
158+
try {
159+
client.shutdownNow();
160+
final boolean terminated = client.awaitTermination(10, TimeUnit.SECONDS);
161+
if (!terminated) {
162+
logger.warn("The metric service client failed to terminate within the timeout");
163+
}
164+
} catch (final RuntimeException e) {
165+
if (quietly) {
166+
logger.warn("Failed to shutdown the metric service client", e);
167+
} else {
168+
throw e;
169+
}
170+
} catch (final InterruptedException e) {
171+
Thread.currentThread().interrupt();
172+
return;
173+
}
174+
}
175+
try {
176+
client.close();
177+
} catch (final RuntimeException e) {
178+
if (quietly) {
179+
logger.warn("Failed to close metric service client", e);
180+
} else {
181+
throw e;
182+
}
152183
}
184+
client = null;
153185
}
154186
}
155187

0 commit comments

Comments
 (0)