Skip to content
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

feature: use mcache to replace bytebufferpool #1030

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/app/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ func (ctx *RequestContext) FileAttachment(filepath, filename string) {

// SetBodyString sets response body to the given value.
func (ctx *RequestContext) SetBodyString(body string) {
ctx.Response.SetBodyString(body)
ctx.Response.SetBodyString(body, string(ctx.URI().RequestURI()))
}

// SetContentTypeBytes sets response Content-Type.
Expand Down
51 changes: 51 additions & 0 deletions pkg/app/server/hertz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"io/ioutil"
"net"
"net/http"
"runtime"
"strings"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -248,6 +249,56 @@
_ = hertz.Shutdown(ctx)
}

func TestBigBodyBug(t *testing.T) {
runtime.GOMAXPROCS(3)
hertz := New(WithHostPorts("127.0.0.1:8888"))
hertz.GET("/test1", func(c context.Context, ctx *app.RequestContext) {
body := make([]byte, 1024*1024*9)
ctx.SetBodyString(string(body))
})
hertz.GET("/test2", func(c context.Context, ctx *app.RequestContext) {
body := make([]byte, 1024)
ctx.SetBodyString(string(body))
})
hertz.GET("/test3", func(c context.Context, ctx *app.RequestContext) {
body := make([]byte, 1024*2)
ctx.SetBodyString(string(body))
})
go hertz.Run()

go func() {
for i := 0; i < 2; i++ {
go func() {
for true {

Check failure on line 272 in pkg/app/server/hertz_test.go

View workflow job for this annotation

GitHub Actions / staticcheck

[staticcheck] reported by reviewdog 🐶 should use for {} instead of for true {} Raw Output: {"source":{"name":"staticcheck","url":"https://staticcheck.io"},"message":"should use for {} instead of for true {}","code":{"value":"S1006","url":"https://staticcheck.io/docs/checks#S1006"},"location":{"path":"/root/runner/cloudwego-lq-runner-02/hertz/hertz/pkg/app/server/hertz_test.go","range":{"start":{"line":272,"column":5}}},"severity":"ERROR"}

Check failure on line 272 in pkg/app/server/hertz_test.go

View workflow job for this annotation

GitHub Actions / lint-and-ut (1.18)

S1006: should use for {} instead of for true {} (gosimple)

Check failure on line 272 in pkg/app/server/hertz_test.go

View workflow job for this annotation

GitHub Actions / lint-and-ut (1.19)

S1006: should use for {} instead of for true {} (gosimple)

Check failure on line 272 in pkg/app/server/hertz_test.go

View workflow job for this annotation

GitHub Actions / lint-and-ut (1.20)

S1006: should use for {} instead of for true {} (gosimple)
http.Get("http://127.0.0.1:8888/test1")
}
}()
}
}()

go func() {
for i := 0; i < 5; i++ {
go func() {
for true {

Check failure on line 282 in pkg/app/server/hertz_test.go

View workflow job for this annotation

GitHub Actions / staticcheck

[staticcheck] reported by reviewdog 🐶 should use for {} instead of for true {} Raw Output: {"source":{"name":"staticcheck","url":"https://staticcheck.io"},"message":"should use for {} instead of for true {}","code":{"value":"S1006","url":"https://staticcheck.io/docs/checks#S1006"},"location":{"path":"/root/runner/cloudwego-lq-runner-02/hertz/hertz/pkg/app/server/hertz_test.go","range":{"start":{"line":282,"column":5}}},"severity":"ERROR"}

Check failure on line 282 in pkg/app/server/hertz_test.go

View workflow job for this annotation

GitHub Actions / lint-and-ut (1.18)

S1006: should use for {} instead of for true {} (gosimple)

Check failure on line 282 in pkg/app/server/hertz_test.go

View workflow job for this annotation

GitHub Actions / lint-and-ut (1.19)

S1006: should use for {} instead of for true {} (gosimple)

Check failure on line 282 in pkg/app/server/hertz_test.go

View workflow job for this annotation

GitHub Actions / lint-and-ut (1.20)

S1006: should use for {} instead of for true {} (gosimple)
http.Get("http://127.0.0.1:8888/test2")
}
}()
}
}()

go func() {
for i := 0; i < 5; i++ {
go func() {
for true {

Check failure on line 292 in pkg/app/server/hertz_test.go

View workflow job for this annotation

GitHub Actions / staticcheck

[staticcheck] reported by reviewdog 🐶 should use for {} instead of for true {} Raw Output: {"source":{"name":"staticcheck","url":"https://staticcheck.io"},"message":"should use for {} instead of for true {}","code":{"value":"S1006","url":"https://staticcheck.io/docs/checks#S1006"},"location":{"path":"/root/runner/cloudwego-lq-runner-02/hertz/hertz/pkg/app/server/hertz_test.go","range":{"start":{"line":292,"column":5}}},"severity":"ERROR"}

Check failure on line 292 in pkg/app/server/hertz_test.go

View workflow job for this annotation

GitHub Actions / lint-and-ut (1.18)

S1006: should use for {} instead of for true {} (gosimple)

Check failure on line 292 in pkg/app/server/hertz_test.go

View workflow job for this annotation

GitHub Actions / lint-and-ut (1.19)

S1006: should use for {} instead of for true {} (gosimple)

Check failure on line 292 in pkg/app/server/hertz_test.go

View workflow job for this annotation

GitHub Actions / lint-and-ut (1.20)

S1006: should use for {} instead of for true {} (gosimple)
http.Get("http://127.0.0.1:8888/test3")
}
}()
}
}()

<-make(chan struct{})
}

