Skip to content

Commit

Permalink
examples: Simplify graceful shutdown in Hostname example
Browse files Browse the repository at this point in the history
I've slept since I wrote the original code, so now I see a less
repetitive implementation.
  • Loading branch information
ejona86 committed Dec 11, 2024
1 parent 6055adc commit f1109e4
Showing 1 changed file with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,17 @@ public void run() {
// Start graceful shutdown
server.shutdown();
try {
// Wait for RPCs to complete processing
if (!server.awaitTermination(30, TimeUnit.SECONDS)) {
// That was plenty of time. Let's cancel the remaining RPCs
server.shutdownNow();
// shutdownNow isn't instantaneous, so give a bit of time to clean resources up
// gracefully. Normally this will be well under a second.
server.awaitTermination(5, TimeUnit.SECONDS);
}
// Wait up to 30 seconds for RPCs to complete processing.
server.awaitTermination(30, TimeUnit.SECONDS);
} catch (InterruptedException ex) {
server.shutdownNow();
Thread.currentThread().interrupt();
}
// Cancel any remaining RPCs. If awaitTermination() returned true above, then there are no
// RPCs and the server is already terminated. But it is safe to call even when terminated.
server.shutdownNow();
// shutdownNow isn't instantaneous, so you want an additional awaitTermination() to give
// time to clean resources up gracefully. Normally it will return in well under a second. In
// this example, the server.awaitTermination() in main() provides that delay.
}
});
// This would normally be tied to the service's dependencies. For example, if HostnameGreeter
Expand Down

0 comments on commit f1109e4

Please sign in to comment.