Skip to content

Commit c706d42

Browse files
nirsbradfitz
authored andcommitted
net/http: add Transport.ReadBufferSize and WriteBufferSize
Previously transport was using the hardcoded bufio.defaultBufSize (4096), limiting throughput and increasing cpu usage when uploading or downloading large files. Add options to allow users to configure the buffer sizes as needed. I tested the maximum benefit of this change by uploading data from /dev/zero to a server discarding the bytes. Here is an example upload using the default buffer size: $ time ./upload 10 https://localhost:8000/ Uploaded 10.00g in 25.13 seconds (407.49m/s) real 0m25.135s user 0m5.167s sys 0m11.643s With this change, using 128k buffer size: $ time ./upload 10 https://localhost:8000/ Uploaded 10.00g in 7.93 seconds (1291.51m/s) real 0m7.935s user 0m4.517s sys 0m2.603s In real world usage the difference will be smaller, depending on the local and remote storage and the network. See https://github.com/nirs/http-bench for more info. Fixes #22618 Change-Id: Iac99ed839c7b95d6dc66602ba8fe1fc5b500c47c Reviewed-on: https://go-review.googlesource.com/c/go/+/76410 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
1 parent 6505b48 commit c706d42

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

src/net/http/transport.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,16 @@ type Transport struct {
254254
// Zero means to use a default limit.
255255
MaxResponseHeaderBytes int64
256256

257+
// WriteBufferSize specifies the size of the write buffer used
258+
// when writing to the transport.
259+
// If zero, a default (currently 4KB) is used.
260+
WriteBufferSize int
261+
262+
// ReadBufferSize specifies the size of the read buffer used
263+
// when reading from the transport.
264+
//If zero, a default (currently 4KB) is used.
265+
ReadBufferSize int
266+
257267
// nextProtoOnce guards initialization of TLSNextProto and
258268
// h2transport (via onceSetNextProtoDefaults)
259269
nextProtoOnce sync.Once
@@ -266,6 +276,20 @@ type Transport struct {
266276
ForceAttemptHTTP2 bool
267277
}
268278

279+
func (t *Transport) writeBufferSize() int {
280+
if t.WriteBufferSize > 0 {
281+
return t.WriteBufferSize
282+
}
283+
return 4 << 10
284+
}
285+
286+
func (t *Transport) readBufferSize() int {
287+
if t.ReadBufferSize > 0 {
288+
return t.ReadBufferSize
289+
}
290+
return 4 << 10
291+
}
292+
269293
// h2Transport is the interface we expect to be able to call from
270294
// net/http against an *http2.Transport that's either bundled into
271295
// h2_bundle.go or supplied by the user via x/net/http2.
@@ -1360,8 +1384,9 @@ func (t *Transport) dialConn(ctx context.Context, cm connectMethod) (*persistCon
13601384
if t.MaxConnsPerHost > 0 {
13611385
pconn.conn = &connCloseListener{Conn: pconn.conn, t: t, cmKey: pconn.cacheKey}
13621386
}
1363-
pconn.br = bufio.NewReader(pconn)
1364-
pconn.bw = bufio.NewWriter(persistConnWriter{pconn})
1387+
pconn.br = bufio.NewReaderSize(pconn, t.readBufferSize())
1388+
pconn.bw = bufio.NewWriterSize(persistConnWriter{pconn}, t.writeBufferSize())
1389+
13651390
go pconn.readLoop()
13661391
go pconn.writeLoop()
13671392
return pconn, nil

0 commit comments

Comments
 (0)