-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbufiopool.go
200 lines (166 loc) · 3.69 KB
/
bufiopool.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package bp
import (
"bufio"
"io"
)
const (
defaultBufioSize int = 4 * 1024
)
type BufioReaderPool struct {
pool chan *bufio.Reader
bufSize int
strict bool
}
func (b *BufioReaderPool) GetRef(r io.Reader) *BufioReaderRef {
data := b.Get(r)
ref := newBufioReaderRef(data, b)
ref.setFinalizer()
return ref
}
func (b *BufioReaderPool) preload(rate float64) {
if 0 < cap(b.pool) {
preloadSize := int(float64(cap(b.pool)) * rate)
for i := 0; i < preloadSize; i += 1 {
b.Put(bufio.NewReaderSize(nil, b.bufSize))
}
}
}
func (b *BufioReaderPool) Get(r io.Reader) *bufio.Reader {
var br *bufio.Reader
select {
case br = <-b.pool:
// reuse exists pool
default:
// create *bufio.Reader
br = bufio.NewReaderSize(nil, b.bufSize)
}
br.Reset(r)
return br
}
func (b *BufioReaderPool) Put(br *bufio.Reader) bool {
br.Reset(nil)
if br.Size() < b.bufSize {
// discard
return false
}
if b.bufSize < br.Size() {
if b.strict {
// discard, same buffer size only
return false
}
}
select {
case b.pool <- br:
// free capacity
return true
default:
// full capacity, discard it
return false
}
}
func (b *BufioReaderPool) Len() int {
return len(b.pool)
}
func (b *BufioReaderPool) Cap() int {
return cap(b.pool)
}
func NewBufioReaderPool(poolSize int, funcs ...optionFunc) *BufioReaderPool {
return newBufioReaderPool(poolSize, defaultBufioSize, false, funcs...)
}
func NewBufioReaderSizePool(poolSize int, bufSize int, funcs ...optionFunc) *BufioReaderPool {
return newBufioReaderPool(poolSize, bufSize, true, funcs...)
}
func newBufioReaderPool(poolSize int, bufSize int, sizeStrict bool, funcs ...optionFunc) *BufioReaderPool {
opt := newOption()
for _, fn := range funcs {
fn(opt)
}
b := &BufioReaderPool{
pool: make(chan *bufio.Reader, poolSize),
bufSize: bufSize,
strict: sizeStrict,
}
if opt.preload {
b.preload(opt.preloadRate)
}
return b
}
type BufioWriterPool struct {
pool chan *bufio.Writer
bufSize int
strict bool
}
func (b *BufioWriterPool) GetRef(w io.Writer) *BufioWriterRef {
data := b.Get(w)
ref := newBufioWriterRef(data, b)
ref.setFinalizer()
return ref
}
func (b *BufioWriterPool) preload(rate float64) {
if 0 < cap(b.pool) {
preloadSize := int(float64(cap(b.pool)) * rate)
for i := 0; i < preloadSize; i += 1 {
b.Put(bufio.NewWriterSize(nil, b.bufSize))
}
}
}
func (b *BufioWriterPool) Get(w io.Writer) *bufio.Writer {
var bw *bufio.Writer
select {
case bw = <-b.pool:
// reuse exists pool
default:
// create *bufio.Writer
bw = bufio.NewWriterSize(nil, b.bufSize)
}
bw.Reset(w)
return bw
}
func (b *BufioWriterPool) Put(bw *bufio.Writer) bool {
bw.Reset(nil)
if bw.Size() < b.bufSize {
// discard
return false
}
if b.bufSize < bw.Size() {
if b.strict {
// discard, same buffer size only
return false
}
}
select {
case b.pool <- bw:
// free capacity
return true
default:
// full capacity, discard it
return false
}
}
func (b *BufioWriterPool) Len() int {
return len(b.pool)
}
func (b *BufioWriterPool) Cap() int {
return cap(b.pool)
}
func NewBufioWriterPool(poolSize int, funcs ...optionFunc) *BufioWriterPool {
return newBufioWriterPool(poolSize, defaultBufioSize, false, funcs...)
}
func NewBufioWriterSizePool(poolSize int, bufSize int, funcs ...optionFunc) *BufioWriterPool {
return newBufioWriterPool(poolSize, bufSize, true, funcs...)
}
func newBufioWriterPool(poolSize int, bufSize int, sizeStrict bool, funcs ...optionFunc) *BufioWriterPool {
opt := newOption()
for _, fn := range funcs {
fn(opt)
}
b := &BufioWriterPool{
pool: make(chan *bufio.Writer, poolSize),
bufSize: bufSize,
strict: sizeStrict,
}
if opt.preload {
b.preload(opt.preloadRate)
}
return b
}