Skip to content

Commit

Permalink
opt: eliminate the inuse conn.inboundBuffer for idle connections
Browse files Browse the repository at this point in the history
  • Loading branch information
panjf2000 committed Nov 16, 2024
1 parent 1eb5e77 commit adcbdd2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
11 changes: 6 additions & 5 deletions connection_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ func (c *conn) sendTo(buf []byte) error {
func (c *conn) resetBuffer() {
c.buffer = c.buffer[:0]
c.inboundBuffer.Reset()
c.inboundBuffer.Done()
}

func (c *conn) Read(p []byte) (n int, err error) {
Expand Down Expand Up @@ -361,18 +362,18 @@ func (c *conn) Peek(n int) (buf []byte, err error) {
}

func (c *conn) Discard(n int) (int, error) {
if len(c.cache) > 0 {
bsPool.Put(c.cache)
c.cache = nil
}

inBufferLen := c.inboundBuffer.Buffered()
tempBufferLen := len(c.buffer)
if inBufferLen+tempBufferLen < n || n <= 0 {
c.resetBuffer()
return inBufferLen + tempBufferLen, nil
}

if len(c.cache) > 0 {
bsPool.Put(c.cache)
c.cache = nil
}

if c.inboundBuffer.IsEmpty() {
c.buffer = c.buffer[n:]
return n, nil
Expand Down
1 change: 1 addition & 0 deletions connection_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ func newUDPConn(el *eventloop, pc net.PacketConn, localAddr, remoteAddr net.Addr
func (c *conn) resetBuffer() {
c.buffer.Reset()
c.inboundBuffer.Reset()
c.inboundBuffer.Done()
}

func (c *conn) Read(p []byte) (n int, err error) {
Expand Down

0 comments on commit adcbdd2

Please sign in to comment.