[go: nahoru, domu]

Skip to content

Commit

Permalink
fix(grpc): use mcache to fix memory leak caused by grpc codec buffer …
Browse files Browse the repository at this point in the history
…to reuse memory incorrectly (#992)
  • Loading branch information
jayantxie committed Jun 21, 2023
1 parent c8fe849 commit 1c4ea3a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pkg/remote/trans/gonet/trans_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestMain(m *testing.M) {
// TestCreateListener test trans_server CreateListener success
func TestCreateListener(t *testing.T) {
// tcp init
addrStr := "127.0.0.1:9091"
addrStr := "127.0.0.1:9093"
addr = utils.NewNetAddr("tcp", addrStr)

// test
Expand Down
4 changes: 2 additions & 2 deletions pkg/remote/trans/netpoll/trans_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestMain(m *testing.M) {
// TestCreateListener test trans_server CreateListener success
func TestCreateListener(t *testing.T) {
// tcp init
addrStr := "127.0.0.1:9090"
addrStr := "127.0.0.1:9091"
addr = utils.NewNetAddr("tcp", addrStr)

// test
Expand All @@ -98,7 +98,7 @@ func TestCreateListener(t *testing.T) {
// TestBootStrap test trans_server BootstrapServer success
func TestBootStrap(t *testing.T) {
// tcp init
addrStr := "127.0.0.1:9090"
addrStr := "127.0.0.1:9092"
addr = utils.NewNetAddr("tcp", addrStr)

// test
Expand Down
27 changes: 20 additions & 7 deletions pkg/remote/trans/nphttp2/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,16 @@ import (
"net"
"sync"

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

"github.com/cloudwego/kitex/pkg/remote"
)

const (
// min is 4k
minMallocSize = 4 * 1024
)

// GRPCConn implement WriteFrame and ReadFrame
type GRPCConn interface {
net.Conn
Expand All @@ -45,11 +52,7 @@ var (

var bufferPool = sync.Pool{
New: func() interface{} {
return &buffer{
rbuf: make([]byte, 0, 4096),
wbuf: nil,
whdr: nil,
}
return &buffer{}
},
}

Expand All @@ -64,7 +67,13 @@ func (b *buffer) growRbuf(n int) {
if capacity >= n {
return
}
buf := make([]byte, 0, 2*capacity+n)
if n < minMallocSize {
n = minMallocSize
}
buf := mcache.Malloc(0, n)
if cap(b.rbuf) > 0 {
mcache.Free(b.rbuf)
}
b.rbuf = buf
}

Expand Down Expand Up @@ -95,7 +104,11 @@ func (b *buffer) Flush() (err error) {
}

func (b *buffer) Release(e error) (err error) {
b.rbuf = b.rbuf[:0]
if cap(b.rbuf) > 0 {
mcache.Free(b.rbuf)
}
b.conn = nil
b.rbuf = nil
b.whdr = nil
b.wbuf = nil
bufferPool.Put(b)
Expand Down

0 comments on commit 1c4ea3a

Please sign in to comment.