1
0
mirror of https://github.com/golang/go synced 2024-09-29 05:14:29 -06:00

update poll.SendFile to return 'handled' value, which is true if SendFile did work or no work was necessary (src returned 0 bytes)

This commit is contained in:
Daulet Zhanguzin 2022-07-06 15:24:48 -07:00
parent 7e137f20db
commit 3a50be4f16
2 changed files with 14 additions and 10 deletions

View File

@ -11,18 +11,21 @@ import "syscall"
const maxSendfileSize int = 4 << 20
// SendFile wraps the sendfile system call.
func SendFile(dstFD *FD, src int, remain int64) (int64, error) {
func SendFile(dstFD *FD, src int, remain int64) (int64, error, bool) {
if err := dstFD.writeLock(); err != nil {
return 0, err
return 0, err, false
}
defer dstFD.writeUnlock()
if err := dstFD.pd.prepareWrite(dstFD.isFile); err != nil {
return 0, err
return 0, err, false
}
dst := dstFD.Sysfd
var written int64
var err error
var (
written int64
err error
handled = true
)
for remain > 0 {
n := maxSendfileSize
if int64(n) > remain {
@ -48,8 +51,9 @@ func SendFile(dstFD *FD, src int, remain int64) (int64, error) {
// support) and syscall.EINVAL (fd types which
// don't implement sendfile)
err = err1
handled = false
break
}
}
return written, err
return written, err, handled
}

View File

@ -13,8 +13,8 @@ import (
// sendFile copies the contents of r to c using the sendfile
// system call to minimize copies.
//
// if handled == true, sendFile returns the number of bytes copied and any
// non-EOF error.
// if handled == true, sendFile returns the number (potentially zero) of bytes
// copied and any non-EOF error.
//
// if handled == false, sendFile performed no work.
func sendFile(c *netFD, r io.Reader) (written int64, err error, handled bool) {
@ -39,7 +39,7 @@ func sendFile(c *netFD, r io.Reader) (written int64, err error, handled bool) {
var werr error
err = sc.Read(func(fd uintptr) bool {
written, werr = poll.SendFile(&c.pfd, int(fd), remain)
written, werr, handled = poll.SendFile(&c.pfd, int(fd), remain)
return true
})
if err == nil {
@ -49,5 +49,5 @@ func sendFile(c *netFD, r io.Reader) (written int64, err error, handled bool) {
if lr != nil {
lr.N = remain - written
}
return written, wrapSyscallError("sendfile", err), err == nil
return written, wrapSyscallError("sendfile", err), handled
}