1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
13 // maxSendfileSize is the largest chunk size we ask the kernel to copy
15 const maxSendfileSize
int = 4 << 20
17 // sendFile copies the contents of r to c using the sendfile
18 // system call to minimize copies.
20 // if handled == true, sendFile returns the number of bytes copied and any
23 // if handled == false, sendFile performed no work.
24 func sendFile(c
*netFD
, r io
.Reader
) (written
int64, err error
, handled
bool) {
25 var remain
int64 = 1 << 62 // by default, copy until EOF
27 lr
, ok
:= r
.(*io
.LimitedReader
)
29 remain
, r
= lr
.N
, lr
.R
39 if err
:= c
.writeLock(); err
!= nil {
48 if int64(n
) > remain
{
51 n
, err1
:= syscall
.Sendfile(dst
, src
, nil, n
)
56 if n
== 0 && err1
== nil {
59 if err1
== syscall
.EAGAIN
{
60 if err1
= c
.pd
.WaitWrite(); err1
== nil {
65 // This includes syscall.ENOSYS (no kernel
66 // support) and syscall.EINVAL (fd types which
67 // don't implement sendfile together)
68 err
= &OpError
{"sendfile", c
.net
, c
.raddr
, err1
}
75 return written
, err
, written
> 0