Use DirectBufferPool for NIO socket reads
This commit is contained in:
parent
aa80596b33
commit
bddfdba415
@ -313,22 +313,38 @@ class NioWorker implements Runnable {
|
|||||||
|
|
||||||
ChannelBuffer buffer =
|
ChannelBuffer buffer =
|
||||||
bufferFactory.getBuffer(predictor.nextReceiveBufferSize());
|
bufferFactory.getBuffer(predictor.nextReceiveBufferSize());
|
||||||
|
|
||||||
|
final ByteBuffer directBuffer;
|
||||||
|
final boolean fromPool;
|
||||||
|
if (buffer.isDirect()) {
|
||||||
|
directBuffer = buffer.toByteBuffer();
|
||||||
|
fromPool = false;
|
||||||
|
} else {
|
||||||
|
directBuffer = directBufferPool.acquire(buffer.writableBytes());
|
||||||
|
fromPool = true;
|
||||||
|
}
|
||||||
|
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
int readBytes = 0;
|
int readBytes = 0;
|
||||||
boolean failure = true;
|
boolean failure = true;
|
||||||
try {
|
try {
|
||||||
while ((ret = buffer.writeBytes(ch, buffer.writableBytes())) > 0) {
|
while ((ret = ch.read(directBuffer)) > 0) {
|
||||||
readBytes += ret;
|
readBytes += ret;
|
||||||
if (!buffer.writable()) {
|
if (!directBuffer.hasRemaining()) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
directBuffer.flip();
|
||||||
|
buffer.writeBytes(directBuffer);
|
||||||
failure = false;
|
failure = false;
|
||||||
} catch (AsynchronousCloseException e) {
|
} catch (AsynchronousCloseException e) {
|
||||||
// Can happen, and does not need a user attention.
|
// Can happen, and does not need a user attention.
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
fireExceptionCaught(channel, t);
|
fireExceptionCaught(channel, t);
|
||||||
|
} finally {
|
||||||
|
if (fromPool) {
|
||||||
|
directBufferPool.release(directBuffer);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (readBytes > 0) {
|
if (readBytes > 0) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user