Use a percentual calculation to see if a new buffer should get allocated. See #480

This commit is contained in:
norman 2012-07-30 14:18:34 +02:00
parent d772e3cc74
commit f129ee1e28
2 changed files with 18 additions and 9 deletions

View File

@ -34,8 +34,7 @@ import org.jboss.netty.channel.ReceiveBufferSizePredictor;
public class NioWorker extends AbstractNioWorker { public class NioWorker extends AbstractNioWorker {
// TODO: Make this configurable ? private final SocketReceiveBufferAllocator recvBufferPool = new SocketReceiveBufferAllocator();
private final SocketReceiveBufferAllocator recvBufferPool = new SocketReceiveBufferAllocator(10);
public NioWorker(Executor executor) { public NioWorker(Executor executor) {
super(executor); super(executor);

View File

@ -24,11 +24,18 @@ final class SocketReceiveBufferAllocator {
private ByteBuffer buf; private ByteBuffer buf;
private int exceedCount; private int exceedCount;
private final int maxExceedCount; private final int maxExceedCount;
private final int percentual;
SocketReceiveBufferAllocator(int maxExceedCount) { SocketReceiveBufferAllocator() {
this.maxExceedCount = maxExceedCount; this(16, 80);
} }
SocketReceiveBufferAllocator(int maxExceedCount, int percentual) {
this.maxExceedCount = maxExceedCount;
this.percentual = percentual;
}
ByteBuffer get(int size) { ByteBuffer get(int size) {
if (buf == null) { if (buf == null) {
buf = ByteBuffer.allocateDirect(normalizeCapacity(size)); buf = ByteBuffer.allocateDirect(normalizeCapacity(size));
@ -37,14 +44,17 @@ final class SocketReceiveBufferAllocator {
ByteBufferUtil.destroy(buf); ByteBufferUtil.destroy(buf);
buf = ByteBuffer.allocateDirect(normalizeCapacity(size)); buf = ByteBuffer.allocateDirect(normalizeCapacity(size));
} else if (buf.capacity() > size) { } else if (buf.capacity() > size) {
if (((buf.capacity() / 100) * percentual) > size) {
if (++exceedCount == maxExceedCount) { if (++exceedCount == maxExceedCount) {
exceedCount = 0; exceedCount = 0;
ByteBufferUtil.destroy(buf); ByteBufferUtil.destroy(buf);
buf = ByteBuffer.allocateDirect(normalizeCapacity(size)); buf = ByteBuffer.allocateDirect(normalizeCapacity(size));
} }
} else { } else {
buf.position(0).limit(size); exceedCount = 0;
} }
}
buf.clear();
return buf; return buf;
} }