Issue #922 - Implements methods Connection.getBytes[In|Out]().

Implemented for HTTP/2.
This commit is contained in:
Simone Bordet 2016-09-14 14:04:23 +02:00
parent 571b0c74c5
commit aa00e4207d
23 changed files with 287 additions and 56 deletions

View File

@ -118,6 +118,20 @@ public class HTTP2ClientConnectionFactory implements ClientConnectionFactory
this.listener = listener;
}
@Override
public int getMessagesIn()
{
HTTP2ClientSession session = (HTTP2ClientSession)getSession();
return (int)session.getStreamsOpened();
}
@Override
public int getMessagesOut()
{
HTTP2ClientSession session = (HTTP2ClientSession)getSession();
return (int)session.getStreamsClosed();
}
@Override
public void onOpen()
{

View File

@ -18,6 +18,8 @@
package org.eclipse.jetty.http2.client;
import java.util.concurrent.atomic.AtomicLong;
import org.eclipse.jetty.http2.FlowControlStrategy;
import org.eclipse.jetty.http2.HTTP2Session;
import org.eclipse.jetty.http2.IStream;
@ -36,11 +38,38 @@ public class HTTP2ClientSession extends HTTP2Session
{
private static final Logger LOG = Log.getLogger(HTTP2ClientSession.class);
private final AtomicLong streamsOpened = new AtomicLong();
private final AtomicLong streamsClosed = new AtomicLong();
public HTTP2ClientSession(Scheduler scheduler, EndPoint endPoint, Generator generator, Session.Listener listener, FlowControlStrategy flowControl)
{
super(scheduler, endPoint, generator, listener, flowControl, 1);
}
@Override
protected void onStreamOpened(IStream stream)
{
super.onStreamOpened(stream);
streamsOpened.incrementAndGet();
}
@Override
protected void onStreamClosed(IStream stream)
{
super.onStreamClosed(stream);
streamsClosed.incrementAndGet();
}
public long getStreamsOpened()
{
return streamsOpened.get();
}
public long getStreamsClosed()
{
return streamsClosed.get();
}
@Override
public void onHeaders(HeadersFrame frame)
{

View File

@ -22,6 +22,7 @@ import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Queue;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicLong;
import org.eclipse.jetty.http2.parser.Parser;
import org.eclipse.jetty.io.AbstractConnection;
@ -39,11 +40,12 @@ public class HTTP2Connection extends AbstractConnection
protected static final Logger LOG = Log.getLogger(HTTP2Connection.class);
private final Queue<Runnable> tasks = new ConcurrentArrayQueue<>();
private final HTTP2Producer producer = new HTTP2Producer();
private final AtomicLong bytesIn = new AtomicLong();
private final ByteBufferPool byteBufferPool;
private final Parser parser;
private final ISession session;
private final int bufferSize;
private final HTTP2Producer producer = new HTTP2Producer();
private final ExecutionStrategy executionStrategy;
public HTTP2Connection(ByteBufferPool byteBufferPool, Executor executor, EndPoint endPoint, Parser parser, ISession session, int bufferSize, ExecutionStrategy.Factory executionFactory)
@ -56,12 +58,23 @@ public class HTTP2Connection extends AbstractConnection
this.executionStrategy = executionFactory.newExecutionStrategy(producer, executor);
}
@Override
public long getBytesIn()
{
return bytesIn.get();
}
@Override
public long getBytesOut()
{
return session.getBytesWritten();
}
public ISession getSession()
{
return session;
}
protected Parser getParser()
{
return parser;
@ -195,6 +208,10 @@ public class HTTP2Connection extends AbstractConnection
session.onShutdown();
return null;
}
else
{
bytesIn.addAndGet(filled);
}
looping = true;
}

View File

@ -31,6 +31,7 @@ import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import org.eclipse.jetty.http2.api.Session;
@ -75,6 +76,7 @@ public abstract class HTTP2Session extends ContainerLifeCycle implements ISessio
private final AtomicInteger sendWindow = new AtomicInteger();
private final AtomicInteger recvWindow = new AtomicInteger();
private final AtomicReference<CloseState> closed = new AtomicReference<>(CloseState.NOT_CLOSED);
private final AtomicLong bytesWritten = new AtomicLong();
private final Scheduler scheduler;
private final EndPoint endPoint;
private final Generator generator;
@ -191,6 +193,12 @@ public abstract class HTTP2Session extends ContainerLifeCycle implements ISessio
return generator;
}
@Override
public long getBytesWritten()
{
return bytesWritten.get();
}
@Override
public void onData(final DataFrame frame)
{
@ -746,6 +754,8 @@ public abstract class HTTP2Session extends ContainerLifeCycle implements ISessio
else
remoteStreamCount.decrementAndGet();
onStreamClosed(stream);
flowControl.onStreamDestroyed(stream);
if (LOG.isDebugEnabled())
@ -934,6 +944,14 @@ public abstract class HTTP2Session extends ContainerLifeCycle implements ISessio
onConnectionFailure(ErrorCode.PROTOCOL_ERROR.code, "upgrade");
}
protected void onStreamOpened(IStream stream)
{
}
protected void onStreamClosed(IStream stream)
{
}
public void disconnect()
{
if (LOG.isDebugEnabled())
@ -1090,6 +1108,8 @@ public abstract class HTTP2Session extends ContainerLifeCycle implements ISessio
private class ControlEntry extends HTTP2Flusher.Entry
{
private long bytes;
private ControlEntry(Frame frame, IStream stream, Callback callback)
{
super(frame, stream, callback);
@ -1099,7 +1119,7 @@ public abstract class HTTP2Session extends ContainerLifeCycle implements ISessio
{
try
{
generator.control(lease, frame);
bytes = generator.control(lease, frame);
if (LOG.isDebugEnabled())
LOG.debug("Generated {}", frame);
prepare();
@ -1148,10 +1168,12 @@ public abstract class HTTP2Session extends ContainerLifeCycle implements ISessio
@Override
public void succeeded()
{
bytesWritten.addAndGet(bytes);
switch (frame.getType())
{
case HEADERS:
{
onStreamOpened(stream);
HeadersFrame headersFrame = (HeadersFrame)frame;
if (stream.updateClose(headersFrame.isEndStream(), true))
removeStream(stream);
@ -1202,6 +1224,7 @@ public abstract class HTTP2Session extends ContainerLifeCycle implements ISessio
private class DataEntry extends HTTP2Flusher.Entry
{
private int length;
private long bytes;
private DataEntry(DataFrame frame, IStream stream, Callback callback)
{
@ -1239,7 +1262,7 @@ public abstract class HTTP2Session extends ContainerLifeCycle implements ISessio
if (LOG.isDebugEnabled())
LOG.debug("Generated {}, length/window={}/{}", frame, length, window);
generator.data(lease, (DataFrame)frame, length);
bytes = generator.data(lease, (DataFrame)frame, length);
flowControl.onDataSending(stream, length);
return null;
}
@ -1254,6 +1277,7 @@ public abstract class HTTP2Session extends ContainerLifeCycle implements ISessio
@Override
public void succeeded()
{
bytesWritten.addAndGet(bytes);
flowControl.onDataSent(stream, length);
// Do we have more to send ?
DataFrame dataFrame = (DataFrame)frame;

View File

@ -127,4 +127,9 @@ public interface ISession extends Session
* @param frame the synthetic frame to process
*/
public void onFrame(Frame frame);
/**
* @return the number of bytes written by this session
*/
public long getBytesWritten();
}

View File

@ -24,6 +24,8 @@ import org.eclipse.jetty.http2.ErrorCode;
public class ResetFrame extends Frame
{
public static final int RESET_LENGTH = 4;
private final int streamId;
private final int error;

View File

@ -20,6 +20,8 @@ package org.eclipse.jetty.http2.frames;
public class WindowUpdateFrame extends Frame
{
public static final int WINDOW_UPDATE_LENGTH = 4;
private final int streamId;
private final int windowDelta;

View File

@ -36,12 +36,12 @@ public class DataGenerator
this.headerGenerator = headerGenerator;
}
public void generate(ByteBufferPool.Lease lease, DataFrame frame, int maxLength)
public long generate(ByteBufferPool.Lease lease, DataFrame frame, int maxLength)
{
generateData(lease, frame.getStreamId(), frame.getData(), frame.isEndStream(), maxLength);
return generateData(lease, frame.getStreamId(), frame.getData(), frame.isEndStream(), maxLength);
}
public void generateData(ByteBufferPool.Lease lease, int streamId, ByteBuffer data, boolean last, int maxLength)
public long generateData(ByteBufferPool.Lease lease, int streamId, ByteBuffer data, boolean last, int maxLength)
{
if (streamId < 0)
throw new IllegalArgumentException("Invalid stream id: " + streamId);
@ -51,8 +51,7 @@ public class DataGenerator
if (dataLength <= maxLength && dataLength <= maxFrameSize)
{
// Single frame.
generateFrame(lease, streamId, data, last);
return;
return generateFrame(lease, streamId, data, last);
}
// Other cases, we need to slice the original buffer into multiple frames.
@ -62,6 +61,7 @@ public class DataGenerator
if (frames * maxFrameSize != length)
++frames;
long totalLength = 0;
int begin = data.position();
int end = data.limit();
for (int i = 1; i <= frames; ++i)
@ -70,12 +70,14 @@ public class DataGenerator
data.limit(limit);
ByteBuffer slice = data.slice();
data.position(limit);
generateFrame(lease, streamId, slice, i == frames && last && limit == end);
totalLength += generateFrame(lease, streamId, slice, i == frames && last && limit == end);
}
data.limit(end);
return totalLength;
}
private void generateFrame(ByteBufferPool.Lease lease, int streamId, ByteBuffer data, boolean last)
private long generateFrame(ByteBufferPool.Lease lease, int streamId, ByteBuffer data, boolean last)
{
int length = data.remaining();
@ -84,10 +86,10 @@ public class DataGenerator
flags |= Flags.END_STREAM;
ByteBuffer header = headerGenerator.generate(lease, FrameType.DATA, Frame.HEADER_LENGTH + length, length, flags, streamId);
BufferUtil.flipToFlush(header, 0);
lease.append(header, true);
lease.append(data, false);
return Frame.HEADER_LENGTH + length;
}
}

View File

@ -29,7 +29,8 @@ public class DisconnectGenerator extends FrameGenerator
}
@Override
public void generate(ByteBufferPool.Lease lease, Frame frame)
public long generate(ByteBufferPool.Lease lease, Frame frame)
{
return 0;
}
}

View File

@ -33,7 +33,7 @@ public abstract class FrameGenerator
this.headerGenerator = headerGenerator;
}
public abstract void generate(ByteBufferPool.Lease lease, Frame frame);
public abstract long generate(ByteBufferPool.Lease lease, Frame frame);
protected ByteBuffer generateHeader(ByteBufferPool.Lease lease, FrameType frameType, int length, int flags, int streamId)
{

View File

@ -75,14 +75,14 @@ public class Generator
headerGenerator.setMaxFrameSize(maxFrameSize);
}
public void control(ByteBufferPool.Lease lease, Frame frame)
public long control(ByteBufferPool.Lease lease, Frame frame)
{
generators[frame.getType().getType()].generate(lease, frame);
return generators[frame.getType().getType()].generate(lease, frame);
}
public void data(ByteBufferPool.Lease lease, DataFrame frame, int maxLength)
public long data(ByteBufferPool.Lease lease, DataFrame frame, int maxLength)
{
dataGenerator.generate(lease, frame, maxLength);
return dataGenerator.generate(lease, frame, maxLength);
}
public void setMaxHeaderListSize(int value)

View File

@ -36,13 +36,13 @@ public class GoAwayGenerator extends FrameGenerator
}
@Override
public void generate(ByteBufferPool.Lease lease, Frame frame)
public long generate(ByteBufferPool.Lease lease, Frame frame)
{
GoAwayFrame goAwayFrame = (GoAwayFrame)frame;
generateGoAway(lease, goAwayFrame.getLastStreamId(), goAwayFrame.getError(), goAwayFrame.getPayload());
return generateGoAway(lease, goAwayFrame.getLastStreamId(), goAwayFrame.getError(), goAwayFrame.getPayload());
}
public void generateGoAway(ByteBufferPool.Lease lease, int lastStreamId, int error, byte[] payload)
public long generateGoAway(ByteBufferPool.Lease lease, int lastStreamId, int error, byte[] payload)
{
if (lastStreamId < 0)
throw new IllegalArgumentException("Invalid last stream id: " + lastStreamId);
@ -62,11 +62,11 @@ public class GoAwayGenerator extends FrameGenerator
header.putInt(error);
if (payload != null)
{
header.put(payload);
}
BufferUtil.flipToFlush(header, 0);
lease.append(header, true);
return Frame.HEADER_LENGTH + length;
}
}

View File

@ -50,13 +50,13 @@ public class HeadersGenerator extends FrameGenerator
}
@Override
public void generate(ByteBufferPool.Lease lease, Frame frame)
public long generate(ByteBufferPool.Lease lease, Frame frame)
{
HeadersFrame headersFrame = (HeadersFrame)frame;
generateHeaders(lease, headersFrame.getStreamId(), headersFrame.getMetaData(), headersFrame.getPriority(), headersFrame.isEndStream());
return generateHeaders(lease, headersFrame.getStreamId(), headersFrame.getMetaData(), headersFrame.getPriority(), headersFrame.isEndStream());
}
public void generateHeaders(ByteBufferPool.Lease lease, int streamId, MetaData metaData, PriorityFrame priority, boolean endStream)
public long generateHeaders(ByteBufferPool.Lease lease, int streamId, MetaData metaData, PriorityFrame priority, boolean endStream)
{
if (streamId < 0)
throw new IllegalArgumentException("Invalid stream id: " + streamId);
@ -87,10 +87,11 @@ public class HeadersGenerator extends FrameGenerator
generatePriority(header, priority);
BufferUtil.flipToFlush(header, 0);
lease.append(header, true);
hpacked.limit(maxHeaderBlockFragment);
lease.append(hpacked.slice(), false);
int totalLength = Frame.HEADER_LENGTH + length;
int position = maxHeaderBlockFragment;
int limit = position + maxHeaderBlockFragment;
while (limit < hpackedLength)
@ -102,6 +103,7 @@ public class HeadersGenerator extends FrameGenerator
lease.append(hpacked.slice(), false);
position += maxHeaderBlockFragment;
limit += maxHeaderBlockFragment;
totalLength += Frame.HEADER_LENGTH + maxHeaderBlockFragment;
}
hpacked.position(position).limit(hpackedLength);
@ -109,6 +111,9 @@ public class HeadersGenerator extends FrameGenerator
BufferUtil.flipToFlush(header, 0);
lease.append(header, true);
lease.append(hpacked, true);
totalLength += Frame.HEADER_LENGTH + hpacked.remaining();
return totalLength;
}
else
{
@ -125,6 +130,8 @@ public class HeadersGenerator extends FrameGenerator
BufferUtil.flipToFlush(header, 0);
lease.append(header, true);
lease.append(hpacked, true);
return Frame.HEADER_LENGTH + length;
}
}

