-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuffer.go
More file actions
67 lines (60 loc) · 1001 Bytes
/
Copy pathbuffer.go
File metadata and controls
67 lines (60 loc) · 1001 Bytes
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
package conduit
type Buffer struct {
input chan Job
output chan Job
grow int
shrink int
cancel chan struct{}
done chan struct{}
buf []Job
}
func NewBuffer(grow, shrink int) (b *Buffer) {
b = &Buffer{
input: make(chan Job),
grow: grow,
shrink: shrink,
cancel: make(chan struct{}),
done: make(chan struct{}),
buf: make([]Job, grow),
}
return
}
func (b *Buffer) main() {
var (
open bool = true
ipt int = 0
opt int = 0
)
for open {
if opt == ipt {
select {
case <-b.cancel:
open = false
case job := <-b.input:
b.buf[ipt] = job
ipt++
}
} else {
select {
case <-b.cancel:
open = false
case job := <-b.input:
b.buf[ipt] = job
ipt++
case b.output <- b.buf[opt]:
opt++
}
}
if ipt == len(b.buf) {
nbf := make([]Job, len(b.buf)+b.grow)
copy(nbf, b.buf)
b.buf = nbf
}
if opt == b.shrink {
b.buf = b.buf[b.shrink:]
opt -= b.shrink
ipt -= b.shrink
}
}
close(b.done)
}