-
Notifications
You must be signed in to change notification settings - Fork 895
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Override HttpRequestDecoder.createMessage()
for perfomance
#4863
Merged
Merged
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
1de569a
Fork netty's `httpServerCodec` and replace its usage
echo304 6b41e20
Fix test compile failure
echo304 ffa914f
Add licenses
echo304 9911367
Implement ArmeriaHttpHeaders
echo304 b688903
Implement `ArmeriaDefaultHttpRequest`
echo304 9de5522
Modify `HttpServerCodec.createMessage` to use `ArmeriaDefaultHttpRequ…
echo304 743a6fc
Modify `Http1RequestDecoder.channelRead` to use `ArmeriaDefaultHttpRe…
echo304 efd741e
Merge branch 'main' into custom-http-server-codec
echo304 bba7d8d
Reflect code review
echo304 8bccbc8
Fix styles
echo304 65ad9cd
Fix constructor of `ArmeriaHttpHeaders` to invoke `add()` method of i…
echo304 ce07dc1
Add `headers()` getter on `ArmeriaDefaultHttpRequest`
echo304 b81f037
Add test for `ArmeriaHttpHeaders` and fix logic
echo304 a2a2f48
Try another approach
echo304 deaa03c
Fix infinite loop bug
echo304 665c104
Embed building headers logic into `ArmeriaHttpHeaders.buildRequestHea…
echo304 2391c7b
Merge remote-tracking branch 'upstream/main' into custom-http-server-…
echo304 7515d8f
Fix ArmeriaHttpHeadersTest failed tests
echo304 89bc4ea
Merge branch 'main' into custom-http-server-codec
echo304 39092ea
Fix failed shouldSupportBindingOnDomainSocket test
echo304 c9f7520
Fix failed testUnsupportedMethod test
echo304 9ecd024
Fix styles
echo304 2cc3f13
Merge branch 'main' into custom-http-server-codec
echo304 f48f148
Merge branch 'main' into custom-http-server-codec
jrhee17 19ab386
minor cleanups
jrhee17 340a6f5
hide over-exposed class
jrhee17 ca51df6
lint
jrhee17 c80eb0c
Hide static methods and Change ArmeriaHttpHeadersFactory to enum
minwoox 1eaad3a
minor fix
ikhoon File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
core/src/main/java/com/linecorp/armeria/server/ArmeriaHttpHeadersFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright 2024 LINE Corporation | ||
* | ||
* LINE Corporation licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package com.linecorp.armeria.server; | ||
|
||
import io.netty.handler.codec.http.HttpHeaders; | ||
import io.netty.handler.codec.http.HttpHeadersFactory; | ||
|
||
enum ArmeriaHttpHeadersFactory implements HttpHeadersFactory { | ||
|
||
INSTANCE; | ||
|
||
@Override | ||
public HttpHeaders newHeaders() { | ||
return new NettyHttp1Headers(); | ||
} | ||
|
||
@Override | ||
public HttpHeaders newEmptyHeaders() { | ||
return new NettyHttp1Headers(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
core/src/main/java/com/linecorp/armeria/server/HttpServerCodec.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
/* | ||
* Copyright 2023 LINE Corporation | ||
* | ||
* LINE Corporation licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
/* | ||
* Copyright 2012 The Netty Project | ||
* | ||
* The Netty Project licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package com.linecorp.armeria.server; | ||
minwoox marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import static io.netty.handler.codec.http.HttpObjectDecoder.DEFAULT_MAX_CHUNK_SIZE; | ||
import static io.netty.handler.codec.http.HttpObjectDecoder.DEFAULT_MAX_HEADER_SIZE; | ||
import static io.netty.handler.codec.http.HttpObjectDecoder.DEFAULT_MAX_INITIAL_LINE_LENGTH; | ||
|
||
import java.util.ArrayDeque; | ||
import java.util.List; | ||
import java.util.Queue; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.channel.CombinedChannelDuplexHandler; | ||
import io.netty.handler.codec.http.HttpClientCodec; | ||
import io.netty.handler.codec.http.HttpHeaderNames; | ||
import io.netty.handler.codec.http.HttpMessage; | ||
import io.netty.handler.codec.http.HttpMethod; | ||
import io.netty.handler.codec.http.HttpRequest; | ||
import io.netty.handler.codec.http.HttpRequestDecoder; | ||
import io.netty.handler.codec.http.HttpResponse; | ||
import io.netty.handler.codec.http.HttpResponseEncoder; | ||
import io.netty.handler.codec.http.HttpServerUpgradeHandler; | ||
import io.netty.handler.codec.http.HttpStatusClass; | ||
import io.netty.handler.codec.http.HttpVersion; | ||
|
||
minwoox marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/** | ||
* Forked from {@link HttpClientCodec} to override {@code HttpServerRequestDecoder#createMessage(String[])}. | ||
* A combination of {@link HttpRequestDecoder} and {@link HttpResponseEncoder} | ||
* which enables easier server side HTTP implementation. | ||
*/ | ||
final class HttpServerCodec extends CombinedChannelDuplexHandler<HttpRequestDecoder, HttpResponseEncoder> | ||
implements HttpServerUpgradeHandler.SourceCodec { | ||
|
||
// Forked from https://github.com/netty/netty/blob/cf624c93c5f97097f1b13fe926ed50c32c8b1430/codec-http/src/main/java/io/netty/handler/codec/http/HttpServerCodec.java | ||
|
||
/** A queue that is used for correlating a request and a response. */ | ||
private final Queue<HttpMethod> queue = new ArrayDeque<HttpMethod>(); | ||
|
||
/** | ||
* Creates a new instance with the default decoder options | ||
* ({@code maxInitialLineLength (4096}}, {@code maxHeaderSize (8192)}, and | ||
* {@code maxChunkSize (8192)}). | ||
*/ | ||
HttpServerCodec() { | ||
this(DEFAULT_MAX_INITIAL_LINE_LENGTH, DEFAULT_MAX_HEADER_SIZE, DEFAULT_MAX_CHUNK_SIZE); | ||
} | ||
|
||
/** | ||
* Creates a new instance with the specified decoder options. | ||
*/ | ||
HttpServerCodec(int maxInitialLineLength, int maxHeaderSize, int maxChunkSize) { | ||
init(new HttpServerRequestDecoder(maxInitialLineLength, maxHeaderSize, maxChunkSize), | ||
new HttpServerResponseEncoder()); | ||
} | ||
|
||
/** | ||
* Upgrades to another protocol from HTTP. Removes the {@link HttpRequestDecoder} and | ||
* {@link HttpResponseEncoder} from the pipeline. | ||
*/ | ||
@Override | ||
public void upgradeFrom(ChannelHandlerContext ctx) { | ||
ctx.pipeline().remove(this); | ||
} | ||
|
||
private final class HttpServerRequestDecoder extends HttpRequestDecoder { | ||
|
||
HttpServerRequestDecoder(int maxInitialLineLength, int maxHeaderSize, int maxChunkSize) { | ||
super(maxInitialLineLength, maxHeaderSize, maxChunkSize); | ||
} | ||
|
||
@Override | ||
protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception { | ||
final int oldSize = out.size(); | ||
super.decode(ctx, buffer, out); | ||
final int size = out.size(); | ||
for (int i = oldSize; i < size; i++) { | ||
final Object obj = out.get(i); | ||
if (obj instanceof HttpRequest) { | ||
queue.add(((HttpRequest) obj).method()); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
protected HttpMessage createMessage(String[] initialLine) throws Exception { | ||
return new NettyHttp1Request( | ||
HttpVersion.valueOf(initialLine[2]), | ||
HttpMethod.valueOf(initialLine[0]), initialLine[1]); | ||
} | ||
} | ||
|
||
private final class HttpServerResponseEncoder extends HttpResponseEncoder { | ||
|
||
private HttpMethod method; | ||
|
||
@Override | ||
protected void sanitizeHeadersBeforeEncode(HttpResponse msg, boolean isAlwaysEmpty) { | ||
if (!isAlwaysEmpty && HttpMethod.CONNECT.equals(method) && | ||
msg.status().codeClass() == HttpStatusClass.SUCCESS) { | ||
// Stripping Transfer-Encoding: | ||
// See section 3.3.1 of https://datatracker.ietf.org/doc/rfc7230 | ||
msg.headers().remove(HttpHeaderNames.TRANSFER_ENCODING); | ||
return; | ||
} | ||
|
||
super.sanitizeHeadersBeforeEncode(msg, isAlwaysEmpty); | ||
} | ||
|
||
@Override | ||
protected boolean isContentAlwaysEmpty(@SuppressWarnings("unused") HttpResponse msg) { | ||
method = queue.poll(); | ||
return HttpMethod.HEAD.equals(method) || super.isContentAlwaysEmpty(msg); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that I haven't migrated this method to use
purgeHttp1OnlyHeaders
since this is probably slower for other code paths using this method without zero-copy