View File

@ -35,13 +35,13 @@ public class PingGenerator extends FrameGenerator
}
@Override
public void generate(ByteBufferPool.Lease lease, Frame frame)
public long generate(ByteBufferPool.Lease lease, Frame frame)
{
PingFrame pingFrame = (PingFrame)frame;
generatePing(lease, pingFrame.getPayload(), pingFrame.isReply());
return generatePing(lease, pingFrame.getPayload(), pingFrame.isReply());
}
public void generatePing(ByteBufferPool.Lease lease, byte[] payload, boolean reply)
public long generatePing(ByteBufferPool.Lease lease, byte[] payload, boolean reply)
{
if (payload.length != PingFrame.PING_LENGTH)
throw new IllegalArgumentException("Invalid payload length: " + payload.length);
@ -52,5 +52,7 @@ public class PingGenerator extends FrameGenerator
BufferUtil.flipToFlush(header, 0);
lease.append(header, true);
return Frame.HEADER_LENGTH + PingFrame.PING_LENGTH;
}
}

View File

@ -32,8 +32,9 @@ public class PrefaceGenerator extends FrameGenerator
}
@Override
public void generate(ByteBufferPool.Lease lease, Frame frame)
public long generate(ByteBufferPool.Lease lease, Frame frame)
{
lease.append(ByteBuffer.wrap(PrefaceFrame.PREFACE_BYTES), false);
return PrefaceFrame.PREFACE_BYTES.length;
}
}

