From 01bfefa6a4e0d33cbe0e7fdf9143f927ef59f5b7 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Thu, 15 Jan 2009 01:33:46 +0000 Subject: [PATCH] * Un-deprecated NioSocketChannelConfig.receiveBufferSizePredictor to resolve the FIXME related with buffer creation * Removed the usage of pre-allocated buffers (reverted back to old behavior) --- .../nio/DefaultNioSocketChannelConfig.java | 15 ++++---- .../socket/nio/NioSocketChannelConfig.java | 6 ---- .../netty/channel/socket/nio/NioWorker.java | 35 +++++++------------ 3 files changed, 20 insertions(+), 36 deletions(-) diff --git a/src/main/java/org/jboss/netty/channel/socket/nio/DefaultNioSocketChannelConfig.java b/src/main/java/org/jboss/netty/channel/socket/nio/DefaultNioSocketChannelConfig.java index 4904ee1f72..fba4fa15d5 100644 --- a/src/main/java/org/jboss/netty/channel/socket/nio/DefaultNioSocketChannelConfig.java +++ b/src/main/java/org/jboss/netty/channel/socket/nio/DefaultNioSocketChannelConfig.java @@ -44,11 +44,10 @@ class DefaultNioSocketChannelConfig extends DefaultSocketChannelConfig private static final InternalLogger logger = InternalLoggerFactory.getInstance(DefaultNioSocketChannelConfig.class); - private static final ReceiveBufferSizePredictor UNUSED_PREDICTOR = - new DefaultReceiveBufferSizePredictor(); - private volatile int writeBufferHighWaterMark = 64 * 1024; private volatile int writeBufferLowWaterMark = 32 * 1024; + private volatile ReceiveBufferSizePredictor predictor = + new DefaultReceiveBufferSizePredictor(); private volatile int writeSpinCount = 16; DefaultNioSocketChannelConfig(Socket socket) { @@ -116,15 +115,15 @@ class DefaultNioSocketChannelConfig extends DefaultSocketChannelConfig } public ReceiveBufferSizePredictor getReceiveBufferSizePredictor() { - logger.warn( - "Detected an access to a deprecated configuration parameter: " + - "receiveBufferSizePredictor"); - return UNUSED_PREDICTOR; + return predictor; } public void setReceiveBufferSizePredictor( ReceiveBufferSizePredictor predictor) { - getReceiveBufferSizePredictor(); + if (predictor == null) { + throw new NullPointerException("predictor"); + } + this.predictor = predictor; } public boolean isReadWriteFair() { diff --git a/src/main/java/org/jboss/netty/channel/socket/nio/NioSocketChannelConfig.java b/src/main/java/org/jboss/netty/channel/socket/nio/NioSocketChannelConfig.java index 77b08c0631..a015efd2dd 100644 --- a/src/main/java/org/jboss/netty/channel/socket/nio/NioSocketChannelConfig.java +++ b/src/main/java/org/jboss/netty/channel/socket/nio/NioSocketChannelConfig.java @@ -87,23 +87,17 @@ public interface NioSocketChannelConfig extends SocketChannelConfig { void setWriteSpinCount(int writeSpinCount); /** - * @deprecated Works fine without prediction since 3.1. - * * Returns the {@link ReceiveBufferSizePredictor} which predicts the * number of readable bytes in the socket receive buffer. The default * predictor is {@link DefaultReceiveBufferSizePredictor}. */ - @Deprecated ReceiveBufferSizePredictor getReceiveBufferSizePredictor(); /** - * @deprecated Works fine without prediction since 3.1. - * * Sets the {@link ReceiveBufferSizePredictor} which predicts the * number of readable bytes in the socket receive buffer. The default * predictor is {@link DefaultReceiveBufferSizePredictor}. */ - @Deprecated void setReceiveBufferSizePredictor(ReceiveBufferSizePredictor predictor); /** diff --git a/src/main/java/org/jboss/netty/channel/socket/nio/NioWorker.java b/src/main/java/org/jboss/netty/channel/socket/nio/NioWorker.java index 55739e1691..72edfd6b03 100644 --- a/src/main/java/org/jboss/netty/channel/socket/nio/NioWorker.java +++ b/src/main/java/org/jboss/netty/channel/socket/nio/NioWorker.java @@ -41,6 +41,7 @@ import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; import org.jboss.netty.buffer.ChannelBuffer; +import org.jboss.netty.buffer.ChannelBufferFactory; import org.jboss.netty.channel.Channel; import org.jboss.netty.channel.ChannelException; import org.jboss.netty.channel.ChannelFuture; @@ -255,29 +256,25 @@ class NioWorker implements Runnable { } } - private ChannelBuffer preallocatedBuffer; - private static void read(SelectionKey k) { ScatteringByteChannel ch = (ScatteringByteChannel) k.channel(); NioSocketChannel channel = (NioSocketChannel) k.attachment(); - ChannelBuffer preallocatedBuffer = channel.getWorker().preallocatedBuffer; - NioWorker worker = channel.getWorker(); - worker.preallocatedBuffer = null; + ReceiveBufferSizePredictor predictor = + channel.getConfig().getReceiveBufferSizePredictor(); + ChannelBufferFactory bufferFactory = + channel.getConfig().getBufferFactory(); - if (preallocatedBuffer == null) { - // TODO Magic number - // FIXME: OOPS - the new buffer should not be shared by more than one connection - preallocatedBuffer = channel.getConfig().getBufferFactory().getBuffer(1048576); - } + ChannelBuffer buffer = + bufferFactory.getBuffer(predictor.nextReceiveBufferSize()); int ret = 0; int readBytes = 0; boolean failure = true; try { - while ((ret = preallocatedBuffer.writeBytes(ch, preallocatedBuffer.writableBytes())) > 0) { + while ((ret = buffer.writeBytes(ch, buffer.writableBytes())) > 0) { readBytes += ret; - if (!preallocatedBuffer.writable()) { + if (!buffer.writable()) { break; } } @@ -289,17 +286,11 @@ class NioWorker implements Runnable { } if (readBytes > 0) { + // Update the predictor. + predictor.previousReceiveBufferSize(readBytes); + // Fire the event. - ChannelBuffer slice = preallocatedBuffer.slice( - preallocatedBuffer.readerIndex(), - preallocatedBuffer.readableBytes()); - preallocatedBuffer.readerIndex(preallocatedBuffer.writerIndex()); - if (preallocatedBuffer.writable()) { - worker.preallocatedBuffer = preallocatedBuffer; - } - fireMessageReceived(channel, slice); - } else if (readBytes == 0) { - worker.preallocatedBuffer = preallocatedBuffer; + fireMessageReceived(channel, buffer); } if (ret < 0 || failure) {