...
1
2
3
4
5 package http2
6
7 import (
8 "errors"
9 "fmt"
10 "sync"
11 )
12
13
14
15
16
17
18
19
20
21
22
23 var dataChunkPools = [...]sync.Pool{
24 {New: func() interface{} { return new([1 << 10]byte) }},
25 {New: func() interface{} { return new([2 << 10]byte) }},
26 {New: func() interface{} { return new([4 << 10]byte) }},
27 {New: func() interface{} { return new([8 << 10]byte) }},
28 {New: func() interface{} { return new([16 << 10]byte) }},
29 }
30
31 func getDataBufferChunk(size int64) []byte {
32 switch {
33 case size <= 1<<10:
34 return dataChunkPools[0].Get().(*[1 << 10]byte)[:]
35 case size <= 2<<10:
36 return dataChunkPools[1].Get().(*[2 << 10]byte)[:]
37 case size <= 4<<10:
38 return dataChunkPools[2].Get().(*[4 << 10]byte)[:]
39 case size <= 8<<10:
40 return dataChunkPools[3].Get().(*[8 << 10]byte)[:]
41 default:
42 return dataChunkPools[4].Get().(*[16 << 10]byte)[:]
43 }
44 }
45
46 func putDataBufferChunk(p []byte) {
47 switch len(p) {
48 case 1 << 10:
49 dataChunkPools[0].Put((*[1 << 10]byte)(p))
50 case 2 << 10:
51 dataChunkPools[1].Put((*[2 << 10]byte)(p))
52 case 4 << 10:
53 dataChunkPools[2].Put((*[4 << 10]byte)(p))
54 case 8 << 10:
55 dataChunkPools[3].Put((*[8 << 10]byte)(p))
56 case 16 << 10:
57 dataChunkPools[4].Put((*[16 << 10]byte)(p))
58 default:
59 panic(fmt.Sprintf("unexpected buffer len=%v", len(p)))
60 }
61 }
62
63
64
65
66
67
68 type dataBuffer struct {
69 chunks [][]byte
70 r int
71 w int
72 size int
73 expected int64
74 }
75
76 var errReadEmpty = errors.New("read from empty dataBuffer")
77
78
79
80 func (b *dataBuffer) Read(p []byte) (int, error) {
81 if b.size == 0 {
82 return 0, errReadEmpty
83 }
84 var ntotal int
85 for len(p) > 0 && b.size > 0 {
86 readFrom := b.bytesFromFirstChunk()
87 n := copy(p, readFrom)
88 p = p[n:]
89 ntotal += n
90 b.r += n
91 b.size -= n
92
93 if b.r == len(b.chunks[0]) {
94 putDataBufferChunk(b.chunks[0])
95 end := len(b.chunks) - 1
96 copy(b.chunks[:end], b.chunks[1:])
97 b.chunks[end] = nil
98 b.chunks = b.chunks[:end]
99 b.r = 0
100 }
101 }
102 return ntotal, nil
103 }
104
105 func (b *dataBuffer) bytesFromFirstChunk() []byte {
106 if len(b.chunks) == 1 {
107 return b.chunks[0][b.r:b.w]
108 }
109 return b.chunks[0][b.r:]
110 }
111
112
113 func (b *dataBuffer) Len() int {
114 return b.size
115 }
116
117
118 func (b *dataBuffer) Write(p []byte) (int, error) {
119 ntotal := len(p)
120 for len(p) > 0 {
121
122
123
124 want := int64(len(p))
125 if b.expected > want {
126 want = b.expected
127 }
128 chunk := b.lastChunkOrAlloc(want)
129 n := copy(chunk[b.w:], p)
130 p = p[n:]
131 b.w += n
132 b.size += n
133 b.expected -= int64(n)
134 }
135 return ntotal, nil
136 }
137
138 func (b *dataBuffer) lastChunkOrAlloc(want int64) []byte {
139 if len(b.chunks) != 0 {
140 last := b.chunks[len(b.chunks)-1]
141 if b.w < len(last) {
142 return last
143 }
144 }
145 chunk := getDataBufferChunk(want)
146 b.chunks = append(b.chunks, chunk)
147 b.w = 0
148 return chunk
149 }
150
View as plain text