Fixes #2291 - Expose HTTP/2 close reason in dumps.

Signed-off-by: Simone Bordet <simone.bordet@gmail.com>
This commit is contained in:
Simone Bordet 2018-03-07 00:45:23 +01:00
parent 8fcfdd9d7f
commit 5dea9b673e
2 changed files with 21 additions and 9 deletions

View File

@ -89,6 +89,7 @@ public abstract class HTTP2Session extends ContainerLifeCycle implements ISessio
private int initialSessionRecvWindow; private int initialSessionRecvWindow;
private boolean pushEnabled; private boolean pushEnabled;
private long idleTime; private long idleTime;
private GoAwayFrame closeFrame;
public HTTP2Session(Scheduler scheduler, EndPoint endPoint, Generator generator, Session.Listener listener, FlowControlStrategy flowControl, int initialStreamId) public HTTP2Session(Scheduler scheduler, EndPoint endPoint, Generator generator, Session.Listener listener, FlowControlStrategy flowControl, int initialStreamId)
{ {
@ -430,6 +431,7 @@ public abstract class HTTP2Session extends ContainerLifeCycle implements ISessio
{ {
// We received a GO_AWAY, so try to write // We received a GO_AWAY, so try to write
// what's in the queue and then disconnect. // what's in the queue and then disconnect.
closeFrame = frame;
notifyClose(this, frame, new DisconnectCallback()); notifyClose(this, frame, new DisconnectCallback());
return; return;
} }
@ -597,8 +599,8 @@ public abstract class HTTP2Session extends ContainerLifeCycle implements ISessio
{ {
if (closed.compareAndSet(current, CloseState.LOCALLY_CLOSED)) if (closed.compareAndSet(current, CloseState.LOCALLY_CLOSED))
{ {
GoAwayFrame frame = newGoAwayFrame(error, reason); closeFrame = newGoAwayFrame(CloseState.LOCALLY_CLOSED, error, reason);
control(null, callback, frame); control(null, callback, closeFrame);
return true; return true;
} }
break; break;
@ -614,7 +616,7 @@ public abstract class HTTP2Session extends ContainerLifeCycle implements ISessio
} }
} }
private GoAwayFrame newGoAwayFrame(int error, String reason) private GoAwayFrame newGoAwayFrame(CloseState closeState, int error, String reason)
{ {
byte[] payload = null; byte[] payload = null;
if (reason != null) if (reason != null)
@ -623,7 +625,7 @@ public abstract class HTTP2Session extends ContainerLifeCycle implements ISessio
reason = reason.substring(0, Math.min(reason.length(), 32)); reason = reason.substring(0, Math.min(reason.length(), 32));
payload = reason.getBytes(StandardCharsets.UTF_8); payload = reason.getBytes(StandardCharsets.UTF_8);
} }
return new GoAwayFrame(lastStreamId.get(), error, payload); return new GoAwayFrame(closeState, lastStreamId.get(), error, payload);
} }
@Override @Override
@ -1125,7 +1127,7 @@ public abstract class HTTP2Session extends ContainerLifeCycle implements ISessio
@Override @Override
public String toString() public String toString()
{ {
return String.format("%s@%x{l:%s <-> r:%s,sendWindow=%s,recvWindow=%s,streams=%d,%s}", return String.format("%s@%x{l:%s <-> r:%s,sendWindow=%s,recvWindow=%s,streams=%d,%s,%s}",
getClass().getSimpleName(), getClass().getSimpleName(),
hashCode(), hashCode(),
getEndPoint().getLocalAddress(), getEndPoint().getLocalAddress(),
@ -1133,7 +1135,8 @@ public abstract class HTTP2Session extends ContainerLifeCycle implements ISessio
sendWindow, sendWindow,
recvWindow, recvWindow,
streams.size(), streams.size(),
closed); closed,
closeFrame);
} }
private class ControlEntry extends HTTP2Flusher.Entry private class ControlEntry extends HTTP2Flusher.Entry
@ -1453,7 +1456,7 @@ public abstract class HTTP2Session extends ContainerLifeCycle implements ISessio
private void complete() private void complete()
{ {
frames(null, Callback.NOOP, newGoAwayFrame(ErrorCode.NO_ERROR.code, null), new DisconnectFrame()); frames(null, Callback.NOOP, newGoAwayFrame(CloseState.CLOSED, ErrorCode.NO_ERROR.code, null), new DisconnectFrame());
} }
} }

View File

@ -20,17 +20,25 @@ package org.eclipse.jetty.http2.frames;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import org.eclipse.jetty.http2.CloseState;
import org.eclipse.jetty.http2.ErrorCode; import org.eclipse.jetty.http2.ErrorCode;
public class GoAwayFrame extends Frame public class GoAwayFrame extends Frame
{ {
private final CloseState closeState;
private final int lastStreamId; private final int lastStreamId;
private final int error; private final int error;
private final byte[] payload; private final byte[] payload;
public GoAwayFrame(int lastStreamId, int error, byte[] payload) public GoAwayFrame(int lastStreamId, int error, byte[] payload)
{
this(CloseState.REMOTELY_CLOSED, lastStreamId, error, payload);
}
public GoAwayFrame(CloseState closeState, int lastStreamId, int error, byte[] payload)
{ {
super(FrameType.GO_AWAY); super(FrameType.GO_AWAY);
this.closeState = closeState;
this.lastStreamId = lastStreamId; this.lastStreamId = lastStreamId;
this.error = error; this.error = error;
this.payload = payload; this.payload = payload;
@ -69,10 +77,11 @@ public class GoAwayFrame extends Frame
public String toString() public String toString()
{ {
ErrorCode errorCode = ErrorCode.from(error); ErrorCode errorCode = ErrorCode.from(error);
return String.format("%s,%d/%s/%s", return String.format("%s,%d/%s/%s/%s",
super.toString(), super.toString(),
lastStreamId, lastStreamId,
errorCode != null ? errorCode.toString() : String.valueOf(error), errorCode != null ? errorCode.toString() : String.valueOf(error),
tryConvertPayload()); tryConvertPayload(),
closeState);
} }
} }