TUN-6035: Reduce buffer size when proxying data

This commit is contained in:
João Oliveirinha
2022-04-11 11:44:42 +01:00
parent 0dc3428424
commit d1a4710aa2
2 changed files with 30 additions and 1 deletions

27
cfio/copy.go Normal file
View File

@@ -0,0 +1,27 @@
package cfio
import (
"io"
"sync"
)
const defaultBufferSize = 16 * 1024
var bufferPool = sync.Pool{
New: func() interface{} {
return make([]byte, defaultBufferSize)
},
}
func Copy(dst io.Writer, src io.Reader) (written int64, err error) {
_, okWriteTo := src.(io.WriterTo)
_, okReadFrom := dst.(io.ReaderFrom)
var buffer []byte = nil
if !(okWriteTo || okReadFrom) {
buffer = bufferPool.Get().([]byte)
defer bufferPool.Put(buffer)
}
return io.CopyBuffer(dst, src, buffer)
}