-
Notifications
You must be signed in to change notification settings - Fork 1.9k
http2: pre-allocate body buffer to eliminate realloc overhead #11972
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -743,7 +743,48 @@ static int http2_data_chunk_recv_callback(nghttp2_session *inner_session, | |
| } | ||
|
|
||
| if (stream->request.body == NULL) { | ||
| stream->request.body = cfl_sds_create_size(len); | ||
| /* | ||
| * Pre-allocate body buffer to avoid repeated reallocs as 16KB HTTP/2 | ||
| * frames arrive. Three strategies, in priority order: | ||
| * 1. Content-Length header — exact size (non-gRPC HTTP/2) | ||
| * 2. gRPC 5-byte frame header — parse message length for exact size | ||
| * 3. Fallback to frame size (len) — original behavior | ||
| */ | ||
| size_t initial_size; | ||
|
|
||
| if (stream->request.content_length > 0) { | ||
| initial_size = stream->request.content_length; | ||
| } | ||
| else if (len >= 5 && | ||
| stream->request.content_type != NULL && | ||
| strncmp(stream->request.content_type, "application/grpc", 16) == 0) { | ||
| /* gRPC length-prefixed frame: 1 byte flags + 4 bytes message length */ | ||
| uint32_t grpc_msg_len = ((uint32_t) data[1] << 24) | | ||
| ((uint32_t) data[2] << 16) | | ||
| ((uint32_t) data[3] << 8) | | ||
| ((uint32_t) data[4]); | ||
| if ((size_t) grpc_msg_len > SIZE_MAX - 5) { | ||
| stream->status = HTTP_STREAM_STATUS_ERROR; | ||
| return -1; | ||
| } | ||
| initial_size = (size_t) grpc_msg_len + 5; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a gRPC DATA callback contains more bytes than the first length-prefixed message advertises (for example a zero-length first message followed by another message in the same DATA frame, or a malformed gRPC request), this sets Useful? React with 👍 / 👎.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ^ @yadoer |
||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| else { | ||
| initial_size = len; | ||
| } | ||
|
|
||
| if (initial_size < len) { | ||
| stream->status = HTTP_STREAM_STATUS_ERROR; | ||
| return -1; | ||
| } | ||
|
|
||
| if (http2_request_body_limit_exceeded(stream, initial_size)) { | ||
| stream->status = HTTP_STREAM_STATUS_ERROR; | ||
|
|
||
| return -1; | ||
| } | ||
|
|
||
| stream->request.body = cfl_sds_create_size(initial_size); | ||
|
|
||
| if (stream->request.body == NULL) { | ||
| stream->status = HTTP_STREAM_STATUS_ERROR; | ||
|
|
||
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.
We need to define variables on the top of functions due to coding conventions on this project.
See: https://github.com/fluent/fluent-bit/blob/master/CONTRIBUTING.md#variable-definitions