113 lines
2.0 KiB
Go
113 lines
2.0 KiB
Go
|
package core
|
||
|
|
||
|
import (
|
||
|
"bufio"
|
||
|
"io"
|
||
|
"sync"
|
||
|
)
|
||
|
|
||
|
var (
|
||
|
connBufferedReaderPool sync.Pool
|
||
|
connBufferedWriterPool sync.Pool
|
||
|
fileBufferedReaderPool sync.Pool
|
||
|
|
||
|
fileReadBufferPool sync.Pool
|
||
|
)
|
||
|
|
||
|
func newConnBufferedReaderPool(size int) sync.Pool {
|
||
|
return sync.Pool{
|
||
|
New: func() interface{} {
|
||
|
return bufio.NewReaderSize(nil, size)
|
||
|
},
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func newConnBufferedWriterPool(size int) sync.Pool {
|
||
|
return sync.Pool{
|
||
|
New: func() interface{} {
|
||
|
return bufio.NewWriterSize(nil, size)
|
||
|
},
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func newFileBufferedReaderPool(size int) sync.Pool {
|
||
|
return sync.Pool{
|
||
|
New: func() interface{} {
|
||
|
return bufio.NewReaderSize(nil, size)
|
||
|
},
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func newFileReadBufferPool(size int) sync.Pool {
|
||
|
return sync.Pool{
|
||
|
New: func() interface{} {
|
||
|
return make([]byte, size)
|
||
|
},
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func getConnBufferedReader(r io.Reader) *bufio.Reader {
|
||
|
// Get buffered reader
|
||
|
br := connBufferedReaderPool.Get().(*bufio.Reader)
|
||
|
|
||
|
// Reset to new reader
|
||
|
br.Reset(r)
|
||
|
|
||
|
// Return!
|
||
|
return br
|
||
|
}
|
||
|
|
||
|
func putConnBufferedReader(br *bufio.Reader) {
|
||
|
// Reset to ensure not hanging onto old reader
|
||
|
br.Reset(nil)
|
||
|
|
||
|
// Put back in pool
|
||
|
connBufferedReaderPool.Put(br)
|
||
|
}
|
||
|
|
||
|
func getConnBufferedWriter(w io.Writer) *bufio.Writer {
|
||
|
// Get buffered writer
|
||
|
bw := connBufferedWriterPool.Get().(*bufio.Writer)
|
||
|
|
||
|
// Reset to new writer
|
||
|
bw.Reset(w)
|
||
|
|
||
|
// Return!
|
||
|
return bw
|
||
|
}
|
||
|
|
||
|
func putConnBufferedWriter(bw *bufio.Writer) {
|
||
|
// Reset to ensure not hanging onto old writer
|
||
|
bw.Reset(nil)
|
||
|
|
||
|
// Put back in pool
|
||
|
connBufferedWriterPool.Put(bw)
|
||
|
}
|
||
|
|
||
|
func getFileBufferedReader(r io.Reader) *bufio.Reader {
|
||
|
// Get buffered reader
|
||
|
br := fileBufferedReaderPool.Get().(*bufio.Reader)
|
||
|
|
||
|
// Reset to new reader
|
||
|
br.Reset(r)
|
||
|
|
||
|
// Return!
|
||
|
return br
|
||
|
}
|
||
|
|
||
|
func putFileBufferedReader(br *bufio.Reader) {
|
||
|
// Reset to ensure not hanging onto old reader
|
||
|
br.Reset(nil)
|
||
|
|
||
|
// Put back in pool
|
||
|
fileBufferedReaderPool.Put(br)
|
||
|
}
|
||
|
|
||
|
func getFileReadBuffer() []byte {
|
||
|
return fileReadBufferPool.Get().([]byte)
|
||
|
}
|
||
|
|
||
|
func putFileReadBuffer(b []byte) {
|
||
|
fileReadBufferPool.Put(b)
|
||
|
}
|