Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more debug information to HTTP/2 GOAWAY frame #145

Merged
merged 1 commit into from
Apr 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@

package com.linecorp.armeria.common.http;

import static io.netty.handler.codec.http2.Http2Error.INTERNAL_ERROR;

import com.linecorp.armeria.common.util.Exceptions;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import io.netty.handler.codec.http2.Http2ConnectionDecoder;
import io.netty.handler.codec.http2.Http2ConnectionEncoder;
import io.netty.handler.codec.http2.Http2ConnectionHandler;
import io.netty.handler.codec.http2.Http2Exception;
import io.netty.handler.codec.http2.Http2Exception.ClosedStreamCreationException;
import io.netty.handler.codec.http2.Http2Settings;
import io.netty.handler.codec.http2.Http2Stream.State;
import io.netty.handler.codec.http2.Http2StreamVisitor;
Expand All @@ -43,8 +48,10 @@ public abstract class AbstractHttpToHttp2ConnectionHandler extends HttpToHttp2Co
private boolean closing;
private boolean handlingConnectionError;

protected AbstractHttpToHttp2ConnectionHandler(Http2ConnectionDecoder decoder, Http2ConnectionEncoder encoder,
Http2Settings initialSettings, boolean validateHeaders) {
protected AbstractHttpToHttp2ConnectionHandler(
Http2ConnectionDecoder decoder, Http2ConnectionEncoder encoder,
Http2Settings initialSettings, boolean validateHeaders) {

super(decoder, encoder, initialSettings, validateHeaders);
}

Expand All @@ -59,9 +66,45 @@ protected void onConnectionError(ChannelHandlerContext ctx, Throwable cause, Htt
}

handlingConnectionError = true;

// TODO(trustin): Remove this once Http2ConnectionHandler.goAway() sends better debugData.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any upstream issue to reference?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. Will add a link.

// See https://github.com/netty/netty/issues/5160
if (http2Ex == null) {
http2Ex = new Http2Exception(INTERNAL_ERROR, goAwayDebugData(null, cause), cause);
} else if (http2Ex instanceof ClosedStreamCreationException) {
final ClosedStreamCreationException e = (ClosedStreamCreationException) http2Ex;
http2Ex = new ClosedStreamCreationException(e.error(), goAwayDebugData(e, cause), cause);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly to clarify, is there a difference between cause and http2Ex.cause here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are same as far as I know.

} else {
http2Ex = new Http2Exception(
http2Ex.error(), goAwayDebugData(http2Ex, cause), cause, http2Ex.shutdownHint());
}

super.onConnectionError(ctx, cause, http2Ex);
}

private static String goAwayDebugData(Http2Exception http2Ex, Throwable cause) {
final StringBuilder buf = new StringBuilder(256);
final String type;
final String message;

if (http2Ex != null) {
type = http2Ex.getClass().getName();
message = http2Ex.getMessage();
} else {
type = null;
message = null;
}

buf.append("type: ");
buf.append(type != null ? type : "n/a");
buf.append(", message: ");
buf.append(message != null ? message : "n/a");
buf.append(", cause: ");
buf.append(cause != null ? Exceptions.traceText(cause) : "n/a");

return buf.toString();
}

@Override
public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
closing = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ ch, sentOrReceived, lastStreamId, errorStr(errorCode),
ByteBufUtil.hexDump(debugData));
}
} else {
if (logger.isInfoEnabled()) {
if (logger.isDebugEnabled()) {
logger.debug("{} {} a GOAWAY frame: lastStreamId={}, errorCode=NO_ERROR",
ch, sentOrReceived, lastStreamId);
}
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/linecorp/armeria/common/util/Exceptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import static java.util.Objects.requireNonNull;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.channels.ClosedChannelException;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -118,5 +120,17 @@ public static <T extends Throwable> T clearTrace(T exception) {
return exception;
}

/**
* Returns the stack trace of the specified {@code exception} as a {@link String}.
*/
public static String traceText(Throwable exception) {
requireNonNull(exception, "exception");
final StringWriter out = new StringWriter(256);
final PrintWriter pout = new PrintWriter(out);
exception.printStackTrace(pout);
pout.flush();
return out.toString();
}

private Exceptions() {}
}