Skip to content

Commit d1a4710

Browse files
committed
TUN-6035: Reduce buffer size when proxying data
1 parent 0dc3428 commit d1a4710

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

cfio/copy.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package cfio
2+
3+
import (
4+
"io"
5+
"sync"
6+
)
7+
8+
const defaultBufferSize = 16 * 1024
9+
10+
var bufferPool = sync.Pool{
11+
New: func() interface{} {
12+
return make([]byte, defaultBufferSize)
13+
},
14+
}
15+
16+
func Copy(dst io.Writer, src io.Reader) (written int64, err error) {
17+
_, okWriteTo := src.(io.WriterTo)
18+
_, okReadFrom := dst.(io.ReaderFrom)
19+
var buffer []byte = nil
20+
21+
if !(okWriteTo || okReadFrom) {
22+
buffer = bufferPool.Get().([]byte)
23+
defer bufferPool.Put(buffer)
24+
}
25+
26+
return io.CopyBuffer(dst, src, buffer)
27+
}

websocket/websocket.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
"github.com/getsentry/raven-go"
1616
"github.com/gorilla/websocket"
1717
"github.com/rs/zerolog"
18+
19+
"github.com/cloudflare/cloudflared/cfio"
1820
)
1921

2022
// IsWebSocketUpgrade checks to see if the request is a WebSocket connection.
@@ -146,7 +148,7 @@ func copyData(dst io.Writer, src io.Reader, dir string) (written int64, err erro
146148
}
147149
return copyBuffer(dst, src, dir)
148150
} else {
149-
return io.Copy(dst, src)
151+
return cfio.Copy(dst, src)
150152
}
151153
}
152154

0 commit comments

Comments
 (0)