2021-11-13 11:29:08 +00:00
|
|
|
package pools
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"io"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2022-05-08 17:49:45 +00:00
|
|
|
// BufioReaderPool is a pooled allocator for bufio.Reader objects.
|
2021-11-13 11:29:08 +00:00
|
|
|
type BufioReaderPool interface {
|
|
|
|
// Get fetches a bufio.Reader from pool and resets to supplied reader
|
|
|
|
Get(io.Reader) *bufio.Reader
|
|
|
|
|
|
|
|
// Put places supplied bufio.Reader back in pool
|
|
|
|
Put(*bufio.Reader)
|
|
|
|
}
|
|
|
|
|
2022-05-08 17:49:45 +00:00
|
|
|
// NewBufioReaderPool returns a newly instantiated bufio.Reader pool.
|
2021-11-13 11:29:08 +00:00
|
|
|
func NewBufioReaderPool(size int) BufioReaderPool {
|
|
|
|
return &bufioReaderPool{
|
2022-05-08 17:49:45 +00:00
|
|
|
pool: sync.Pool{
|
2021-11-13 11:29:08 +00:00
|
|
|
New: func() interface{} {
|
|
|
|
return bufio.NewReaderSize(nil, size)
|
|
|
|
},
|
|
|
|
},
|
2022-05-08 17:49:45 +00:00
|
|
|
size: size,
|
2021-11-13 11:29:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-08 17:49:45 +00:00
|
|
|
// bufioReaderPool is our implementation of BufioReaderPool.
|
|
|
|
type bufioReaderPool struct {
|
|
|
|
pool sync.Pool
|
|
|
|
size int
|
|
|
|
}
|
2021-11-13 11:29:08 +00:00
|
|
|
|
|
|
|
func (p *bufioReaderPool) Get(r io.Reader) *bufio.Reader {
|
2022-05-08 17:49:45 +00:00
|
|
|
br := p.pool.Get().(*bufio.Reader)
|
2021-11-13 11:29:08 +00:00
|
|
|
br.Reset(r)
|
|
|
|
return br
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *bufioReaderPool) Put(br *bufio.Reader) {
|
2022-05-08 17:49:45 +00:00
|
|
|
if br.Size() < p.size {
|
|
|
|
return
|
|
|
|
}
|
2021-11-13 11:29:08 +00:00
|
|
|
br.Reset(nil)
|
2022-05-08 17:49:45 +00:00
|
|
|
p.pool.Put(br)
|
2021-11-13 11:29:08 +00:00
|
|
|
}
|
|
|
|
|
2022-05-08 17:49:45 +00:00
|
|
|
// BufioWriterPool is a pooled allocator for bufio.Writer objects.
|
2021-11-13 11:29:08 +00:00
|
|
|
type BufioWriterPool interface {
|
|
|
|
// Get fetches a bufio.Writer from pool and resets to supplied writer
|
|
|
|
Get(io.Writer) *bufio.Writer
|
|
|
|
|
|
|
|
// Put places supplied bufio.Writer back in pool
|
|
|
|
Put(*bufio.Writer)
|
|
|
|
}
|
|
|
|
|
2022-05-08 17:49:45 +00:00
|
|
|
// NewBufioWriterPool returns a newly instantiated bufio.Writer pool.
|
2021-11-13 11:29:08 +00:00
|
|
|
func NewBufioWriterPool(size int) BufioWriterPool {
|
|
|
|
return &bufioWriterPool{
|
2022-05-08 17:49:45 +00:00
|
|
|
pool: sync.Pool{
|
2021-11-13 11:29:08 +00:00
|
|
|
New: func() interface{} {
|
|
|
|
return bufio.NewWriterSize(nil, size)
|
|
|
|
},
|
|
|
|
},
|
2022-05-08 17:49:45 +00:00
|
|
|
size: size,
|
2021-11-13 11:29:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-08 17:49:45 +00:00
|
|
|
// bufioWriterPool is our implementation of BufioWriterPool.
|
|
|
|
type bufioWriterPool struct {
|
|
|
|
pool sync.Pool
|
|
|
|
size int
|
|
|
|
}
|
2021-11-13 11:29:08 +00:00
|
|
|
|
|
|
|
func (p *bufioWriterPool) Get(w io.Writer) *bufio.Writer {
|
2022-05-08 17:49:45 +00:00
|
|
|
bw := p.pool.Get().(*bufio.Writer)
|
2021-11-13 11:29:08 +00:00
|
|
|
bw.Reset(w)
|
|
|
|
return bw
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *bufioWriterPool) Put(bw *bufio.Writer) {
|
2022-05-08 17:49:45 +00:00
|
|
|
if bw.Size() < p.size {
|
|
|
|
return
|
|
|
|
}
|
2021-11-13 11:29:08 +00:00
|
|
|
bw.Reset(nil)
|
2022-05-08 17:49:45 +00:00
|
|
|
p.pool.Put(bw)
|
2021-11-13 11:29:08 +00:00
|
|
|
}
|