-
Notifications
You must be signed in to change notification settings - Fork 24
/
util.go
179 lines (163 loc) · 4.2 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package yamux
import (
"fmt"
"io"
"sync"
pool "github.com/libp2p/go-buffer-pool"
)
// asyncSendErr is used to try an async send of an error
func asyncSendErr(ch chan error, err error) {
if ch == nil {
return
}
select {
case ch <- err:
default:
}
}
// asyncNotify is used to signal a waiting goroutine
func asyncNotify(ch chan struct{}) {
select {
case ch <- struct{}{}:
default:
}
}
// min computes the minimum of a set of values
func min(values ...uint32) uint32 {
m := values[0]
for _, v := range values[1:] {
if v < m {
m = v
}
}
return m
}
// The segmented buffer looks like:
//
// | data | empty space |
// < window (10) >
// < len (5) > < cap (5) >
//
// As data is read, the buffer gets updated like so:
//
// | data | empty space |
// < window (8) >
// < len (3) > < cap (5) >
//
// It can then grow as follows (given a "max" of 10):
//
// | data | empty space |
// < window (10) >
// < len (3) > < cap (7) >
//
// Data can then be written into the empty space, expanding len,
// and shrinking cap:
//
// | data | empty space |
// < window (10) >
// < len (5) > < cap (5) >
type segmentedBuffer struct {
cap uint32
len uint32
bm sync.Mutex
// read position in b[bPos].
// We must not reslice any of the buffers in b, as we need to put them back into the pool.
readPos int
// bPos is an index in b slice. If bPos == len(b), it means that buffer is empty.
bPos int
// b is used as a growable buffer. Each Append adds []byte to the end of b.
// If there is no space available at the end of the buffer (len(b) == cap(b)), but it has space
// at the beginning (bPos > 0 and at least 1/4 of the buffer is empty), data inside b is shifted to the beginning.
// Each Read reads from b[bPos] and increments bPos if b[bPos] was fully read.
b [][]byte
}
// NewSegmentedBuffer allocates a ring buffer.
func newSegmentedBuffer(initialCapacity uint32) segmentedBuffer {
return segmentedBuffer{cap: initialCapacity, b: make([][]byte, 0, 16)}
}
// Len is the amount of data in the receive buffer.
func (s *segmentedBuffer) Len() uint32 {
s.bm.Lock()
defer s.bm.Unlock()
return s.len
}
// If the space to write into + current buffer size has grown to half of the window size,
// grow up to that max size, and indicate how much additional space was reserved.
func (s *segmentedBuffer) GrowTo(max uint32, force bool) (bool, uint32) {
s.bm.Lock()
defer s.bm.Unlock()
currentWindow := s.cap + s.len
if currentWindow >= max {
return force, 0
}
delta := max - currentWindow
if delta < (max/2) && !force {
return false, 0
}
s.cap += delta
return true, delta
}
func (s *segmentedBuffer) Read(b []byte) (int, error) {
s.bm.Lock()
defer s.bm.Unlock()
if s.bPos == len(s.b) {
return 0, io.EOF
}
data := s.b[s.bPos][s.readPos:]
n := copy(b, data)
if n == len(data) {
pool.Put(s.b[s.bPos])
s.b[s.bPos] = nil
s.bPos++
s.readPos = 0
} else {
s.readPos += n
}
if n > 0 {
s.len -= uint32(n)
}
return n, nil
}
func (s *segmentedBuffer) checkOverflow(l uint32) error {
s.bm.Lock()
defer s.bm.Unlock()
if s.cap < l {
return fmt.Errorf("receive window exceeded (remain: %d, recv: %d)", s.cap, l)
}
return nil
}
func (s *segmentedBuffer) Append(input io.Reader, length uint32) error {
if err := s.checkOverflow(length); err != nil {
return err
}
dst := pool.Get(int(length))
n, err := io.ReadFull(input, dst)
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
s.bm.Lock()
defer s.bm.Unlock()
if n > 0 {
s.len += uint32(n)
s.cap -= uint32(n)
// s.b has no available space at the end, but has space at the beginning
if len(s.b) == cap(s.b) && s.bPos > 0 {
if s.bPos == len(s.b) {
// the buffer is empty, so just move pos
s.bPos = 0
s.b = s.b[:0]
} else if s.bPos > cap(s.b)/4 {
// at least 1/4 of buffer is empty, so shift data to the left to free space at the end
copied := copy(s.b, s.b[s.bPos:])
// clear references to copied data
for i := copied; i < len(s.b); i++ {
s.b[i] = nil
}
s.b = s.b[:copied]
s.bPos = 0
}
}
s.b = append(s.b, dst[0:n])
}
return err
}