Fix splice losing data

This commit is contained in:
wwqgtxx 2023-04-25 22:59:13 +08:00
parent bf0aaacc67
commit 0c037cb0e2

View file

@ -24,6 +24,7 @@ func splice(source syscall.RawConn, destination syscall.RawConn, readCounters []
_, _ = unix.FcntlInt(uintptr(pipeFDs[0]), unix.F_SETPIPE_SZ, maxSpliceSize) _, _ = unix.FcntlInt(uintptr(pipeFDs[0]), unix.F_SETPIPE_SZ, maxSpliceSize)
var readN int var readN int
var readErr error var readErr error
var writeSize int
var writeErr error var writeErr error
readFunc := func(fd uintptr) (done bool) { readFunc := func(fd uintptr) (done bool) {
p0, p1 := unix.Splice(int(fd), nil, pipeFDs[1], nil, maxSpliceSize, unix.SPLICE_F_NONBLOCK) p0, p1 := unix.Splice(int(fd), nil, pipeFDs[1], nil, maxSpliceSize, unix.SPLICE_F_NONBLOCK)
@ -32,16 +33,14 @@ func splice(source syscall.RawConn, destination syscall.RawConn, readCounters []
return readErr != unix.EAGAIN return readErr != unix.EAGAIN
} }
writeFunc := func(fd uintptr) (done bool) { writeFunc := func(fd uintptr) (done bool) {
var writeN int for writeSize > 0 {
size := readN p0, p1 := unix.Splice(pipeFDs[0], nil, int(fd), nil, writeSize, unix.SPLICE_F_NONBLOCK|unix.SPLICE_F_MOVE)
for size > 0 { writeN := int(p0)
p0, p1 := unix.Splice(pipeFDs[0], nil, int(fd), nil, size, unix.SPLICE_F_NONBLOCK|unix.SPLICE_F_MOVE)
writeN = int(p0)
writeErr = p1 writeErr = p1
if writeErr != nil { if writeErr != nil {
return writeErr != unix.EAGAIN return writeErr != unix.EAGAIN
} }
size -= int(writeN) writeSize -= writeN
} }
return true return true
} }
@ -61,6 +60,7 @@ func splice(source syscall.RawConn, destination syscall.RawConn, readCounters []
if readN == 0 { if readN == 0 {
return return
} }
writeSize = readN
err = destination.Write(writeFunc) err = destination.Write(writeFunc)
if err != nil { if err != nil {
writeErr = err writeErr = err