func TestNotAbsolutePath(t *testing.T) {
engine := New(WithHostPorts("127.0.0.1:9990"))
engine.POST("/", func(c context.Context, ctx *app.RequestContext) {
Expand Down
14 changes: 13 additions & 1 deletion pkg/common/bytebufferpool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ import (
"sort"
"sync"
"sync/atomic"

"github.com/bytedance/gopkg/lang/mcache"
)

const (
Expand All @@ -54,7 +56,7 @@ const (
minSize = 1 << minBitSize
maxSize = 1 << (minBitSize + steps - 1)

calibrateCallsThreshold = 42000
calibrateCallsThreshold = 10 // Just for test
maxPercentile = 0.95
)

Expand Down Expand Up @@ -96,6 +98,16 @@ func (p *Pool) Get() *ByteBuffer {
}
}

func (p *Pool) GetWithSize(size int) *ByteBuffer {
return &ByteBuffer{
B: mcache.Malloc(size)[:0],
}
}

func (p *Pool) PutWithByte(b []byte) {
mcache.Free(b)
}

// Put returns byte buffer to the pool.
//
// ByteBuffer.B mustn't be touched after returning it to the pool.
Expand Down
2 changes: 1 addition & 1 deletion pkg/common/config/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const (
defaultAddr = ":8888"
defaultNetwork = "tcp"
defaultBasePath = "/"
defaultMaxRequestBodySize = 4 * 1024 * 1024
defaultMaxRequestBodySize = 0
defaultWaitExitTimeout = time.Second * 5
defaultReadBufferSize = 4 * 1024
)
Expand Down
24 changes: 19 additions & 5 deletions pkg/protocol/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
package protocol

import (
"fmt"
"io"
"net"
"sync"
Expand Down Expand Up @@ -138,9 +139,11 @@ func (resp *Response) SetConnectionClose() {
}

// SetBodyString sets response body.
func (resp *Response) SetBodyString(body string) {
resp.CloseBodyStream() //nolint:errcheck
resp.BodyBuffer().SetString(body) //nolint:errcheck
func (resp *Response) SetBodyString(body, url string) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Break change is forbidden in v1 regular iteration.

resp.CloseBodyStream() //nolint:errcheck
// resp.BodyBuffer().SetString(body) //nolint:errcheck
// resp.BodyBuffer(url).SetString(body) //nolint:errcheck
resp.BodyBufferWithSize(len(body), url).SetString(body) //nolint:errcheck
}

func (resp *Response) ConstructBodyStream(body *bytebufferpool.ByteBuffer, bodyStream io.Reader) {
Expand Down Expand Up @@ -299,7 +302,8 @@ func (resp *Response) ResetBody() {
resp.body.Reset()
return
}
responseBodyPool.Put(resp.body)
// responseBodyPool.Put(resp.body)
responseBodyPool.PutWithByte(resp.body.B)
resp.body = nil
}
}
Expand Down Expand Up @@ -385,11 +389,21 @@ func (resp *Response) CloseBodyStream() error {
return err
}

func (resp *Response) BodyBuffer() *bytebufferpool.ByteBuffer {
func (resp *Response) BodyBufferWithSize(size int, uri string) *bytebufferpool.ByteBuffer {
if resp.body == nil {
resp.body = responseBodyPool.GetWithSize(size)
}
resp.bodyRaw = nil
fmt.Printf("flipped url=%s resp Body cap=%d\n", uri, cap(resp.body.B))
return resp.body
}

func (resp *Response) BodyBuffer(url ...string) *bytebufferpool.ByteBuffer {
if resp.body == nil {
resp.body = responseBodyPool.Get()
}
resp.bodyRaw = nil
fmt.Printf("flipped url=%s resp Body cap=%d\n", url, cap(resp.body.B))
return resp.body
}

Expand Down
Loading