Skip to content

Commit

Permalink
Chore: adjust all udp alloc size
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamacro committed Nov 3, 2021
1 parent ebbc960 commit bcb301b
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 3 deletions.
5 changes: 5 additions & 0 deletions common/pool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ const (
// but the maximum packet size of vmess/shadowsocks is about 16 KiB
// so define a buffer of 20 KiB to reduce the memory of each TCP relay
RelayBufferSize = 20 * 1024

// RelayBufferSize uses 20KiB, but due to the allocator it will actually
// request 32Kib. Most UDPs are smaller than the MTU, and the TUN's MTU
// set to 9000, so the UDP Buffer size set to 16Kib
UDPBufferSize = 16 * 1024
)

func Get(size int) []byte {
Expand Down
2 changes: 1 addition & 1 deletion listener/socks/udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func NewUDP(addr string, in chan<- *inbound.PacketAdapter) (*UDPListener, error)
}
go func() {
for {
buf := pool.Get(pool.RelayBufferSize)
buf := pool.Get(pool.UDPBufferSize)
n, remoteAddr, err := l.ReadFrom(buf)
if err != nil {
pool.Put(buf)
Expand Down
2 changes: 1 addition & 1 deletion listener/tproxy/udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func NewUDP(addr string, in chan<- *inbound.PacketAdapter) (*UDPListener, error)
go func() {
oob := make([]byte, 1024)
for {
buf := pool.Get(pool.RelayBufferSize)
buf := pool.Get(pool.UDPBufferSize)
n, oobn, _, lAddr, err := c.ReadMsgUDP(buf, oob)
if err != nil {
pool.Put(buf)
Expand Down
2 changes: 1 addition & 1 deletion tunnel/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func handleUDPToRemote(packet C.UDPPacket, pc C.PacketConn, metadata *C.Metadata
}

func handleUDPToLocal(packet C.UDPPacket, pc net.PacketConn, key string, fAddr net.Addr) {
buf := pool.Get(pool.RelayBufferSize)
buf := pool.Get(pool.UDPBufferSize)
defer pool.Put(buf)
defer natTable.Delete(key)
defer pc.Close()
Expand Down

0 comments on commit bcb301b

Please sign in to comment.