-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Description
Feature Request
Is your feature request related to a problem? Please describe
Some business scenarios, we will query large key (approximately 10M) once daily at midnight, while most other times only query small key (less than 1KB). However, once the readBuffer in the CommandHandler is expanded, it cannot be dynamically shrunk, resulting in wasted off-heap memory.
Describe the solution you'd like
When the readBuffer is insufficient, a new larger Buffer object is allocated. At the same time, the defaultTmpBuffer variable is used to point to the readBuffer object to save it. After the current request is completed, the larger Buffer object is released, and the defaultTmpBuffer is assigned back to the readBuffer. This approach can help save resources and reduce costs in scenarios with occasional large keys.
private static final int DEFAULT_READER_BUFFER_CAPACITY = 8192 * 8;
private ByteBuf defaultTmpBuffer;
private ByteBufAllocator alloc;
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
.......
if (traceEnabled) {
logger.trace("{} Buffer: {}", logPrefix(), input.toString(Charset.defaultCharset()).trim());
}
// if buffer capacity larger than default capacity, then create a new buffer with double capacity
if (readBuffer.capacity() == DEFAULT_READER_BUFFER_CAPACITY && readBuffer.writableBytes() < input.readableBytes()
&& alloc != null) {
ByteBuf byteBuf = alloc.directBuffer(readBuffer.capacity() << 1);
byteBuf.writeBytes(readBuffer);
defaultTmpBuffer = readBuffer;
readBuffer = byteBuf;
}
readBuffer.touch("CommandHandler.read(…)");
readBuffer.writeBytes(input);
decode(ctx, readBuffer);
} finally {
input.release();
}
}When Decode Completed ,Then Release The Large Buffer
private void releaseLargeBufferIfNecessary(ByteBuf buffer) {
if (this.defaultTmpBuffer != null) {
buffer.release();
this.readBuffer = defaultTmpBuffer;
this.readBuffer.clear();
this.defaultTmpBuffer = null;
}
}Describe alternatives you've considered
NA
Teachability, Documentation, Adoption, Migration Strategy
NA