Skip to content

Commit 614fd4a

Browse files
committed
fix:shutdown grpc connection gracefully.
1 parent 981232b commit 614fd4a

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

polaris-plugins/polaris-plugins-connector/connector-polaris-grpc/src/main/java/com/tencent/polaris/plugins/connector/grpc/Connection.java

+24-7
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,31 @@ public void closeConnection() {
128128
if (ref.get() <= 0 && !closed) {
129129
LOG.info("connection {}: closed", connID);
130130
closed = true;
131-
ManagedChannel shutdownChan = channel.shutdownNow();
132-
if (null == shutdownChan) {
133-
return;
131+
// Gracefully shutdown the gRPC managed-channel.
132+
if (channel != null && !channel.isShutdown()) {
133+
try {
134+
channel.shutdown();
135+
if (!channel.awaitTermination(1, TimeUnit.SECONDS)) {
136+
LOG.warn("Timed out gracefully shutting down connection: {}. ", connID);
137+
}
138+
} catch (Exception e) {
139+
LOG.error("Unexpected exception while waiting for channel {} gracefully termination", connID, e);
140+
}
134141
}
135-
try {
136-
shutdownChan.awaitTermination(100, TimeUnit.MILLISECONDS);
137-
} catch (InterruptedException e) {
138-
LOG.error(String.format("interrupted while closing connection %s", connID), e);
142+
143+
// Forcefully shutdown if still not terminated.
144+
if (channel != null && !channel.isTerminated()) {
145+
try {
146+
channel.shutdownNow();
147+
if (!channel.awaitTermination(100, TimeUnit.MILLISECONDS)) {
148+
LOG.warn("Timed out forcefully shutting down connection: {}. ", connID);
149+
}
150+
LOG.debug("Success to forcefully shutdown connection: {}. ", connID);
151+
} catch (Exception e) {
152+
LOG.error("Unexpected exception while waiting for channel {} forcefully termination", connID, e);
153+
}
154+
} else {
155+
LOG.debug("Success to gracefully shutdown connection: {}. ", connID);
139156
}
140157
}
141158
}

polaris-plugins/polaris-plugins-connector/connector-polaris-grpc/src/main/java/com/tencent/polaris/plugins/connector/grpc/SpecStreamClient.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -264,9 +264,9 @@ public void onNext(ResponseProto.DiscoverResponse response) {
264264
return;
265265
}
266266
if (updateTask.getTaskType() == Type.FIRST) {
267-
LOG.info("[ServerConnector]receive response for {}", serviceEventKey);
267+
LOG.info("[ServerConnector]request(id={}) receive response for {}", getReqId(), serviceEventKey);
268268
} else {
269-
LOG.debug("[ServerConnector]receive response for {}", serviceEventKey);
269+
LOG.debug("[ServerConnector]request(id={}) receive response for {}", getReqId(), serviceEventKey);
270270
}
271271
boolean svcDeleted = updateTask.notifyServerEvent(new ServerEvent(serviceEventKey, response, null));
272272
if (!svcDeleted) {

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464

6565
<properties>
6666
<!-- Project revision -->
67-
<revision>1.12.4</revision>
67+
<revision>1.12.5</revision>
6868

6969
<timestamp>${maven.build.timestamp}</timestamp>
7070
<skip.maven.deploy>false</skip.maven.deploy>

0 commit comments

Comments
 (0)