Files
v2ray_simple/common/pool.go
hahahrfool c450196341 试图完善DetectConn,添加了delay fix 功能,但收效甚微
在这个commit里,完整地发现了因为tls record数据被切割而带来的问题,并且试图修复它。

添加的Sleep方式 实测仍然效果很差
2022-03-12 17:00:13 +08:00

104 lines
1.9 KiB
Go
Raw 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 common
import (
"bytes"
"sync"
)
var (
standardBytesPool sync.Pool //1500
// 实际上tcp默认是 16384, 16k实际上范围是1k128k之间我们64k已经够了
//而 udp则最大还不到 64k。(65535208)
standardPacketPool sync.Pool //64*1024
customBytesPool sync.Pool // >1500
bufPool sync.Pool
)
const StandardBytesLength int = 1500
const maxBufLen int = 64 * 1024
func init() {
standardBytesPool = sync.Pool{
New: func() interface{} {
return make([]byte, StandardBytesLength)
},
}
standardPacketPool = sync.Pool{
New: func() interface{} {
return make([]byte, maxBufLen)
},
}
customBytesPool = sync.Pool{
New: func() interface{} {
return make([]byte, maxBufLen)
},
}
bufPool = sync.Pool{
New: func() interface{} {
return &bytes.Buffer{}
},
}
}
func GetBuf() *bytes.Buffer {
return bufPool.Get().(*bytes.Buffer)
}
func PutBuf(buf *bytes.Buffer) {
buf.Reset()
bufPool.Put(buf)
}
//建议在 Read net.Conn 时, 使用 GetPacket函数 获取到足够大的byte64*1024字节
func GetPacket() []byte {
return standardPacketPool.Get().([]byte)
}
func PutPacket(bs []byte) {
c := cap(bs)
if c < maxBufLen { //如果不够大,考虑放到更小的 pool里
if c > StandardBytesLength {
standardBytesPool.Put(bs[:c])
}
return
}
standardPacketPool.Put(bs[:c])
}
// 从pool中获取 []byte, 在 size <= 1500时有最佳性能
func GetBytes(size int) []byte {
if size < StandardBytesLength {
bs := standardBytesPool.Get().([]byte)
return bs[:size]
}
randomBytes1 := standardBytesPool.Get().([]byte)
if len(randomBytes1) >= size {
return randomBytes1[:size]
} else {
standardBytesPool.Put(randomBytes1)
return make([]byte, size)
}
}
func PutBytes(bs []byte) {
c := cap(bs)
if c < StandardBytesLength {
return
} else if c == StandardBytesLength {
standardBytesPool.Put(bs[:c])
} else {
customBytesPool.Put(bs)
}
}