View File

@ -35,18 +35,19 @@ public class PriorityGenerator extends FrameGenerator
}
@Override
public void generate(ByteBufferPool.Lease lease, Frame frame)
public long generate(ByteBufferPool.Lease lease, Frame frame)
{
PriorityFrame priorityFrame = (PriorityFrame)frame;
generatePriority(lease, priorityFrame.getStreamId(), priorityFrame.getParentStreamId(), priorityFrame.getWeight(), priorityFrame.isExclusive());
return generatePriority(lease, priorityFrame.getStreamId(), priorityFrame.getParentStreamId(), priorityFrame.getWeight(), priorityFrame.isExclusive());
}
public void generatePriority(ByteBufferPool.Lease lease, int streamId, int parentStreamId, int weight, boolean exclusive)
public long generatePriority(ByteBufferPool.Lease lease, int streamId, int parentStreamId, int weight, boolean exclusive)
{
ByteBuffer header = generateHeader(lease, FrameType.PRIORITY, PriorityFrame.PRIORITY_LENGTH, Flags.NONE, streamId);
generatePriorityBody(header, streamId, parentStreamId, weight, exclusive);
BufferUtil.flipToFlush(header, 0);
lease.append(header, true);
return Frame.HEADER_LENGTH + PriorityFrame.PRIORITY_LENGTH;
}
public void generatePriorityBody(ByteBuffer header, int streamId, int parentStreamId, int weight, boolean exclusive)

