Skip to content
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
48 changes: 45 additions & 3 deletions pkg/proxy/net/packetio.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"crypto/tls"
"io"
"net"
"sync"
"time"

"github.com/pingcap/tiproxy/lib/config"
Expand All @@ -42,6 +43,19 @@ var (
ErrInvalidSequence = errors.New("invalid sequence")
)

var (
readerPool = sync.Pool{
New: func() any {
return bufio.NewReaderSize(nil, DefaultConnBufferSize)
},
}
writerPool = sync.Pool{
New: func() any {
return bufio.NewWriterSize(nil, DefaultConnBufferSize)
},
}
)

const (
DefaultConnBufferSize = 32 * 1024
)
Expand Down Expand Up @@ -86,16 +100,27 @@ type basicReadWriter struct {
inBytes uint64
outBytes uint64
sequence uint8
pooled bool
}

func newBasicReadWriter(conn net.Conn, bufferSize int) *basicReadWriter {
if bufferSize == 0 {
bufferSize = DefaultConnBufferSize
}
return &basicReadWriter{
Conn: conn,
ReadWriter: bufio.NewReadWriter(bufio.NewReaderSize(conn, bufferSize), bufio.NewWriterSize(conn, bufferSize)),
brw := &basicReadWriter{
Conn: conn,
}
if bufferSize == DefaultConnBufferSize {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm wondering how to take full advantage of the buffer pool.
If the global buffer size config changes, all the memory in the pool is useless and becomes a leak. Meanwhile, subsequent allocations can't use the pool.

Copy link
Author

Choose a reason for hiding this comment

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

What if we check the capacity on Get() using the built-in .Size() method? If the buffer from the pool doesn't match the current bufferSize config, we can just discard it and allocate a fresh one.
Does this approach look good? I will verify and update the PR as soon as I get some time

r := readerPool.Get().(*bufio.Reader)
r.Reset(conn)
w := writerPool.Get().(*bufio.Writer)
w.Reset(conn)
brw.ReadWriter = bufio.NewReadWriter(r, w)
brw.pooled = true
} else {
brw.ReadWriter = bufio.NewReadWriter(bufio.NewReaderSize(conn, bufferSize), bufio.NewWriterSize(conn, bufferSize))
}
return brw
}

func (brw *basicReadWriter) Read(b []byte) (n int, err error) {
Expand Down Expand Up @@ -153,6 +178,22 @@ func (brw *basicReadWriter) ResetSequence() {
brw.sequence = 0
}

func (brw *basicReadWriter) Free() {
if brw.pooled {
brw.pooled = false
brw.ReadWriter.Reader.Reset(nil)
brw.ReadWriter.Writer.Reset(nil)
readerPool.Put(brw.ReadWriter.Reader)
writerPool.Put(brw.ReadWriter.Writer)
}
}

func (brw *basicReadWriter) Close() error {
err := brw.Conn.Close()
brw.Free()
return err
}

func (brw *basicReadWriter) TLSConnectionState() tls.ConnectionState {
return tls.ConnectionState{}
}
Expand Down Expand Up @@ -496,6 +537,7 @@ func (p *packetIO) Close() error {
errs = append(errs, err)
}
*/

if err := p.readWriter.Close(); err != nil && !errors.Is(err, net.ErrClosed) {
errs = append(errs, errors.WithStack(err))
}
Expand Down
52 changes: 52 additions & 0 deletions pkg/proxy/net/packetio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -719,3 +719,55 @@ func runForwardBenchmark(b *testing.B, f func(packetIO1, packetIO2 *packetIO)) {
_ = packetIO2.Close()
wg.Wait()
}

func TestPacketIOPooling(t *testing.T) {
testTCPConn(t,
func(t *testing.T, cli *packetIO) {
brw, ok := cli.readWriter.(*basicReadWriter)
require.True(t, ok)
require.True(t, brw.pooled, "pooled flag should be true for default buffer size")

require.NoError(t, cli.WritePacket([]byte("pooltest"), true))
},
func(t *testing.T, srv *packetIO) {
brw, ok := srv.readWriter.(*basicReadWriter)
require.True(t, ok)
require.True(t, brw.pooled, "pooled flag should be true for default buffer size")

data, err := srv.ReadPacket()
require.NoError(t, err)
require.Equal(t, []byte("pooltest"), data)
},
1,
)

lg, _ := logger.CreateLoggerForTest(t)
cli, srv := net.Pipe()
cliIO := NewPacketIO(cli, lg, DefaultConnBufferSize*2)
srvIO := NewPacketIO(srv, lg, DefaultConnBufferSize*2)
brw, ok := cliIO.readWriter.(*basicReadWriter)
require.True(t, ok)
require.False(t, brw.pooled, "pooled flag should be false for non-default buffer size")
_ = cliIO.Close()
_ = srvIO.Close()

testTCPConn(t,
func(t *testing.T, cli *packetIO) {
require.NoError(t, cli.Close())
require.NoError(t, cli.Close())
},
func(t *testing.T, srv *packetIO) {
require.NoError(t, srv.Close())
require.NoError(t, srv.Close())
},
1,
)

for i := 0; i < 100; i++ {
c1, c2 := net.Pipe()
p1 := NewPacketIO(c1, lg, DefaultConnBufferSize)
p2 := NewPacketIO(c2, lg, DefaultConnBufferSize)
_ = p1.Close()
_ = p2.Close()
}
}