Skip to content

Commit bcb301b

Browse files
committed
Chore: adjust all udp alloc size
1 parent ebbc960 commit bcb301b

File tree

4 files changed

+8
-3
lines changed

4 files changed

+8
-3
lines changed

common/pool/pool.go

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ const (
55
// but the maximum packet size of vmess/shadowsocks is about 16 KiB
66
// so define a buffer of 20 KiB to reduce the memory of each TCP relay
77
RelayBufferSize = 20 * 1024
8+
9+
// RelayBufferSize uses 20KiB, but due to the allocator it will actually
10+
// request 32Kib. Most UDPs are smaller than the MTU, and the TUN's MTU
11+
// set to 9000, so the UDP Buffer size set to 16Kib
12+
UDPBufferSize = 16 * 1024
813
)
914

1015
func Get(size int) []byte {

listener/socks/udp.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func NewUDP(addr string, in chan<- *inbound.PacketAdapter) (*UDPListener, error)
4949
}
5050
go func() {
5151
for {
52-
buf := pool.Get(pool.RelayBufferSize)
52+
buf := pool.Get(pool.UDPBufferSize)
5353
n, remoteAddr, err := l.ReadFrom(buf)
5454
if err != nil {
5555
pool.Put(buf)

listener/tproxy/udp.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func NewUDP(addr string, in chan<- *inbound.PacketAdapter) (*UDPListener, error)
5757
go func() {
5858
oob := make([]byte, 1024)
5959
for {
60-
buf := pool.Get(pool.RelayBufferSize)
60+
buf := pool.Get(pool.UDPBufferSize)
6161
n, oobn, _, lAddr, err := c.ReadMsgUDP(buf, oob)
6262
if err != nil {
6363
pool.Put(buf)

tunnel/connection.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func handleUDPToRemote(packet C.UDPPacket, pc C.PacketConn, metadata *C.Metadata
3939
}
4040

4141
func handleUDPToLocal(packet C.UDPPacket, pc net.PacketConn, key string, fAddr net.Addr) {
42-
buf := pool.Get(pool.RelayBufferSize)
42+
buf := pool.Get(pool.UDPBufferSize)
4343
defer pool.Put(buf)
4444
defer natTable.Delete(key)
4545
defer pc.Close()

0 commit comments

Comments
 (0)