Chore: adjust Relay copy memory alloc logic

This commit is contained in:
Dreamacro 2022-06-11 20:38:16 +08:00
parent 09d49bac95
commit 06d75da257

View file

@ -4,8 +4,6 @@ import (
"io" "io"
"net" "net"
"time" "time"
"github.com/Dreamacro/clash/common/pool"
) )
// Relay copies between left and right bidirectionally. // Relay copies between left and right bidirectionally.
@ -13,18 +11,14 @@ func Relay(leftConn, rightConn net.Conn) {
ch := make(chan error) ch := make(chan error)
go func() { go func() {
buf := pool.Get(pool.RelayBufferSize)
// Wrapping to avoid using *net.TCPConn.(ReadFrom) // Wrapping to avoid using *net.TCPConn.(ReadFrom)
// See also https://github.com/Dreamacro/clash/pull/1209 // See also https://github.com/Dreamacro/clash/pull/1209
_, err := io.CopyBuffer(WriteOnlyWriter{Writer: leftConn}, ReadOnlyReader{Reader: rightConn}, buf) _, err := io.Copy(WriteOnlyWriter{Writer: leftConn}, ReadOnlyReader{Reader: rightConn})
pool.Put(buf)
leftConn.SetReadDeadline(time.Now()) leftConn.SetReadDeadline(time.Now())
ch <- err ch <- err
}() }()
buf := pool.Get(pool.RelayBufferSize) io.Copy(WriteOnlyWriter{Writer: rightConn}, ReadOnlyReader{Reader: leftConn})
io.CopyBuffer(WriteOnlyWriter{Writer: rightConn}, ReadOnlyReader{Reader: leftConn}, buf)
pool.Put(buf)
rightConn.SetReadDeadline(time.Now()) rightConn.SetReadDeadline(time.Now())
<-ch <-ch
} }