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

allow packet.Conn buffer size to be adjustable #892

Merged
merged 1 commit into from
Jun 23, 2024

Conversation

dvilaverde
Copy link
Contributor

@dvilaverde dvilaverde commented Jun 21, 2024

Our usage of this library is in an application that will maintain a few thousand active MySQL connections from a single instance and we've noticed that the application is holding a lot of memory in packet.Conn, specifically the bufio.Reader instances are holding about 500MB of the total 800MB heap.

This PR will allow our application to change the hardcoded 65536 buffer size to something more reasonable for our use case (maybe 4096), which should reduce our heap by at least 50%.

srv := CreateMockServer(t)
defer srv.Stop()

SetDSNOptions(map[string]DriverOption{
Copy link
Contributor Author

Choose a reason for hiding this comment

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

our application is already using SetDSNOptions so we're planning on using the same technique for the BufferSize. I didn't want to add more configuration to the public README of this project because I felt that the default for most people is probably fine and therefore no need to add more configuration options.

@@ -53,19 +53,23 @@ type Conn struct {
}

func NewConn(conn net.Conn) *Conn {
return NewBufferedConn(conn, 65536) // 64kb
Copy link
Contributor Author

@dvilaverde dvilaverde Jun 21, 2024

Choose a reason for hiding this comment

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

keeping this NewConn function for compatibility reasons. As of now just the server code and most unit tests use this. The client code will be using NewBufferedConn.

@@ -94,6 +99,7 @@ type Dialer func(ctx context.Context, network, address string) (net.Conn, error)
func ConnectWithDialer(ctx context.Context, network, addr, user, password, dbName string, dialer Dialer, options ...Option) (*Conn, error) {
c := new(Conn)

c.BufferSize = defaultBufferSize
Copy link
Contributor Author

Choose a reason for hiding this comment

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

init the new packet Conn with the 64kb buffer size

@lance6716 lance6716 merged commit 8d0b3e3 into go-mysql-org:master Jun 23, 2024
13 checks passed
@dvilaverde dvilaverde deleted the buffer_size branch June 23, 2024 11:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants