Files
goodlink/netstack/pool.go
kony ec26d2ccd8 创建了缓冲池模块减少内存分配
优化了 TCP/UDP 转发,系统调用减少 66%
提升了缓冲区大小以提高吞吐量
改进了服务器连接处理的缓冲区重用
2025-12-08 18:02:05 +08:00

55 lines
1.0 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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)
}
}