Files
v2ray_simple/utils/readv_posix.go
hahahrfool f8ef685bdd 解决readv内存泄漏问题;解决转发时断连后的悬垂链接问题
在四点链接的情况下,我们只终端中间两点是不够的,要切三刀;
总之实践很简单,就是copy完成之后,要Close所有的链接

readv的话,系统readv数组和buffer不要在put进pool后相互引用

添加-bl 选项,可以自定义buf大小;注意越小可能越慢,建议buf大小保持在4k以上

添加-pp选项,可以生成cpu.pprof文件

修复其它小问题.
2022-03-31 01:32:58 +08:00

56 lines
1.2 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.

//go:build !windows && !wasm && !illumos
// +build !windows,!wasm,!illumos
package utils
import (
"syscall"
"unsafe"
)
func GetReadVReader() SystemReadver {
return &posixReader{}
}
type posixReader struct {
iovecs []syscall.Iovec
}
func (r *posixReader) Init(bs [][]byte, singleBufLen int) {
iovecs := r.iovecs
if iovecs == nil {
iovecs = make([]syscall.Iovec, 0, len(bs))
}
for idx, b := range bs {
iovecs = append(iovecs, syscall.Iovec{
Base: &(b[0]),
})
iovecs[idx].SetLen(singleBufLen)
}
r.iovecs = iovecs
}
//正常readv返回的应该是 ssize_t, 在64位机器上应该是 int64, 但是负数只用于返回-1错误而且我们提供的buffer长度远远小于 uint32的上限所以uint32可以
func (r *posixReader) Read(fd uintptr) (uint32, error) {
n, _, e := syscall.Syscall(syscall.SYS_READV, fd, uintptr(unsafe.Pointer(&r.iovecs[0])), uintptr(len(r.iovecs)))
if e != 0 {
return 0, e
}
return uint32(n), nil
}
func (r *posixReader) Clear() {
for idx := range r.iovecs {
r.iovecs[idx].Base = nil
}
r.iovecs = r.iovecs[:0]
}
func (r *posixReader) Recover(bsLen int, bs [][]byte) {
r.iovecs = r.iovecs[:len(bs)]
for idx := range r.iovecs {
r.iovecs[idx].Base = &(bs[idx][0])
}
}