* Un-deprecated NioSocketChannelConfig.receiveBufferSizePredictor to resolve the FIXME related with buffer creation

* Removed the usage of pre-allocated buffers (reverted back to old behavior)
This commit is contained in:
Trustin Lee 2009-01-15 01:33:46 +00:00
parent 80237f3074
commit 01bfefa6a4
3 changed files with 20 additions and 36 deletions

View File

@ -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() {

View File

@ -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);
/**

View File

@ -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) {