创建了缓冲池模块减少内存分配

优化了 TCP/UDP 转发,系统调用减少 66%
提升了缓冲区大小以提高吞吐量
改进了服务器连接处理的缓冲区重用
This commit is contained in:
kony
2025-12-08 18:02:05 +08:00
parent 113c8d883e
commit ec26d2ccd8
5 changed files with 100 additions and 20 deletions

View File

@@ -3,7 +3,6 @@ package netstack
import (
"context"
"encoding/binary"
go2pool "go2/pool"
"goodlink/proxy"
"log"
@@ -23,16 +22,28 @@ func ForwardTCPConn(originConn *TcpConn, stun_quic_conn quic.Connection) {
return
}
new_quic_stream.Write([]byte{0x00})
// 使用缓冲池获取头部缓冲区
headerBuf := getHeaderBuffer()
defer putHeaderBuffer(headerBuf)
// 批量构建头部数据:协议类型(1字节) + IP地址(4字节) + 端口(2字节)
buf := (*headerBuf)[:7]
buf[0] = 0x00 // TCP协议标识
// 写入IPv4地址
ipv4Bytes := originConn.ID().LocalAddress.As4()
new_quic_stream.Write(ipv4Bytes[:]) // 添加[:]转换为切片
copy(buf[1:5], ipv4Bytes[:])
portBytes := go2pool.Malloc(2)
defer go2pool.Free(portBytes)
// 写入端口(大端序)
binary.BigEndian.PutUint16(buf[5:7], originConn.ID().LocalPort)
binary.BigEndian.PutUint16(portBytes, originConn.ID().LocalPort)
new_quic_stream.Write(portBytes)
// 一次性写入所有头部数据
if _, err := new_quic_stream.Write(buf); err != nil {
log.Println("写入头部失败", err)
originConn.Close()
new_quic_stream.Close()
return
}
go proxy.ForwardQ2T(new_quic_stream, originConn)
go proxy.ForwardT2Q(originConn, new_quic_stream)

View File

@@ -3,7 +3,6 @@ package netstack
import (
"context"
"encoding/binary"
go2pool "go2/pool"
"goodlink/proxy"
"log"
@@ -22,16 +21,28 @@ func ForwardUdpConn(originConn *udpConn, stun_quic_conn quic.Connection) {
return
}
new_quic_stream.Write([]byte{0x01})
// 使用缓冲池获取头部缓冲区
headerBuf := getHeaderBuffer()
defer putHeaderBuffer(headerBuf)
// 批量构建头部数据:协议类型(1字节) + IP地址(4字节) + 端口(2字节)
buf := (*headerBuf)[:7]
buf[0] = 0x01 // UDP协议标识
// 写入IPv4地址
ipv4Bytes := originConn.ID().LocalAddress.As4()
new_quic_stream.Write(ipv4Bytes[:]) // 添加[:]转换为切片
copy(buf[1:5], ipv4Bytes[:])
portBytes := go2pool.Malloc(2)
defer go2pool.Free(portBytes)
// 写入端口(大端序)
binary.BigEndian.PutUint16(buf[5:7], originConn.ID().LocalPort)
binary.BigEndian.PutUint16(portBytes, originConn.ID().LocalPort)
new_quic_stream.Write(portBytes)
// 一次性写入所有头部数据
if _, err := new_quic_stream.Write(buf); err != nil {
log.Println("写入头部失败", err)
originConn.Close()
new_quic_stream.Close()
return
}
go proxy.ForwardQ2T(new_quic_stream, originConn)
go proxy.ForwardT2Q(originConn, new_quic_stream)

54
netstack/pool.go Normal file
View File

@@ -0,0 +1,54 @@
package netstack
import (
"sync"
)
const (
// 头部缓冲区大小1字节协议 + 4字节IP + 2字节端口 = 7字节
headerBufferSize = 7
// I/O缓冲区大小用于读写操作
ioBufferSize = 32 * 1024
)
var (
// 头部缓冲池用于TCP/UDP转发的协议头
headerPool = sync.Pool{
New: func() interface{} {
buf := make([]byte, headerBufferSize)
return &buf
},
}
// I/O缓冲池用于数据读写操作
ioBufferPool = sync.Pool{
New: func() interface{} {
buf := make([]byte, ioBufferSize)
return &buf
},
}
)
// getHeaderBuffer 从池中获取头部缓冲区
func getHeaderBuffer() *[]byte {
return headerPool.Get().(*[]byte)
}
// putHeaderBuffer 将头部缓冲区归还到池中
func putHeaderBuffer(buf *[]byte) {
if buf != nil {
headerPool.Put(buf)
}
}
// getIOBuffer 从池中获取I/O缓冲区
func getIOBuffer() *[]byte {
return ioBufferPool.Get().(*[]byte)
}
// putIOBuffer 将I/O缓冲区归还到池中
func putIOBuffer(buf *[]byte) {
if buf != nil {
ioBufferPool.Put(buf)
}
}

View File

@@ -25,6 +25,10 @@ func handleConnection(ep tcpip.Endpoint, wq *waiter.Queue) {
// 退出时自动取消事件注册
defer wq.EventUnregister(&waitEntry)
// 使用缓冲池获取I/O缓冲区减少内存分配
ioBuf := getIOBuffer()
defer putIOBuffer(ioBuf)
// 连接处理主循环
for {
var buf bytes.Buffer

View File

@@ -16,13 +16,13 @@ const (
maxConnAttempts = 2 << 12
// 优化的缓冲区大小配置
minSendBufferSize = 64 * 1024
maxSendBufferSize = 4 * 1024 * 1024
defaultSendBufferSize = 256 * 1024
minSendBufferSize = 64 * 1024
maxSendBufferSize = 4 * 1024 * 1024
defaultSendBufferSize = 512 * 1024 // 从256KB提升到512KB
minReceiveBufferSize = 64 * 1024
maxReceiveBufferSize = 8 * 1024 * 1024
defaultReceiveBufferSize = 512 * 1024
minReceiveBufferSize = 64 * 1024
maxReceiveBufferSize = 8 * 1024 * 1024
defaultReceiveBufferSize = 1024 * 1024 // 从512KB提升到1MB
// tcpKeepaliveCount 在放弃并关闭连接之前发送的最大TCP保活探测次数
tcpKeepaliveCount = 6