Skip to content

fix: compare body.Cap() with maxKeepBodySize instead of body.Len() #1294

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

Merged
merged 2 commits into from
Mar 25, 2025

Conversation

AsterDY
Copy link
Contributor

@AsterDY AsterDY commented Mar 13, 2025

What type of PR is this?

fix

Check the PR title.

  • This PR title match the format: <type>(optional scope): <description>
  • The description of this PR title is user-oriented and clear enough for others to understand.
  • Attach the PR updating the user documentation if the current PR requires user awareness at the usage level. User docs repo

(Optional) Translate the PR title into Chinese.

(Optional) More detailed description for this PR(en: English/zh: Chinese).

en: hertz use http-body-slice's length to compare with MaxKeepBodySize to decide if keep it in request/response or put into requestBodyPool on ResetBody().

// ResetBody resets request body.
func (req *Request) ResetBody() {
	req.bodyRaw = nil
	req.RemoveMultipartFormFiles()
	req.CloseBodyStream() //nolint:errcheck
	if req.body != nil {
		if req.body.Len() <= req.maxKeepBodySize { // use Length to judge here
			req.body.Reset()
			return
		}
		requestBodyPool.Put(req.body) // big body will be put into buffer pool
		req.body = nil
	}
}

However, a big request body (.Len()>MaxKeepBodySize) which was firstly put into requestBodyPool is still likely to be used in subsequent request, even if the actually request data is small..

func (req *Request) BodyBuffer() *bytebufferpool.ByteBuffer {
	if req.body == nil {
		req.body = requestBodyPool.Get() // big body may be reused for small request
	}
	req.bodyRaw = nil
	return req.body
}

therefore, once the subsequent request finish and back to ResetBody() logic, although its body's Cap is much larger than Len, it will still be kept in request/repsponse, which may introduce memory leak.

zh(optional): 将 MaxKeepBody 的判定条件由 Len() 改成 Cap()

(Optional) Which issue(s) this PR fixes:

(Optional) The PR that updates user documentation:

@AsterDY AsterDY requested review from a team as code owners March 13, 2025 03:38
Copy link

codecov bot commented Mar 13, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 72.39%. Comparing base (576bc6f) to head (98ac70f).
Report is 1 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #1294   +/-   ##
========================================
  Coverage    72.38%   72.39%           
========================================
  Files          139      139           
  Lines        16235    16237    +2     
========================================
+ Hits         11752    11754    +2     
  Misses        3888     3888           
  Partials       595      595           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@AsterDY AsterDY changed the title Fix/max keep body size fix: compare body.Cap() with maxKeepBodySize instead of body.Len() Mar 13, 2025
@AsterDY AsterDY force-pushed the fix/maxKeepBodySize branch from f824e5d to 701411e Compare March 25, 2025 03:25
YangruiEmma
YangruiEmma previously approved these changes Mar 25, 2025
@AsterDY AsterDY force-pushed the fix/maxKeepBodySize branch from 62118c1 to 98ac70f Compare March 25, 2025 04:37
@AsterDY AsterDY merged commit 80498eb into develop Mar 25, 2025
32 checks passed
@AsterDY AsterDY deleted the fix/maxKeepBodySize branch March 25, 2025 05:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

5 participants