Skip to content

Commit debc688

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

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

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

Lines changed: 39 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,45 @@ 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+
}
165+
catch (final RuntimeException e) {
166+
if (quietly) {
167+
logger.warn("Failed to shutdown the metric service client", e);
168+
}
169+
else {
170+
throw e;
171+
}
172+
}
173+
catch (final InterruptedException e) {
174+
Thread.currentThread().interrupt();
175+
return;
176+
}
177+
}
178+
try {
179+
client.close();
180+
}
181+
catch (final RuntimeException e) {
182+
if (quietly) {
183+
logger.warn("Failed to close metric service client", e);
184+
}
185+
else {
186+
throw e;
187+
}
152188
}
189+
client = null;
153190
}
154191
}
155192

0 commit comments

Comments
 (0)