[#1812] Allow for inline for most common cases when use NioByteUnsafe.read()
This commit is contained in:
parent
9e882c793b
commit
db765e5dd4
@ -56,12 +56,8 @@ public abstract class AbstractNioByteChannel extends AbstractNioChannel {
|
|||||||
private final class NioByteUnsafe extends AbstractNioUnsafe {
|
private final class NioByteUnsafe extends AbstractNioUnsafe {
|
||||||
private RecvByteBufAllocator.Handle allocHandle;
|
private RecvByteBufAllocator.Handle allocHandle;
|
||||||
|
|
||||||
@Override
|
private void removeReadOp() {
|
||||||
public void read() {
|
SelectionKey key = selectionKey();
|
||||||
assert eventLoop().inEventLoop();
|
|
||||||
final SelectionKey key = selectionKey();
|
|
||||||
final ChannelConfig config = config();
|
|
||||||
if (!config.isAutoRead()) {
|
|
||||||
int interestOps = key.interestOps();
|
int interestOps = key.interestOps();
|
||||||
if ((interestOps & readInterestOp) != 0) {
|
if ((interestOps & readInterestOp) != 0) {
|
||||||
// only remove readInterestOp if needed
|
// only remove readInterestOp if needed
|
||||||
@ -69,65 +65,8 @@ public abstract class AbstractNioByteChannel extends AbstractNioChannel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
final ChannelPipeline pipeline = pipeline();
|
private void closeOnRead(ChannelPipeline pipeline) {
|
||||||
|
SelectionKey key = selectionKey();
|
||||||
RecvByteBufAllocator.Handle allocHandle = this.allocHandle;
|
|
||||||
if (allocHandle == null) {
|
|
||||||
this.allocHandle = allocHandle = config.getRecvByteBufAllocator().newHandle();
|
|
||||||
}
|
|
||||||
|
|
||||||
final ByteBufAllocator allocator = config.getAllocator();
|
|
||||||
final int maxMessagesPerRead = config.getMaxMessagesPerRead();
|
|
||||||
|
|
||||||
boolean closed = false;
|
|
||||||
Throwable exception = null;
|
|
||||||
ByteBuf byteBuf = null;
|
|
||||||
int messages = 0;
|
|
||||||
try {
|
|
||||||
for (;;) {
|
|
||||||
byteBuf = allocHandle.allocate(allocator);
|
|
||||||
int localReadAmount = doReadBytes(byteBuf);
|
|
||||||
if (localReadAmount == 0) {
|
|
||||||
byteBuf.release();
|
|
||||||
byteBuf = null;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (localReadAmount < 0) {
|
|
||||||
closed = true;
|
|
||||||
byteBuf.release();
|
|
||||||
byteBuf = null;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
pipeline.fireChannelRead(byteBuf);
|
|
||||||
allocHandle.record(localReadAmount);
|
|
||||||
byteBuf = null;
|
|
||||||
if (++ messages == maxMessagesPerRead) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (Throwable t) {
|
|
||||||
exception = t;
|
|
||||||
} finally {
|
|
||||||
if (byteBuf != null) {
|
|
||||||
if (byteBuf.isReadable()) {
|
|
||||||
pipeline.fireChannelRead(byteBuf);
|
|
||||||
} else {
|
|
||||||
byteBuf.release();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pipeline.fireChannelReadComplete();
|
|
||||||
|
|
||||||
if (exception != null) {
|
|
||||||
if (exception instanceof IOException) {
|
|
||||||
closed = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
pipeline().fireExceptionCaught(exception);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (closed) {
|
|
||||||
setInputShutdown();
|
setInputShutdown();
|
||||||
if (isOpen()) {
|
if (isOpen()) {
|
||||||
if (Boolean.TRUE.equals(config().getOption(ChannelOption.ALLOW_HALF_CLOSURE))) {
|
if (Boolean.TRUE.equals(config().getOption(ChannelOption.ALLOW_HALF_CLOSURE))) {
|
||||||
@ -138,6 +77,59 @@ public abstract class AbstractNioByteChannel extends AbstractNioChannel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void handleReadException(ChannelPipeline pipeline, ByteBuf byteBuf, Throwable cause, boolean close) {
|
||||||
|
if (byteBuf != null) {
|
||||||
|
if (byteBuf.isReadable()) {
|
||||||
|
pipeline.fireChannelRead(byteBuf);
|
||||||
|
} else {
|
||||||
|
byteBuf.release();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pipeline.fireChannelReadComplete();
|
||||||
|
if (close || cause instanceof IOException) {
|
||||||
|
closeOnRead(pipeline);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void read() {
|
||||||
|
final ChannelConfig config = config();
|
||||||
|
final ChannelPipeline pipeline = pipeline();
|
||||||
|
final ByteBufAllocator allocator = config.getAllocator();
|
||||||
|
final int maxMessagesPerRead = config.getMaxMessagesPerRead();
|
||||||
|
RecvByteBufAllocator.Handle allocHandle = this.allocHandle;
|
||||||
|
if (allocHandle == null) {
|
||||||
|
this.allocHandle = allocHandle = config.getRecvByteBufAllocator().newHandle();
|
||||||
|
}
|
||||||
|
if (!config.isAutoRead()) {
|
||||||
|
removeReadOp();
|
||||||
|
}
|
||||||
|
|
||||||
|
ByteBuf byteBuf = null;
|
||||||
|
int messages = 0;
|
||||||
|
boolean close = false;
|
||||||
|
try {
|
||||||
|
do {
|
||||||
|
byteBuf = allocHandle.allocate(allocator);
|
||||||
|
int localReadAmount = doReadBytes(byteBuf);
|
||||||
|
if (localReadAmount <= 0) {
|
||||||
|
close = localReadAmount < 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
pipeline.fireChannelRead(byteBuf);
|
||||||
|
byteBuf = null;
|
||||||
|
allocHandle.record(localReadAmount);
|
||||||
|
} while (++ messages < maxMessagesPerRead);
|
||||||
|
|
||||||
|
pipeline.fireChannelReadComplete();
|
||||||
|
|
||||||
|
if (close) {
|
||||||
|
closeOnRead(pipeline);
|
||||||
|
close = false;
|
||||||
|
}
|
||||||
|
} catch (Throwable t) {
|
||||||
|
handleReadException(pipeline, byteBuf, t, close);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user