Skip to content

Commit

Permalink
fix:shutdown grpc connection gracefully.
Browse files Browse the repository at this point in the history
  • Loading branch information
SkyeBeFreeman committed May 12, 2023
1 parent 981232b commit 614fd4a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,31 @@ public void closeConnection() {
if (ref.get() <= 0 && !closed) {
LOG.info("connection {}: closed", connID);
closed = true;
ManagedChannel shutdownChan = channel.shutdownNow();
if (null == shutdownChan) {
return;
// Gracefully shutdown the gRPC managed-channel.
if (channel != null && !channel.isShutdown()) {
try {
channel.shutdown();
if (!channel.awaitTermination(1, TimeUnit.SECONDS)) {
LOG.warn("Timed out gracefully shutting down connection: {}. ", connID);
}
} catch (Exception e) {
LOG.error("Unexpected exception while waiting for channel {} gracefully termination", connID, e);
}
}
try {
shutdownChan.awaitTermination(100, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
LOG.error(String.format("interrupted while closing connection %s", connID), e);

// Forcefully shutdown if still not terminated.
if (channel != null && !channel.isTerminated()) {
try {
channel.shutdownNow();
if (!channel.awaitTermination(100, TimeUnit.MILLISECONDS)) {
LOG.warn("Timed out forcefully shutting down connection: {}. ", connID);
}
LOG.debug("Success to forcefully shutdown connection: {}. ", connID);
} catch (Exception e) {
LOG.error("Unexpected exception while waiting for channel {} forcefully termination", connID, e);
}
} else {
LOG.debug("Success to gracefully shutdown connection: {}. ", connID);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,9 @@ public void onNext(ResponseProto.DiscoverResponse response) {
return;
}
if (updateTask.getTaskType() == Type.FIRST) {
LOG.info("[ServerConnector]receive response for {}", serviceEventKey);
LOG.info("[ServerConnector]request(id={}) receive response for {}", getReqId(), serviceEventKey);
} else {
LOG.debug("[ServerConnector]receive response for {}", serviceEventKey);
LOG.debug("[ServerConnector]request(id={}) receive response for {}", getReqId(), serviceEventKey);
}
boolean svcDeleted = updateTask.notifyServerEvent(new ServerEvent(serviceEventKey, response, null));
if (!svcDeleted) {
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@

<properties>
<!-- Project revision -->
<revision>1.12.4</revision>
<revision>1.12.5</revision>

<timestamp>${maven.build.timestamp}</timestamp>
<skip.maven.deploy>false</skip.maven.deploy>
Expand Down

0 comments on commit 614fd4a

Please sign in to comment.