1 // Copyright 2014 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 // Buffer chunks are allocated from a pool to reduce pressure on GC.
14 // The maximum wasted space per dataBuffer is 2x the largest size class,
15 // which happens when the dataBuffer has multiple chunks and there is
16 // one unread byte in both the first and last chunks. We use a few size
17 // classes to minimize overheads for servers that typically receive very
18 // small request bodies.
20 // TODO: Benchmark to determine if the pools are necessary. The GC may have
21 // improved enough that we can instead allocate chunks like this:
22 // make([]byte, max(16<<10, expectedBytesRemaining))
24 dataChunkSizeClasses = []int{
31 dataChunkPools = [...]sync.Pool{
32 {New: func() interface{} { return make([]byte, 1<<10) }},
33 {New: func() interface{} { return make([]byte, 2<<10) }},
34 {New: func() interface{} { return make([]byte, 4<<10) }},
35 {New: func() interface{} { return make([]byte, 8<<10) }},
36 {New: func() interface{} { return make([]byte, 16<<10) }},
40 func getDataBufferChunk(size int64) []byte {
42 for ; i < len(dataChunkSizeClasses)-1; i++ {
43 if size <= int64(dataChunkSizeClasses[i]) {
47 return dataChunkPools[i].Get().([]byte)
50 func putDataBufferChunk(p []byte) {
51 for i, n := range dataChunkSizeClasses {
53 dataChunkPools[i].Put(p)
57 panic(fmt.Sprintf("unexpected buffer len=%v", len(p)))
60 // dataBuffer is an io.ReadWriter backed by a list of data chunks.
61 // Each dataBuffer is used to read DATA frames on a single stream.
62 // The buffer is divided into chunks so the server can limit the
63 // total memory used by a single connection without limiting the
64 // request body size on any single stream.
65 type dataBuffer struct {
67 r int // next byte to read is chunks[0][r]
68 w int // next byte to write is chunks[len(chunks)-1][w]
69 size int // total buffered bytes
70 expected int64 // we expect at least this many bytes in future Write calls (ignored if <= 0)
73 var errReadEmpty = errors.New("read from empty dataBuffer")
75 // Read copies bytes from the buffer into p.
76 // It is an error to read when no data is available.
77 func (b *dataBuffer) Read(p []byte) (int, error) {
79 return 0, errReadEmpty
82 for len(p) > 0 && b.size > 0 {
83 readFrom := b.bytesFromFirstChunk()
84 n := copy(p, readFrom)
89 // If the first chunk has been consumed, advance to the next chunk.
90 if b.r == len(b.chunks[0]) {
91 putDataBufferChunk(b.chunks[0])
92 end := len(b.chunks) - 1
93 copy(b.chunks[:end], b.chunks[1:])
95 b.chunks = b.chunks[:end]
102 func (b *dataBuffer) bytesFromFirstChunk() []byte {
103 if len(b.chunks) == 1 {
104 return b.chunks[0][b.r:b.w]
106 return b.chunks[0][b.r:]
109 // Len returns the number of bytes of the unread portion of the buffer.
110 func (b *dataBuffer) Len() int {
114 // Write appends p to the buffer.
115 func (b *dataBuffer) Write(p []byte) (int, error) {
118 // If the last chunk is empty, allocate a new chunk. Try to allocate
119 // enough to fully copy p plus any additional bytes we expect to
120 // receive. However, this may allocate less than len(p).
121 want := int64(len(p))
122 if b.expected > want {
125 chunk := b.lastChunkOrAlloc(want)
126 n := copy(chunk[b.w:], p)
130 b.expected -= int64(n)
135 func (b *dataBuffer) lastChunkOrAlloc(want int64) []byte {
136 if len(b.chunks) != 0 {
137 last := b.chunks[len(b.chunks)-1]
142 chunk := getDataBufferChunk(want)
143 b.chunks = append(b.chunks, chunk)