Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
panxiaojun233 committed Jan 18, 2021
1 parent 79b73a7 commit 7fc0d46
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
*/
package org.apache.dubbo.remoting.api;

import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import io.netty.util.concurrent.ImmediateEventExecutor;
import io.netty.util.concurrent.Promise;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.common.logger.Logger;
Expand Down Expand Up @@ -43,6 +47,7 @@
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;

import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.apache.dubbo.common.constants.CommonConstants.ANYHOST_KEY;
Expand Down Expand Up @@ -151,11 +156,25 @@ protected void doClose() throws Throwable {
channel.close();
channel = null;
}

channelGroup.close();
Thread.sleep(15000);
} catch (Throwable e) {
logger.warn(e.getMessage(), e);
long start = System.currentTimeMillis();
channelGroup.close().addListener(new GenericFutureListener<Future<? super Void>>() {
@Override
public void operationComplete(final Future future) throws Exception {
if (!future.isSuccess()) {
logger.warn("Error shutting down server", future.cause());
}
System.out.println("shut operationComplete..");
long now = System.currentTimeMillis();
while (now - start > 15000 || channelGroup.size() > 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
}
}
}).await();
} catch (InterruptedException e) {
logger.warn("Interrupted while shutting down", e);
}

for (WireProtocol protocol : protocols) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,37 @@
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import io.netty.handler.codec.http2.Http2ChannelDuplexHandler;
import io.netty.handler.codec.http2.Http2PingFrame;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;

import static org.apache.dubbo.rpc.protocol.tri.GracefulShutdown.GRACEFUL_SHUTDOWN_PING;

public class GracefulShutdownHandler extends Http2ChannelDuplexHandler {
private GracefulShutdown gracefulShutdown;
private static final Logger logger = LoggerFactory.getLogger(GracefulShutdownHandler.class);

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
super.channelRead(ctx, msg);

if (msg instanceof Http2PingFrame) {
if (((Http2PingFrame)msg).content() == GRACEFUL_SHUTDOWN_PING) {
if (gracefulShutdown == null) {
// this should never happen
logger.warn("Received GRACEFUL_SHUTDOWN_PING Ack but gracefulShutdown is null");
} else {
gracefulShutdown.secondGoAwayAndClose(ctx);
}
}
}
}

@Override
public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
new GracefulShutdown(ctx,"app_requested", null).gracefulShutdown();
if (gracefulShutdown == null) {
gracefulShutdown = new GracefulShutdown(ctx,"app_requested", null);
gracefulShutdown.gracefulShutdown();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)
}
}

@Override
public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
if (ctx.channel().isActive()) { // Ignore notification that the socket was closed
final Connection connection = Connection.getConnectionFromChannel(ctx.channel());
if (connection != null) {
connection.onIdle();
}
}
super.close(ctx, promise);
}

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if(msg instanceof Http2SettingsFrame){
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.apache.dubbo.rpc.protocol.tri;

import io.netty.handler.codec.http2.Http2PingFrame;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.common.logger.Logger;
Expand All @@ -21,6 +22,7 @@
import io.netty.handler.codec.http2.Http2Headers;
import io.netty.handler.codec.http2.Http2HeadersFrame;

import static org.apache.dubbo.rpc.protocol.tri.GracefulShutdown.GRACEFUL_SHUTDOWN_PING;
import static org.apache.dubbo.rpc.protocol.tri.TripleUtil.responseErr;
import static org.apache.dubbo.rpc.protocol.tri.TripleUtil.responsePlainTextError;

Expand Down

0 comments on commit 7fc0d46

Please sign in to comment.