Skip to content

Commit

Permalink
opt: put the bytes of net.Addr.Zone back to pool when connection is c…
Browse files Browse the repository at this point in the history
…losed

Fixes #364
  • Loading branch information
panjf2000 committed Apr 26, 2022
1 parent 8d921f9 commit 50406b3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
13 changes: 13 additions & 0 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
gio "github.com/panjf2000/gnet/v2/internal/io"
"github.com/panjf2000/gnet/v2/internal/netpoll"
"github.com/panjf2000/gnet/v2/internal/socket"
"github.com/panjf2000/gnet/v2/internal/toolkit"
"github.com/panjf2000/gnet/v2/pkg/buffer/elastic"
gerrors "github.com/panjf2000/gnet/v2/pkg/errors"
bsPool "github.com/panjf2000/gnet/v2/pkg/pool/byteslice"
Expand Down Expand Up @@ -70,9 +71,15 @@ func (c *conn) releaseTCP() {
c.buffer = nil
if addr, ok := c.localAddr.(*net.TCPAddr); ok && c.localAddr != c.loop.ln.addr {
bsPool.Put(addr.IP)
if len(addr.Zone) > 0 {
bsPool.Put(toolkit.StringToBytes(addr.Zone))
}
}
if addr, ok := c.remoteAddr.(*net.TCPAddr); ok {
bsPool.Put(addr.IP)
if len(addr.Zone) > 0 {
bsPool.Put(toolkit.StringToBytes(addr.Zone))
}
}
c.localAddr = nil
c.remoteAddr = nil
Expand Down Expand Up @@ -101,9 +108,15 @@ func (c *conn) releaseUDP() {
c.ctx = nil
if addr, ok := c.localAddr.(*net.UDPAddr); ok && c.localAddr != c.loop.ln.addr {
bsPool.Put(addr.IP)
if len(addr.Zone) > 0 {
bsPool.Put(toolkit.StringToBytes(addr.Zone))
}
}
if addr, ok := c.remoteAddr.(*net.UDPAddr); ok {
bsPool.Put(addr.IP)
if len(addr.Zone) > 0 {
bsPool.Put(toolkit.StringToBytes(addr.Zone))
}
}
c.localAddr = nil
c.remoteAddr = nil
Expand Down
3 changes: 2 additions & 1 deletion internal/socket/socktoaddr.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"golang.org/x/sys/unix"

"github.com/panjf2000/gnet/v2/internal/toolkit"
bsPool "github.com/panjf2000/gnet/v2/pkg/pool/byteslice"
)

Expand Down Expand Up @@ -100,5 +101,5 @@ func int2decimal(i uint) string {
bp--
b[bp] = byte(i%10) + '0'
}
return string(b[bp:])
return toolkit.BytesToString(b[bp:])
}

0 comments on commit 50406b3

Please sign in to comment.