Skip to content

Commit

Permalink
HttpObjectDecoder configurable initial buffer size
Browse files Browse the repository at this point in the history
Motivation:
The initial buffer size used to decode HTTP objects is currently fixed at 128. This may be too small for some use cases and create a high amount of overhead associated with resizing/copying. The user should be able to configure the initial size as they please.

Modifications:
- Make HttpObjectDecoder's AppendableCharSequence initial size configurable

Result:
Users can more finely tune initial buffer size for increased performance or to save memory.
Fixes #4807
  • Loading branch information
Scottmitch committed Feb 8, 2016
1 parent f59392d commit a15ff32
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,17 @@ public HttpClientCodec(
this.failOnMissingResponse = failOnMissingResponse;
}

/**
* Creates a new instance with the specified decoder options.
*/
public HttpClientCodec(
int maxInitialLineLength, int maxHeaderSize, int maxChunkSize, boolean failOnMissingResponse,
boolean validateHeaders, int initialBufferSize) {
init(new Decoder(maxInitialLineLength, maxHeaderSize, maxChunkSize, validateHeaders, initialBufferSize),
new Encoder());
this.failOnMissingResponse = failOnMissingResponse;
}

/**
* Prepares to upgrade to another protocol from HTTP. Disables the {@link Encoder}.
*/
Expand Down Expand Up @@ -148,6 +159,11 @@ private final class Decoder extends HttpResponseDecoder {
super(maxInitialLineLength, maxHeaderSize, maxChunkSize, validateHeaders);
}

Decoder(int maxInitialLineLength, int maxHeaderSize, int maxChunkSize, boolean validateHeaders,
int initialBufferSize) {
super(maxInitialLineLength, maxHeaderSize, maxChunkSize, validateHeaders, initialBufferSize);
}

@Override
protected void decode(
ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,12 @@ protected HttpObjectDecoder(
protected HttpObjectDecoder(
int maxInitialLineLength, int maxHeaderSize, int maxChunkSize,
boolean chunkedSupported, boolean validateHeaders) {
this(maxInitialLineLength, maxHeaderSize, maxChunkSize, chunkedSupported, validateHeaders, 128);
}

protected HttpObjectDecoder(
int maxInitialLineLength, int maxHeaderSize, int maxChunkSize,
boolean chunkedSupported, boolean validateHeaders, int initialBufferSize) {
if (maxInitialLineLength <= 0) {
throw new IllegalArgumentException(
"maxInitialLineLength must be a positive integer: " +
Expand All @@ -178,12 +183,12 @@ protected HttpObjectDecoder(
"maxChunkSize must be a positive integer: " +
maxChunkSize);
}
AppendableCharSequence seq = new AppendableCharSequence(initialBufferSize);
lineParser = new LineParser(seq, maxInitialLineLength);
headerParser = new HeaderParser(seq, maxHeaderSize);
this.maxChunkSize = maxChunkSize;
this.chunkedSupported = chunkedSupported;
this.validateHeaders = validateHeaders;
AppendableCharSequence seq = new AppendableCharSequence(128);
lineParser = new LineParser(seq, maxInitialLineLength);
headerParser = new HeaderParser(seq, maxHeaderSize);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ public HttpRequestDecoder(
super(maxInitialLineLength, maxHeaderSize, maxChunkSize, true, validateHeaders);
}

public HttpRequestDecoder(
int maxInitialLineLength, int maxHeaderSize, int maxChunkSize, boolean validateHeaders,
int initialBufferSize) {
super(maxInitialLineLength, maxHeaderSize, maxChunkSize, true, validateHeaders, initialBufferSize);
}

@Override
protected HttpMessage createMessage(String[] initialLine) throws Exception {
return new DefaultHttpRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ public HttpResponseDecoder(
super(maxInitialLineLength, maxHeaderSize, maxChunkSize, true, validateHeaders);
}

public HttpResponseDecoder(
int maxInitialLineLength, int maxHeaderSize, int maxChunkSize, boolean validateHeaders,
int initialBufferSize) {
super(maxInitialLineLength, maxHeaderSize, maxChunkSize, true, validateHeaders, initialBufferSize);
}

@Override
protected HttpMessage createMessage(String[] initialLine) {
return new DefaultHttpResponse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ public HttpServerCodec(int maxInitialLineLength, int maxHeaderSize, int maxChunk
new HttpResponseEncoder());
}

/**
* Creates a new instance with the specified decoder options.
*/
public HttpServerCodec(int maxInitialLineLength, int maxHeaderSize, int maxChunkSize, boolean validateHeaders,
int initialBufferSize) {
super(
new HttpRequestDecoder(maxInitialLineLength, maxHeaderSize, maxChunkSize, validateHeaders, initialBufferSize),
new HttpResponseEncoder());
}

/**
* Upgrades to another protocol from HTTP. Removes the {@link HttpRequestDecoder} and
* {@link HttpResponseEncoder} from the pipeline.
Expand Down

0 comments on commit a15ff32

Please sign in to comment.