View File

@ -40,13 +40,13 @@ public class PushPromiseGenerator extends FrameGenerator
}
@Override
public void generate(ByteBufferPool.Lease lease, Frame frame)
public long generate(ByteBufferPool.Lease lease, Frame frame)
{
PushPromiseFrame pushPromiseFrame = (PushPromiseFrame)frame;
generatePushPromise(lease, pushPromiseFrame.getStreamId(), pushPromiseFrame.getPromisedStreamId(), pushPromiseFrame.getMetaData());
return generatePushPromise(lease, pushPromiseFrame.getStreamId(), pushPromiseFrame.getPromisedStreamId(), pushPromiseFrame.getMetaData());
}
public void generatePushPromise(ByteBufferPool.Lease lease, int streamId, int promisedStreamId, MetaData metaData)
public long generatePushPromise(ByteBufferPool.Lease lease, int streamId, int promisedStreamId, MetaData metaData)
{
if (streamId < 0)
throw new IllegalArgumentException("Invalid stream id: " + streamId);
@ -73,5 +73,7 @@ public class PushPromiseGenerator extends FrameGenerator
lease.append(header, true);
lease.append(hpacked, true);
return Frame.HEADER_LENGTH + length;
}
}

View File

@ -35,22 +35,22 @@ public class ResetGenerator extends FrameGenerator
}
@Override
public void generate(ByteBufferPool.Lease lease, Frame frame)
public long generate(ByteBufferPool.Lease lease, Frame frame)
{
ResetFrame resetFrame = (ResetFrame)frame;
generateReset(lease, resetFrame.getStreamId(), resetFrame.getError());
return generateReset(lease, resetFrame.getStreamId(), resetFrame.getError());
}
public void generateReset(ByteBufferPool.Lease lease, int streamId, int error)
public long generateReset(ByteBufferPool.Lease lease, int streamId, int error)
{
if (streamId < 0)
throw new IllegalArgumentException("Invalid stream id: " + streamId);
ByteBuffer header = generateHeader(lease, FrameType.RST_STREAM, 4, Flags.NONE, streamId);
ByteBuffer header = generateHeader(lease, FrameType.RST_STREAM, ResetFrame.RESET_LENGTH, Flags.NONE, streamId);
header.putInt(error);
BufferUtil.flipToFlush(header, 0);
lease.append(header, true);
return Frame.HEADER_LENGTH + ResetFrame.RESET_LENGTH;
}
}

