2012-06-04 22:31:44 +02:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2012-05-30 01:41:26 +02:00
|
|
|
package io.netty.bootstrap;
|
|
|
|
|
|
|
|
import io.netty.channel.Channel;
|
2013-05-08 06:57:52 +02:00
|
|
|
import io.netty.channel.ChannelConfig;
|
2012-05-30 01:41:26 +02:00
|
|
|
import io.netty.channel.ChannelHandler;
|
2012-06-07 07:52:33 +02:00
|
|
|
import io.netty.channel.ChannelHandlerContext;
|
Revamp the core API to reduce memory footprint and consumption
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.
2013-05-28 13:40:19 +02:00
|
|
|
import io.netty.channel.ChannelInboundHandlerAdapter;
|
2012-05-30 01:41:26 +02:00
|
|
|
import io.netty.channel.ChannelInitializer;
|
|
|
|
import io.netty.channel.ChannelOption;
|
2012-09-22 05:05:00 +02:00
|
|
|
import io.netty.channel.ChannelPipeline;
|
2012-08-10 13:17:18 +02:00
|
|
|
import io.netty.channel.EventLoopGroup;
|
Revamp the core API to reduce memory footprint and consumption
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.
2013-05-28 13:40:19 +02:00
|
|
|
import io.netty.channel.MessageList;
|
2012-05-30 01:41:26 +02:00
|
|
|
import io.netty.channel.ServerChannel;
|
2012-09-11 10:04:05 +02:00
|
|
|
import io.netty.channel.socket.SocketChannel;
|
2012-10-26 23:52:50 +02:00
|
|
|
import io.netty.util.AttributeKey;
|
2013-02-26 23:54:25 +01:00
|
|
|
import io.netty.util.internal.logging.InternalLogger;
|
|
|
|
import io.netty.util.internal.logging.InternalLoggerFactory;
|
2012-05-14 16:57:23 +02:00
|
|
|
|
|
|
|
import java.util.LinkedHashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map.Entry;
|
2013-05-08 06:57:52 +02:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2012-05-14 16:57:23 +02:00
|
|
|
|
2012-09-11 10:04:05 +02:00
|
|
|
/**
|
|
|
|
* {@link Bootstrap} sub-class which allows easy bootstrap of {@link ServerChannel}
|
|
|
|
*
|
|
|
|
*/
|
2013-01-30 21:38:15 +01:00
|
|
|
public final class ServerBootstrap extends AbstractBootstrap<ServerBootstrap, ServerChannel> {
|
2012-05-14 16:57:23 +02:00
|
|
|
|
2012-05-30 01:41:26 +02:00
|
|
|
private static final InternalLogger logger = InternalLoggerFactory.getInstance(ServerBootstrap.class);
|
2012-05-14 16:57:23 +02:00
|
|
|
|
|
|
|
private final Map<ChannelOption<?>, Object> childOptions = new LinkedHashMap<ChannelOption<?>, Object>();
|
2012-10-26 23:52:50 +02:00
|
|
|
private final Map<AttributeKey<?>, Object> childAttrs = new LinkedHashMap<AttributeKey<?>, Object>();
|
2013-01-31 03:34:28 +01:00
|
|
|
private volatile EventLoopGroup childGroup;
|
|
|
|
private volatile ChannelHandler childHandler;
|
2012-05-14 16:57:23 +02:00
|
|
|
|
2013-01-30 13:12:02 +01:00
|
|
|
public ServerBootstrap() { }
|
|
|
|
|
2013-01-30 13:55:10 +01:00
|
|
|
private ServerBootstrap(ServerBootstrap bootstrap) {
|
2013-01-30 13:12:02 +01:00
|
|
|
super(bootstrap);
|
|
|
|
childGroup = bootstrap.childGroup;
|
|
|
|
childHandler = bootstrap.childHandler;
|
2013-01-31 03:34:28 +01:00
|
|
|
synchronized (bootstrap.childOptions) {
|
|
|
|
childOptions.putAll(bootstrap.childOptions);
|
|
|
|
}
|
|
|
|
synchronized (bootstrap.childAttrs) {
|
|
|
|
childAttrs.putAll(bootstrap.childAttrs);
|
|
|
|
}
|
2013-01-30 13:12:02 +01:00
|
|
|
}
|
|
|
|
|
2012-09-11 10:04:05 +02:00
|
|
|
/**
|
|
|
|
* Specify the {@link EventLoopGroup} which is used for the parent (acceptor) and the child (client).
|
|
|
|
*/
|
2012-09-11 08:31:20 +02:00
|
|
|
@Override
|
2012-08-10 13:26:04 +02:00
|
|
|
public ServerBootstrap group(EventLoopGroup group) {
|
2012-09-11 08:31:20 +02:00
|
|
|
return group(group, group);
|
2012-08-10 13:26:04 +02:00
|
|
|
}
|
|
|
|
|
2012-09-11 10:04:05 +02:00
|
|
|
/**
|
|
|
|
* Set the {@link EventLoopGroup} for the parent (acceptor) and the child (client). These
|
|
|
|
* {@link EventLoopGroup}'s are used to handle all the events and IO for {@link SocketChannel} and
|
|
|
|
* {@link Channel}'s.
|
|
|
|
*/
|
2012-08-10 13:17:18 +02:00
|
|
|
public ServerBootstrap group(EventLoopGroup parentGroup, EventLoopGroup childGroup) {
|
2012-09-11 08:31:20 +02:00
|
|
|
super.group(parentGroup);
|
|
|
|
if (childGroup == null) {
|
|
|
|
throw new NullPointerException("childGroup");
|
2012-05-14 16:57:23 +02:00
|
|
|
}
|
2012-09-11 08:31:20 +02:00
|
|
|
if (this.childGroup != null) {
|
|
|
|
throw new IllegalStateException("childGroup set already");
|
2012-05-30 01:41:26 +02:00
|
|
|
}
|
2012-08-10 13:17:18 +02:00
|
|
|
this.childGroup = childGroup;
|
2012-05-14 16:57:23 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2012-09-11 10:04:05 +02:00
|
|
|
/**
|
|
|
|
* Allow to specify a {@link ChannelOption} which is used for the {@link Channel} instances once they get created
|
2012-11-12 03:51:23 +01:00
|
|
|
* (after the acceptor accepted the {@link Channel}). Use a value of {@code null} to remove a previous set
|
2012-09-11 10:04:05 +02:00
|
|
|
* {@link ChannelOption}.
|
|
|
|
*/
|
2012-05-30 01:41:26 +02:00
|
|
|
public <T> ServerBootstrap childOption(ChannelOption<T> childOption, T value) {
|
2012-05-14 16:57:23 +02:00
|
|
|
if (childOption == null) {
|
|
|
|
throw new NullPointerException("childOption");
|
|
|
|
}
|
|
|
|
if (value == null) {
|
2013-01-31 03:34:28 +01:00
|
|
|
synchronized (childOptions) {
|
|
|
|
childOptions.remove(childOption);
|
|
|
|
}
|
2012-05-14 16:57:23 +02:00
|
|
|
} else {
|
2013-01-31 03:34:28 +01:00
|
|
|
synchronized (childOptions) {
|
|
|
|
childOptions.put(childOption, value);
|
|
|
|
}
|
2012-05-14 16:57:23 +02:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2012-12-21 11:03:35 +01:00
|
|
|
/**
|
|
|
|
* Set the specific {@link AttributeKey} with the given value on every child {@link Channel}. If the value is
|
|
|
|
* {@code null} the {@link AttributeKey} is removed
|
|
|
|
*/
|
2012-10-26 23:52:50 +02:00
|
|
|
public <T> ServerBootstrap childAttr(AttributeKey<T> childKey, T value) {
|
|
|
|
if (childKey == null) {
|
|
|
|
throw new NullPointerException("childKey");
|
|
|
|
}
|
|
|
|
if (value == null) {
|
|
|
|
childAttrs.remove(childKey);
|
|
|
|
} else {
|
|
|
|
childAttrs.put(childKey, value);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2012-09-11 10:04:05 +02:00
|
|
|
/**
|
2013-01-28 11:26:53 +01:00
|
|
|
* Set the {@link ChannelHandler} which is used to serve the request for the {@link Channel}'s.
|
2012-09-11 10:04:05 +02:00
|
|
|
*/
|
2012-06-03 10:00:16 +02:00
|
|
|
public ServerBootstrap childHandler(ChannelHandler childHandler) {
|
|
|
|
if (childHandler == null) {
|
|
|
|
throw new NullPointerException("childHandler");
|
2012-05-14 16:57:23 +02:00
|
|
|
}
|
2012-06-03 10:00:16 +02:00
|
|
|
this.childHandler = childHandler;
|
2012-05-14 16:57:23 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2012-09-11 08:31:20 +02:00
|
|
|
@Override
|
2013-03-21 13:34:13 +01:00
|
|
|
void init(Channel channel) throws Exception {
|
|
|
|
final Map<ChannelOption<?>, Object> options = options();
|
|
|
|
synchronized (options) {
|
|
|
|
channel.config().setOptions(options);
|
2012-07-07 07:37:44 +02:00
|
|
|
}
|
|
|
|
|
2013-01-31 03:34:28 +01:00
|
|
|
final Map<AttributeKey<?>, Object> attrs = attrs();
|
|
|
|
synchronized (attrs) {
|
|
|
|
for (Entry<AttributeKey<?>, Object> e: attrs.entrySet()) {
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
AttributeKey<Object> key = (AttributeKey<Object>) e.getKey();
|
|
|
|
channel.attr(key).set(e.getValue());
|
|
|
|
}
|
2012-10-26 23:52:50 +02:00
|
|
|
}
|
|
|
|
|
2013-01-30 12:06:40 +01:00
|
|
|
ChannelPipeline p = channel.pipeline();
|
2012-10-09 20:55:24 +02:00
|
|
|
if (handler() != null) {
|
|
|
|
p.addLast(handler());
|
2012-05-14 16:57:23 +02:00
|
|
|
}
|
2013-01-31 03:34:28 +01:00
|
|
|
|
|
|
|
final EventLoopGroup currentChildGroup = childGroup;
|
|
|
|
final ChannelHandler currentChildHandler = childHandler;
|
|
|
|
final Entry<ChannelOption<?>, Object>[] currentChildOptions;
|
|
|
|
final Entry<AttributeKey<?>, Object>[] currentChildAttrs;
|
|
|
|
synchronized (childOptions) {
|
|
|
|
currentChildOptions = childOptions.entrySet().toArray(newOptionArray(childOptions.size()));
|
|
|
|
}
|
|
|
|
synchronized (childAttrs) {
|
|
|
|
currentChildAttrs = childAttrs.entrySet().toArray(newAttrArray(childAttrs.size()));
|
|
|
|
}
|
|
|
|
|
|
|
|
p.addLast(new ChannelInitializer<Channel>() {
|
|
|
|
@Override
|
|
|
|
public void initChannel(Channel ch) throws Exception {
|
|
|
|
ch.pipeline().addLast(new ServerBootstrapAcceptor(
|
|
|
|
currentChildGroup, currentChildHandler, currentChildOptions, currentChildAttrs));
|
|
|
|
}
|
|
|
|
});
|
2012-05-14 16:57:23 +02:00
|
|
|
}
|
|
|
|
|
2012-09-11 08:31:20 +02:00
|
|
|
@Override
|
2013-04-03 09:15:33 +02:00
|
|
|
@Deprecated
|
|
|
|
@SuppressWarnings("deprecation")
|
2012-05-15 06:45:25 +02:00
|
|
|
public void shutdown() {
|
2012-09-11 08:31:20 +02:00
|
|
|
super.shutdown();
|
2012-08-10 13:17:18 +02:00
|
|
|
if (childGroup != null) {
|
|
|
|
childGroup.shutdown();
|
2012-05-15 06:45:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-11 08:31:20 +02:00
|
|
|
@Override
|
2013-02-08 07:48:47 +01:00
|
|
|
public ServerBootstrap validate() {
|
2012-09-11 08:31:20 +02:00
|
|
|
super.validate();
|
2012-06-03 10:00:16 +02:00
|
|
|
if (childHandler == null) {
|
|
|
|
throw new IllegalStateException("childHandler not set");
|
2012-05-14 16:57:23 +02:00
|
|
|
}
|
2012-08-10 13:17:18 +02:00
|
|
|
if (childGroup == null) {
|
|
|
|
logger.warn("childGroup is not set. Using parentGroup instead.");
|
2012-09-11 08:31:20 +02:00
|
|
|
childGroup = group();
|
2012-05-14 16:57:23 +02:00
|
|
|
}
|
2013-02-08 07:48:47 +01:00
|
|
|
return this;
|
2012-05-14 16:57:23 +02:00
|
|
|
}
|
|
|
|
|
2013-01-31 03:34:28 +01:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
private static Entry<ChannelOption<?>, Object>[] newOptionArray(int size) {
|
|
|
|
return new Entry[size];
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
private static Entry<AttributeKey<?>, Object>[] newAttrArray(int size) {
|
|
|
|
return new Entry[size];
|
|
|
|
}
|
|
|
|
|
Revamp the core API to reduce memory footprint and consumption
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.
2013-05-28 13:40:19 +02:00
|
|
|
private static class ServerBootstrapAcceptor extends ChannelInboundHandlerAdapter {
|
2012-06-10 05:22:32 +02:00
|
|
|
|
2013-01-31 03:34:28 +01:00
|
|
|
private final EventLoopGroup childGroup;
|
|
|
|
private final ChannelHandler childHandler;
|
|
|
|
private final Entry<ChannelOption<?>, Object>[] childOptions;
|
|
|
|
private final Entry<AttributeKey<?>, Object>[] childAttrs;
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
ServerBootstrapAcceptor(
|
|
|
|
EventLoopGroup childGroup, ChannelHandler childHandler,
|
|
|
|
Entry<ChannelOption<?>, Object>[] childOptions, Entry<AttributeKey<?>, Object>[] childAttrs) {
|
|
|
|
this.childGroup = childGroup;
|
|
|
|
this.childHandler = childHandler;
|
|
|
|
this.childOptions = childOptions;
|
|
|
|
this.childAttrs = childAttrs;
|
|
|
|
}
|
|
|
|
|
2012-05-14 16:57:23 +02:00
|
|
|
@Override
|
Read only when requested (read-on-demand)
This pull request introduces a new operation called read() that replaces the existing inbound traffic control method. EventLoop now performs socket reads only when the read() operation has been issued. Once the requested read() operation is actually performed, EventLoop triggers an inboundBufferSuspended event that tells the handlers that the requested read() operation has been performed and the inbound traffic has been suspended again. A handler can decide to continue reading or not.
Unlike other outbound operations, read() does not use ChannelFuture at all to avoid GC cost. If there's a good reason to create a new future per read at the GC cost, I'll change this.
This pull request consequently removes the readable property in ChannelHandlerContext, which means how the traffic control works changed significantly.
This pull request also adds a new configuration property ChannelOption.AUTO_READ whose default value is true. If true, Netty will call ctx.read() for you. If you need a close control over when read() is called, you can set it to false.
Another interesting fact is that non-terminal handlers do not really need to call read() at all. Only the last inbound handler will have to call it, and that's just enough. Actually, you don't even need to call it at the last handler in most cases because of the ChannelOption.AUTO_READ mentioned above.
There's no serious backward compatibility issue. If the compiler complains your handler does not implement the read() method, add the following:
public void read(ChannelHandlerContext ctx) throws Exception {
ctx.read();
}
Note that this pull request certainly makes bounded inbound buffer support very easy, but itself does not add the bounded inbound buffer support.
2012-12-30 13:53:59 +01:00
|
|
|
@SuppressWarnings("unchecked")
|
Revamp the core API to reduce memory footprint and consumption
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.
2013-05-28 13:40:19 +02:00
|
|
|
public void messageReceived(ChannelHandlerContext ctx, MessageList<Object> msgs) {
|
|
|
|
int size = msgs.size();
|
|
|
|
for (int i = 0; i < size; i ++) {
|
|
|
|
Channel child = (Channel) msgs.get(i);
|
2012-06-03 10:00:16 +02:00
|
|
|
child.pipeline().addLast(childHandler);
|
2012-05-14 16:57:23 +02:00
|
|
|
|
2013-01-31 03:34:28 +01:00
|
|
|
for (Entry<ChannelOption<?>, Object> e: childOptions) {
|
2012-05-14 16:57:23 +02:00
|
|
|
try {
|
|
|
|
if (!child.config().setOption((ChannelOption<Object>) e.getKey(), e.getValue())) {
|
|
|
|
logger.warn("Unknown channel option: " + e);
|
|
|
|
}
|
|
|
|
} catch (Throwable t) {
|
|
|
|
logger.warn("Failed to set a channel option: " + child, t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-31 03:34:28 +01:00
|
|
|
for (Entry<AttributeKey<?>, Object> e: childAttrs) {
|
2012-10-26 23:52:50 +02:00
|
|
|
child.attr((AttributeKey<Object>) e.getKey()).set(e.getValue());
|
|
|
|
}
|
|
|
|
|
2012-05-14 16:57:23 +02:00
|
|
|
try {
|
2012-08-10 13:17:18 +02:00
|
|
|
childGroup.register(child);
|
2012-05-14 16:57:23 +02:00
|
|
|
} catch (Throwable t) {
|
2012-09-22 05:05:00 +02:00
|
|
|
child.unsafe().closeForcibly();
|
2012-05-14 16:57:23 +02:00
|
|
|
logger.warn("Failed to register an accepted channel: " + child, t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-05-08 06:57:52 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
|
|
|
final ChannelConfig config = ctx.channel().config();
|
|
|
|
if (config.isAutoRead()) {
|
|
|
|
// stop accept new connections for 1 second to allow the channel to recover
|
|
|
|
// See https://github.com/netty/netty/issues/1328
|
|
|
|
config.setAutoRead(false);
|
|
|
|
ctx.channel().eventLoop().schedule(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
config.setAutoRead(true);
|
|
|
|
}
|
|
|
|
}, 1, TimeUnit.SECONDS);
|
|
|
|
}
|
|
|
|
// still let the exceptionCaught event flow through the pipeline to give the user
|
|
|
|
// a chance to do something with it
|
|
|
|
ctx.fireExceptionCaught(cause);
|
|
|
|
}
|
2012-05-14 16:57:23 +02:00
|
|
|
}
|
2012-11-10 20:43:53 +01:00
|
|
|
|
2013-01-30 13:12:02 +01:00
|
|
|
@Override
|
|
|
|
@SuppressWarnings("CloneDoesntCallSuperClone")
|
|
|
|
public ServerBootstrap clone() {
|
|
|
|
return new ServerBootstrap(this);
|
|
|
|
}
|
|
|
|
|
2012-11-26 08:14:24 +01:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
StringBuilder buf = new StringBuilder(super.toString());
|
|
|
|
buf.setLength(buf.length() - 1);
|
|
|
|
buf.append(", ");
|
|
|
|
if (childGroup != null) {
|
|
|
|
buf.append("childGroup: ");
|
|
|
|
buf.append(childGroup.getClass().getSimpleName());
|
|
|
|
buf.append(", ");
|
|
|
|
}
|
2013-01-31 03:34:28 +01:00
|
|
|
synchronized (childOptions) {
|
|
|
|
if (!childOptions.isEmpty()) {
|
|
|
|
buf.append("childOptions: ");
|
|
|
|
buf.append(childOptions);
|
|
|
|
buf.append(", ");
|
|
|
|
}
|
2012-11-26 08:14:24 +01:00
|
|
|
}
|
2013-01-31 03:34:28 +01:00
|
|
|
synchronized (childAttrs) {
|
|
|
|
if (!childAttrs.isEmpty()) {
|
|
|
|
buf.append("childAttrs: ");
|
|
|
|
buf.append(childAttrs);
|
|
|
|
buf.append(", ");
|
|
|
|
}
|
2012-11-26 08:14:24 +01:00
|
|
|
}
|
|
|
|
if (childHandler != null) {
|
|
|
|
buf.append("childHandler: ");
|
|
|
|
buf.append(childHandler);
|
|
|
|
buf.append(", ");
|
|
|
|
}
|
|
|
|
if (buf.charAt(buf.length() - 1) == '(') {
|
|
|
|
buf.append(')');
|
|
|
|
} else {
|
|
|
|
buf.setCharAt(buf.length() - 2, ')');
|
|
|
|
buf.setLength(buf.length() - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.toString();
|
|
|
|
}
|
2012-05-14 16:57:23 +02:00
|
|
|
}
|