@@ -52,6 +52,17 @@ const (
5252 DefaultConnBufferSize = 32 * 1024
5353)
5454
55+ func normalizeConnBufferSize (bufferSize int ) int {
56+ if bufferSize == 0 {
57+ return DefaultConnBufferSize
58+ }
59+ return bufferSize
60+ }
61+
62+ func estimateConnBufferMemory (bufferSize int ) int64 {
63+ return int64 (normalizeConnBufferSize (bufferSize ) * 2 )
64+ }
65+
5566type rwStatus int
5667
5768const (
@@ -116,9 +127,7 @@ func getPooledWriter(conn net.Conn, size int) *bufio.Writer {
116127}
117128
118129func newBasicReadWriter (conn net.Conn , bufferSize int ) * basicReadWriter {
119- if bufferSize == 0 {
120- bufferSize = DefaultConnBufferSize
121- }
130+ bufferSize = normalizeConnBufferSize (bufferSize )
122131 return & basicReadWriter {
123132 Conn : conn ,
124133 ReadWriter : bufio .NewReadWriter (getPooledReader (conn , bufferSize ), getPooledWriter (conn , bufferSize )),
@@ -274,26 +283,33 @@ type PacketIO interface {
274283
275284// PacketIO is a helper to read and write sql and proxy protocol.
276285type packetIO struct {
277- lastKeepAlive config.KeepAlive
278- rawConn net.Conn
279- readWriter packetReadWriter
280- limitReader io.LimitedReader // reuse memory to reduce allocation
281- logger * zap.Logger
282- remoteAddr net.Addr
283- wrap error
284- header [4 ]byte // reuse memory to reduce allocation
285- readPacketLimit int
286- inPackets uint64
287- outPackets uint64
286+ lastKeepAlive config.KeepAlive
287+ rawConn net.Conn
288+ readWriter packetReadWriter
289+ limitReader io.LimitedReader // reuse memory to reduce allocation
290+ logger * zap.Logger
291+ remoteAddr net.Addr
292+ wrap error
293+ header [4 ]byte // reuse memory to reduce allocation
294+ readPacketLimit int
295+ inPackets uint64
296+ outPackets uint64
297+ connBufferEstimate int64
298+ connBufferTracker ConnBufferMemoryTracker
299+ connBufferTracked bool
300+ releaseConnBuffer sync.Once
288301}
289302
290303func NewPacketIO (conn net.Conn , lg * zap.Logger , bufferSize int , opts ... PacketIOption ) * packetIO {
304+ bufferSize = normalizeConnBufferSize (bufferSize )
291305 p := & packetIO {
292- rawConn : conn ,
293- logger : lg ,
294- readWriter : newBasicReadWriter (conn , bufferSize ),
306+ rawConn : conn ,
307+ logger : lg ,
308+ readWriter : newBasicReadWriter (conn , bufferSize ),
309+ connBufferEstimate : estimateConnBufferMemory (bufferSize ),
295310 }
296311 p .ApplyOpts (opts ... )
312+ p .trackConnBufferMemory ()
297313 return p
298314}
299315
@@ -303,6 +319,22 @@ func (p *packetIO) ApplyOpts(opts ...PacketIOption) {
303319 }
304320}
305321
322+ func (p * packetIO ) trackConnBufferMemory () {
323+ if p .connBufferTracked || p .connBufferTracker == nil || p .connBufferEstimate == 0 {
324+ return
325+ }
326+ p .connBufferTracker .UpdateConnBufferMemory (p .connBufferEstimate )
327+ p .connBufferTracked = true
328+ }
329+
330+ func (p * packetIO ) releaseConnBufferMemory () {
331+ p .releaseConnBuffer .Do (func () {
332+ if p .connBufferTracked && p .connBufferTracker != nil && p .connBufferEstimate != 0 {
333+ p .connBufferTracker .UpdateConnBufferMemory (- p .connBufferEstimate )
334+ }
335+ })
336+ }
337+
306338func (p * packetIO ) wrapErr (err error ) error {
307339 return errors .Wrap (err , p .wrap )
308340}
@@ -553,6 +585,7 @@ func (p *packetIO) GracefulClose() error {
553585}
554586
555587func (p * packetIO ) Close () error {
588+ defer p .releaseConnBufferMemory ()
556589 var errs []error
557590 /*
558591 TODO: flush when we want to smoothly exit
0 commit comments