View File

@ -36,13 +36,13 @@ public class SettingsGenerator extends FrameGenerator
}
@Override
public void generate(ByteBufferPool.Lease lease, Frame frame)
public long generate(ByteBufferPool.Lease lease, Frame frame)
{
SettingsFrame settingsFrame = (SettingsFrame)frame;
generateSettings(lease, settingsFrame.getSettings(), settingsFrame.isReply());
return generateSettings(lease, settingsFrame.getSettings(), settingsFrame.isReply());
}
public void generateSettings(ByteBufferPool.Lease lease, Map<Integer, Integer> settings, boolean reply)
public long generateSettings(ByteBufferPool.Lease lease, Map<Integer, Integer> settings, boolean reply)
{
// Two bytes for the identifier, four bytes for the value.
int entryLength = 2 + 4;
@ -60,5 +60,7 @@ public class SettingsGenerator extends FrameGenerator
BufferUtil.flipToFlush(header, 0);
lease.append(header, true);
return Frame.HEADER_LENGTH + length;
}
}

View File

@ -35,20 +35,22 @@ public class WindowUpdateGenerator extends FrameGenerator
}
@Override
public void generate(ByteBufferPool.Lease lease, Frame frame)
public long generate(ByteBufferPool.Lease lease, Frame frame)
{
WindowUpdateFrame windowUpdateFrame = (WindowUpdateFrame)frame;
generateWindowUpdate(lease, windowUpdateFrame.getStreamId(), windowUpdateFrame.getWindowDelta());
return generateWindowUpdate(lease, windowUpdateFrame.getStreamId(), windowUpdateFrame.getWindowDelta());
}
public void generateWindowUpdate(ByteBufferPool.Lease lease, int streamId, int windowUpdate)
public long generateWindowUpdate(ByteBufferPool.Lease lease, int streamId, int windowUpdate)
{
if (windowUpdate < 0)
throw new IllegalArgumentException("Invalid window update: " + windowUpdate);
ByteBuffer header = generateHeader(lease, FrameType.WINDOW_UPDATE, 4, Flags.NONE, streamId);
ByteBuffer header = generateHeader(lease, FrameType.WINDOW_UPDATE, WindowUpdateFrame.WINDOW_UPDATE_LENGTH, Flags.NONE, streamId);
header.putInt(windowUpdate);
BufferUtil.flipToFlush(header, 0);
lease.append(header, true);
return Frame.HEADER_LENGTH + WindowUpdateFrame.WINDOW_UPDATE_LENGTH;
}
}

