HTTP/2 Make DefaultHttp2HeadersDecoder's Http2Headers object creation extensible

Motivation:
It is generally useful to override DefaultHttp2HeadersDecoder's creation of a new Http2Headers object so more optimized versions can be substituted if the use case allows for it.

Modifications:
- DefaultHttp2HeadersDecoder should support an overridable method to generate the new Http2Headers object for each decode operation

Result:
DefaultHttp2HeadersDecoder is more extensible.
Fixes https://github.com/netty/netty/issues/6591.
This commit is contained in:
Scott Mitchell 2017-03-31 17:56:59 -07:00
parent e8da5e5162
commit 225d10e1ad

View File

@ -112,7 +112,7 @@ public class DefaultHttp2HeadersDecoder implements Http2HeadersDecoder, Http2Hea
@Override @Override
public Http2Headers decodeHeaders(int streamId, ByteBuf headerBlock) throws Http2Exception { public Http2Headers decodeHeaders(int streamId, ByteBuf headerBlock) throws Http2Exception {
try { try {
final Http2Headers headers = new DefaultHttp2Headers(validateHeaders, (int) headerArraySizeAccumulator); final Http2Headers headers = newHeaders();
hpackDecoder.decode(streamId, headerBlock, headers); hpackDecoder.decode(streamId, headerBlock, headers);
headerArraySizeAccumulator = HEADERS_COUNT_WEIGHT_NEW * headers.size() + headerArraySizeAccumulator = HEADERS_COUNT_WEIGHT_NEW * headers.size() +
HEADERS_COUNT_WEIGHT_HISTORICAL * headerArraySizeAccumulator; HEADERS_COUNT_WEIGHT_HISTORICAL * headerArraySizeAccumulator;
@ -126,4 +126,28 @@ public class DefaultHttp2HeadersDecoder implements Http2HeadersDecoder, Http2Hea
throw connectionError(COMPRESSION_ERROR, e, e.getMessage()); throw connectionError(COMPRESSION_ERROR, e, e.getMessage());
} }
} }
/**
* A weighted moving average estimating how many headers are expected during the decode process.
* @return an estimate of how many headers are expected during the decode process.
*/
protected final int numberOfHeadersGuess() {
return (int) headerArraySizeAccumulator;
}
/**
* Determines if the headers should be validated as a result of the decode operation.
* @return {@code true} if the headers should be validated as a result of the decode operation.
*/
protected final boolean validateHeaders() {
return validateHeaders;
}
/**
* Create a new {@link Http2Headers} object which will store the results of the decode operation.
* @return a new {@link Http2Headers} object which will store the results of the decode operation.
*/
protected Http2Headers newHeaders() {
return new DefaultHttp2Headers(validateHeaders, (int) headerArraySizeAccumulator);
}
} }