-
Notifications
You must be signed in to change notification settings - Fork 12
/
poolreader.go
53 lines (45 loc) · 983 Bytes
/
poolreader.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
// Copyright 2020 The searKing Author. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package bufio
import (
"bufio"
"io"
"sync"
)
type ReaderPool struct {
pool sync.Pool
size int
}
func NewReaderPool() *ReaderPool {
return &ReaderPool{}
}
func NewReaderPoolSize(size int) *ReaderPool {
return &ReaderPool{
size: size,
}
}
func (p *ReaderPool) Put(br *bufio.Reader) {
br.Reset(nil)
p.pool.Put(br)
}
func (p *ReaderPool) Get(r io.Reader) *bufio.Reader {
if v := p.pool.Get(); v != nil {
br := v.(*bufio.Reader)
br.Reset(r)
return br
}
// Note: if this reader size is ever changed, update
// TestHandlerBodyClose's assumptions.
return bufio.NewReaderSize(r, p.size)
}
func (p *ReaderPool) Clear() {
// Get One elem in the pool
// for p.pool.New is nil, so p.pool.New will return nil if empty
if p.pool.Get() == nil {
// The pool is empty
return
}
p.Clear()
return
}