The API changes made so far turned out to increase the memory footprint and consumption while our intention was actually decreasing them. Memory consumption issue: When there are many connections which does not exchange data frequently, the old Netty 4 API spent a lot more memory than 3 because it always allocates per-handler buffer for each connection unless otherwise explicitly stated by a user. In a usual real world load, a client doesn't always send requests without pausing, so the idea of having a buffer whose life cycle if bound to the life cycle of a connection didn't work as expected. Memory footprint issue: The old Netty 4 API decreased overall memory footprint by a great deal in many cases. It was mainly because the old Netty 4 API did not allocate a new buffer and event object for each read. Instead, it created a new buffer for each handler in a pipeline. This works pretty well as long as the number of handlers in a pipeline is only a few. However, for a highly modular application with many handlers which handles connections which lasts for relatively short period, it actually makes the memory footprint issue much worse. Changes: All in all, this is about retaining all the good changes we made in 4 so far such as better thread model and going back to the way how we dealt with message events in 3. To fix the memory consumption/footprint issue mentioned above, we made a hard decision to break the backward compatibility again with the following changes: - Remove MessageBuf - Merge Buf into ByteBuf - Merge ChannelInboundByte/MessageHandler and ChannelStateHandler into ChannelInboundHandler - Similar changes were made to the adapter classes - Merge ChannelOutboundByte/MessageHandler and ChannelOperationHandler into ChannelOutboundHandler - Similar changes were made to the adapter classes - Introduce MessageList which is similar to `MessageEvent` in Netty 3 - Replace inboundBufferUpdated(ctx) with messageReceived(ctx, MessageList) - Replace flush(ctx, promise) with write(ctx, MessageList, promise) - Remove ByteToByteEncoder/Decoder/Codec - Replaced by MessageToByteEncoder<ByteBuf>, ByteToMessageDecoder<ByteBuf>, and ByteMessageCodec<ByteBuf> - Merge EmbeddedByteChannel and EmbeddedMessageChannel into EmbeddedChannel - Add SimpleChannelInboundHandler which is sometimes more useful than ChannelInboundHandlerAdapter - Bring back Channel.isWritable() from Netty 3 - Add ChannelInboundHandler.channelWritabilityChanges() event - Add RecvByteBufAllocator configuration property - Similar to ReceiveBufferSizePredictor in Netty 3 - Some existing configuration properties such as DatagramChannelConfig.receivePacketSize is gone now. - Remove suspend/resumeIntermediaryDeallocation() in ByteBuf This change would have been impossible without @normanmaurer's help. He fixed, ported, and improved many parts of the changes.
68 lines
2.5 KiB
Java
68 lines
2.5 KiB
Java
/*
|
|
* Copyright 2012 The Netty Project
|
|
*
|
|
* The Netty Project licenses this file to you under the Apache License,
|
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
|
* with the License. You may obtain a copy of the License at:
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
* License for the specific language governing permissions and limitations
|
|
* under the License.
|
|
*/
|
|
package io.netty.handler.codec.spdy;
|
|
|
|
import io.netty.buffer.ByteBufUtil;
|
|
import io.netty.channel.ChannelHandlerContext;
|
|
import io.netty.channel.MessageList;
|
|
import io.netty.handler.codec.MessageToMessageCodec;
|
|
import io.netty.handler.codec.http.HttpMessage;
|
|
|
|
import java.util.LinkedList;
|
|
import java.util.Queue;
|
|
|
|
/**
|
|
* {@link MessageToMessageCodec} that takes care of adding the right {@link SpdyHttpHeaders.Names#STREAM_ID} to the
|
|
* {@link HttpMessage} if one is not present. This makes it possible to just re-use plan handlers current used
|
|
* for HTTP.
|
|
*/
|
|
public class SpdyHttpResponseStreamIdHandler extends
|
|
MessageToMessageCodec<Object, HttpMessage> {
|
|
private static final Integer NO_ID = -1;
|
|
private final Queue<Integer> ids = new LinkedList<Integer>();
|
|
|
|
@Override
|
|
public boolean acceptInboundMessage(Object msg) throws Exception {
|
|
return msg instanceof HttpMessage || msg instanceof SpdyRstStreamFrame;
|
|
}
|
|
|
|
@Override
|
|
protected void encode(ChannelHandlerContext ctx, HttpMessage msg, MessageList<Object> out) throws Exception {
|
|
Integer id = ids.poll();
|
|
if (id != null && id.intValue() != NO_ID && !msg.headers().contains(SpdyHttpHeaders.Names.STREAM_ID)) {
|
|
SpdyHttpHeaders.setStreamId(msg, id);
|
|
}
|
|
|
|
out.add(ByteBufUtil.retain(msg));
|
|
}
|
|
|
|
@Override
|
|
protected void decode(ChannelHandlerContext ctx, Object msg, MessageList<Object> out) throws Exception {
|
|
if (msg instanceof HttpMessage) {
|
|
boolean contains = ((HttpMessage) msg).headers().contains(SpdyHttpHeaders.Names.STREAM_ID);
|
|
if (!contains) {
|
|
ids.add(NO_ID);
|
|
} else {
|
|
ids.add(SpdyHttpHeaders.getStreamId((HttpMessage) msg));
|
|
}
|
|
} else if (msg instanceof SpdyRstStreamFrame) {
|
|
ids.remove(((SpdyRstStreamFrame) msg).getStreamId());
|
|
}
|
|
|
|
out.add(ByteBufUtil.retain(msg));
|
|
}
|
|
}
|