Skip to content
This repository was archived by the owner on Sep 26, 2023. It is now read-only.

Commit bd1714e

Browse files
authored
fix: Watchdog controls lifecycle of the future, not executor (#1890)
1 parent f0a769c commit bd1714e

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

gax/src/main/java/com/google/api/gax/rpc/Watchdog.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@
3636
import java.util.Map.Entry;
3737
import java.util.concurrent.CancellationException;
3838
import java.util.concurrent.ConcurrentHashMap;
39+
import java.util.concurrent.ExecutionException;
3940
import java.util.concurrent.ScheduledExecutorService;
4041
import java.util.concurrent.ScheduledFuture;
4142
import java.util.concurrent.TimeUnit;
43+
import java.util.concurrent.TimeoutException;
4244
import java.util.logging.Level;
4345
import java.util.logging.Logger;
4446
import javax.annotation.Nonnull;
@@ -61,6 +63,7 @@
6163
* </ul>
6264
*/
6365
public final class Watchdog implements Runnable, BackgroundResource {
66+
6467
private static final Logger LOG = Logger.getLogger(Watchdog.class.getName());
6568

6669
// Dummy value to convert the ConcurrentHashMap into a Set
@@ -138,12 +141,12 @@ public void shutdown() {
138141

139142
@Override
140143
public boolean isShutdown() {
141-
return executor.isShutdown();
144+
return future.isCancelled();
142145
}
143146

144147
@Override
145148
public boolean isTerminated() {
146-
return executor.isTerminated();
149+
return future.isDone();
147150
}
148151

149152
@Override
@@ -153,7 +156,14 @@ public void shutdownNow() {
153156

154157
@Override
155158
public boolean awaitTermination(long duration, TimeUnit unit) throws InterruptedException {
156-
return executor.awaitTermination(duration, unit);
159+
try {
160+
future.get(duration, unit);
161+
return true;
162+
} catch (ExecutionException | CancellationException e) {
163+
return true;
164+
} catch (TimeoutException e) {
165+
return false;
166+
}
157167
}
158168

159169
@Override

gax/src/main/java/com/google/api/gax/rpc/WatchdogProvider.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,6 @@ public interface WatchdogProvider {
4949

5050
Watchdog getWatchdog();
5151

52+
/** Return true if the watchdog should be automatically unscheduled. */
5253
boolean shouldAutoClose();
5354
}

0 commit comments

Comments
 (0)