View File

@ -23,6 +23,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicLong;
import org.eclipse.jetty.http.BadMessageException;
import org.eclipse.jetty.http.HttpField;
@ -59,9 +60,11 @@ import org.eclipse.jetty.util.thread.ExecutionStrategy;
public class HTTP2ServerConnection extends HTTP2Connection implements Connection.UpgradeTo
{
private final Queue<HttpChannelOverHTTP2> channels = new ConcurrentArrayQueue<>();
private final List<Frame> upgradeFrames = new ArrayList<>();
private final AtomicLong totalRequests = new AtomicLong();
private final AtomicLong totalResponses = new AtomicLong();
private final ServerSessionListener listener;
private final HttpConfiguration httpConfig;
private final List<Frame> upgradeFrames = new ArrayList<>();
public HTTP2ServerConnection(ByteBufferPool byteBufferPool, Executor executor, EndPoint endPoint, HttpConfiguration httpConfig, ServerParser parser, ISession session, int inputBufferSize, ServerSessionListener listener)
{
@ -75,6 +78,18 @@ public class HTTP2ServerConnection extends HTTP2Connection implements Connection
this.httpConfig = httpConfig;
}
@Override
public int getMessagesIn()
{
return totalRequests.intValue();
}
@Override
public int getMessagesOut()
{
return totalResponses.intValue();
}
@Override
protected ServerParser getParser()
{
@ -243,21 +258,29 @@ public class HTTP2ServerConnection extends HTTP2Connection implements Connection
}
@Override
public void recycle()
public Runnable onRequest(HeadersFrame frame)
{
getStream().removeAttribute(IStream.CHANNEL_ATTRIBUTE);
super.recycle();
channels.offer(this);
totalRequests.incrementAndGet();
return super.onRequest(frame);
}
@Override
public void onCompleted()
{
totalResponses.incrementAndGet();
super.onCompleted();
if (!getStream().isReset())
recycle();
}
@Override
public void recycle()
{
getStream().removeAttribute(IStream.CHANNEL_ATTRIBUTE);
super.recycle();
channels.offer(this);
}
@Override
public void reject()
{

View File

@ -89,6 +89,7 @@ public class HTTP2ServerSession extends HTTP2Session implements ServerParser.Lis
IStream stream = createRemoteStream(frame.getStreamId());
if (stream != null)
{
onStreamOpened(stream);
stream.process(frame, Callback.NOOP);
Stream.Listener listener = notifyNewStream(stream, frame);
stream.setListener(listener);

View File

@ -0,0 +1,94 @@
//
// ========================================================================
// Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.http.client;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.client.util.BytesContentProvider;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.io.ConnectionStatistics;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.util.IO;
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Test;
public class ConnectionStatisticsTest extends AbstractTest
{
public ConnectionStatisticsTest(Transport transport)
{
super(transport);
}
@Test
public void testConnectionStatistics() throws Exception
{
Assume.assumeThat(transport, Matchers.isOneOf(Transport.H2C, Transport.H2));
start(new AbstractHandler()
{
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
baseRequest.setHandled(true);
IO.copy(request.getInputStream(), response.getOutputStream());
}
});
ConnectionStatistics serverStats = new ConnectionStatistics();
connector.addBean(serverStats);
serverStats.start();
ConnectionStatistics clientStats = new ConnectionStatistics();
client.addBean(clientStats);
clientStats.start();
byte[] content = new byte[3072];
long contentLength = content.length;
ContentResponse response = client.newRequest(newURI())
.content(new BytesContentProvider(content))
.timeout(5, TimeUnit.SECONDS)
.send();
Assert.assertThat(response.getStatus(), Matchers.equalTo(HttpStatus.OK_200));
// Close all connections.
stop();
Assert.assertThat(serverStats.getConnectionsMax(), Matchers.greaterThan(0L));
Assert.assertThat(serverStats.getReceivedBytes(), Matchers.greaterThan(contentLength));
Assert.assertThat(serverStats.getSentBytes(), Matchers.greaterThan(contentLength));
Assert.assertThat(serverStats.getReceivedMessages(), Matchers.greaterThan(0L));
Assert.assertThat(serverStats.getSentMessages(), Matchers.greaterThan(0L));
Assert.assertThat(clientStats.getConnectionsMax(), Matchers.greaterThan(0L));
Assert.assertThat(clientStats.getReceivedBytes(), Matchers.greaterThan(contentLength));
Assert.assertThat(clientStats.getSentBytes(), Matchers.greaterThan(contentLength));
Assert.assertThat(clientStats.getReceivedMessages(), Matchers.greaterThan(0L));
Assert.assertThat(clientStats.getSentMessages(), Matchers.greaterThan(0L));
}
}