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

Commit

Permalink
fix: Watchdog controls lifecycle of the future, not executor (#1890)
Browse files Browse the repository at this point in the history
  • Loading branch information
meltsufin authored and blakeli0 committed Dec 9, 2022
1 parent f7caf21 commit 0d2fadf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
16 changes: 13 additions & 3 deletions gax/src/main/java/com/google/api/gax/rpc/Watchdog.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@
import java.util.Map.Entry;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nonnull;
Expand All @@ -61,6 +63,7 @@
* </ul>
*/
public final class Watchdog implements Runnable, BackgroundResource {

private static final Logger LOG = Logger.getLogger(Watchdog.class.getName());

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

@Override
public boolean isShutdown() {
return executor.isShutdown();
return future.isCancelled();
}

@Override
public boolean isTerminated() {
return executor.isTerminated();
return future.isDone();
}

@Override
Expand All @@ -153,7 +156,14 @@ public void shutdownNow() {

@Override
public boolean awaitTermination(long duration, TimeUnit unit) throws InterruptedException {
return executor.awaitTermination(duration, unit);
try {
future.get(duration, unit);
return true;
} catch (ExecutionException | CancellationException e) {
return true;
} catch (TimeoutException e) {
return false;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,6 @@ public interface WatchdogProvider {

Watchdog getWatchdog();

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

0 comments on commit 0d2fadf

Please sign in to comment.