Initial commit
This commit is contained in:
119
vendor/github.com/hyperhq/hypercli/pkg/pools/pools.go
generated
vendored
Normal file
119
vendor/github.com/hyperhq/hypercli/pkg/pools/pools.go
generated
vendored
Normal file
@@ -0,0 +1,119 @@
|
||||
// Package pools provides a collection of pools which provide various
|
||||
// data types with buffers. These can be used to lower the number of
|
||||
// memory allocations and reuse buffers.
|
||||
//
|
||||
// New pools should be added to this package to allow them to be
|
||||
// shared across packages.
|
||||
//
|
||||
// Utility functions which operate on pools should be added to this
|
||||
// package to allow them to be reused.
|
||||
package pools
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io"
|
||||
"sync"
|
||||
|
||||
"github.com/hyperhq/hypercli/pkg/ioutils"
|
||||
)
|
||||
|
||||
var (
|
||||
// BufioReader32KPool is a pool which returns bufio.Reader with a 32K buffer.
|
||||
BufioReader32KPool *BufioReaderPool
|
||||
// BufioWriter32KPool is a pool which returns bufio.Writer with a 32K buffer.
|
||||
BufioWriter32KPool *BufioWriterPool
|
||||
)
|
||||
|
||||
const buffer32K = 32 * 1024
|
||||
|
||||
// BufioReaderPool is a bufio reader that uses sync.Pool.
|
||||
type BufioReaderPool struct {
|
||||
pool sync.Pool
|
||||
}
|
||||
|
||||
func init() {
|
||||
BufioReader32KPool = newBufioReaderPoolWithSize(buffer32K)
|
||||
BufioWriter32KPool = newBufioWriterPoolWithSize(buffer32K)
|
||||
}
|
||||
|
||||
// newBufioReaderPoolWithSize is unexported because new pools should be
|
||||
// added here to be shared where required.
|
||||
func newBufioReaderPoolWithSize(size int) *BufioReaderPool {
|
||||
pool := sync.Pool{
|
||||
New: func() interface{} { return bufio.NewReaderSize(nil, size) },
|
||||
}
|
||||
return &BufioReaderPool{pool: pool}
|
||||
}
|
||||
|
||||
// Get returns a bufio.Reader which reads from r. The buffer size is that of the pool.
|
||||
func (bufPool *BufioReaderPool) Get(r io.Reader) *bufio.Reader {
|
||||
buf := bufPool.pool.Get().(*bufio.Reader)
|
||||
buf.Reset(r)
|
||||
return buf
|
||||
}
|
||||
|
||||
// Put puts the bufio.Reader back into the pool.
|
||||
func (bufPool *BufioReaderPool) Put(b *bufio.Reader) {
|
||||
b.Reset(nil)
|
||||
bufPool.pool.Put(b)
|
||||
}
|
||||
|
||||
// Copy is a convenience wrapper which uses a buffer to avoid allocation in io.Copy.
|
||||
func Copy(dst io.Writer, src io.Reader) (written int64, err error) {
|
||||
buf := BufioReader32KPool.Get(src)
|
||||
written, err = io.Copy(dst, buf)
|
||||
BufioReader32KPool.Put(buf)
|
||||
return
|
||||
}
|
||||
|
||||
// NewReadCloserWrapper returns a wrapper which puts the bufio.Reader back
|
||||
// into the pool and closes the reader if it's an io.ReadCloser.
|
||||
func (bufPool *BufioReaderPool) NewReadCloserWrapper(buf *bufio.Reader, r io.Reader) io.ReadCloser {
|
||||
return ioutils.NewReadCloserWrapper(r, func() error {
|
||||
if readCloser, ok := r.(io.ReadCloser); ok {
|
||||
readCloser.Close()
|
||||
}
|
||||
bufPool.Put(buf)
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// BufioWriterPool is a bufio writer that uses sync.Pool.
|
||||
type BufioWriterPool struct {
|
||||
pool sync.Pool
|
||||
}
|
||||
|
||||
// newBufioWriterPoolWithSize is unexported because new pools should be
|
||||
// added here to be shared where required.
|
||||
func newBufioWriterPoolWithSize(size int) *BufioWriterPool {
|
||||
pool := sync.Pool{
|
||||
New: func() interface{} { return bufio.NewWriterSize(nil, size) },
|
||||
}
|
||||
return &BufioWriterPool{pool: pool}
|
||||
}
|
||||
|
||||
// Get returns a bufio.Writer which writes to w. The buffer size is that of the pool.
|
||||
func (bufPool *BufioWriterPool) Get(w io.Writer) *bufio.Writer {
|
||||
buf := bufPool.pool.Get().(*bufio.Writer)
|
||||
buf.Reset(w)
|
||||
return buf
|
||||
}
|
||||
|
||||
// Put puts the bufio.Writer back into the pool.
|
||||
func (bufPool *BufioWriterPool) Put(b *bufio.Writer) {
|
||||
b.Reset(nil)
|
||||
bufPool.pool.Put(b)
|
||||
}
|
||||
|
||||
// NewWriteCloserWrapper returns a wrapper which puts the bufio.Writer back
|
||||
// into the pool and closes the writer if it's an io.Writecloser.
|
||||
func (bufPool *BufioWriterPool) NewWriteCloserWrapper(buf *bufio.Writer, w io.Writer) io.WriteCloser {
|
||||
return ioutils.NewWriteCloserWrapper(w, func() error {
|
||||
buf.Flush()
|
||||
if writeCloser, ok := w.(io.WriteCloser); ok {
|
||||
writeCloser.Close()
|
||||
}
|
||||
bufPool.Put(buf)
|
||||
return nil
|
||||
})
|
||||
}
|
||||
162
vendor/github.com/hyperhq/hypercli/pkg/pools/pools_test.go
generated
vendored
Normal file
162
vendor/github.com/hyperhq/hypercli/pkg/pools/pools_test.go
generated
vendored
Normal file
@@ -0,0 +1,162 @@
|
||||
package pools
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"io"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBufioReaderPoolGetWithNoReaderShouldCreateOne(t *testing.T) {
|
||||
reader := BufioReader32KPool.Get(nil)
|
||||
if reader == nil {
|
||||
t.Fatalf("BufioReaderPool should have create a bufio.Reader but did not.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBufioReaderPoolPutAndGet(t *testing.T) {
|
||||
sr := bufio.NewReader(strings.NewReader("foobar"))
|
||||
reader := BufioReader32KPool.Get(sr)
|
||||
if reader == nil {
|
||||
t.Fatalf("BufioReaderPool should not return a nil reader.")
|
||||
}
|
||||
// verify the first 3 byte
|
||||
buf1 := make([]byte, 3)
|
||||
_, err := reader.Read(buf1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if actual := string(buf1); actual != "foo" {
|
||||
t.Fatalf("The first letter should have been 'foo' but was %v", actual)
|
||||
}
|
||||
BufioReader32KPool.Put(reader)
|
||||
// Try to read the next 3 bytes
|
||||
_, err = sr.Read(make([]byte, 3))
|
||||
if err == nil || err != io.EOF {
|
||||
t.Fatalf("The buffer should have been empty, issue an EOF error.")
|
||||
}
|
||||
}
|
||||
|
||||
type simpleReaderCloser struct {
|
||||
io.Reader
|
||||
closed bool
|
||||
}
|
||||
|
||||
func (r *simpleReaderCloser) Close() error {
|
||||
r.closed = true
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestNewReadCloserWrapperWithAReadCloser(t *testing.T) {
|
||||
br := bufio.NewReader(strings.NewReader(""))
|
||||
sr := &simpleReaderCloser{
|
||||
Reader: strings.NewReader("foobar"),
|
||||
closed: false,
|
||||
}
|
||||
reader := BufioReader32KPool.NewReadCloserWrapper(br, sr)
|
||||
if reader == nil {
|
||||
t.Fatalf("NewReadCloserWrapper should not return a nil reader.")
|
||||
}
|
||||
// Verify the content of reader
|
||||
buf := make([]byte, 3)
|
||||
_, err := reader.Read(buf)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if actual := string(buf); actual != "foo" {
|
||||
t.Fatalf("The first 3 letter should have been 'foo' but were %v", actual)
|
||||
}
|
||||
reader.Close()
|
||||
// Read 3 more bytes "bar"
|
||||
_, err = reader.Read(buf)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if actual := string(buf); actual != "bar" {
|
||||
t.Fatalf("The first 3 letter should have been 'bar' but were %v", actual)
|
||||
}
|
||||
if !sr.closed {
|
||||
t.Fatalf("The ReaderCloser should have been closed, it is not.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBufioWriterPoolGetWithNoReaderShouldCreateOne(t *testing.T) {
|
||||
writer := BufioWriter32KPool.Get(nil)
|
||||
if writer == nil {
|
||||
t.Fatalf("BufioWriterPool should have create a bufio.Writer but did not.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBufioWriterPoolPutAndGet(t *testing.T) {
|
||||
buf := new(bytes.Buffer)
|
||||
bw := bufio.NewWriter(buf)
|
||||
writer := BufioWriter32KPool.Get(bw)
|
||||
if writer == nil {
|
||||
t.Fatalf("BufioReaderPool should not return a nil writer.")
|
||||
}
|
||||
written, err := writer.Write([]byte("foobar"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if written != 6 {
|
||||
t.Fatalf("Should have written 6 bytes, but wrote %v bytes", written)
|
||||
}
|
||||
// Make sure we Flush all the way ?
|
||||
writer.Flush()
|
||||
bw.Flush()
|
||||
if len(buf.Bytes()) != 6 {
|
||||
t.Fatalf("The buffer should contain 6 bytes ('foobar') but contains %v ('%v')", buf.Bytes(), string(buf.Bytes()))
|
||||
}
|
||||
// Reset the buffer
|
||||
buf.Reset()
|
||||
BufioWriter32KPool.Put(writer)
|
||||
// Try to write something
|
||||
written, err = writer.Write([]byte("barfoo"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// If we now try to flush it, it should panic (the writer is nil)
|
||||
// recover it
|
||||
defer func() {
|
||||
if r := recover(); r == nil {
|
||||
t.Fatal("Trying to flush the writter should have 'paniced', did not.")
|
||||
}
|
||||
}()
|
||||
writer.Flush()
|
||||
}
|
||||
|
||||
type simpleWriterCloser struct {
|
||||
io.Writer
|
||||
closed bool
|
||||
}
|
||||
|
||||
func (r *simpleWriterCloser) Close() error {
|
||||
r.closed = true
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestNewWriteCloserWrapperWithAWriteCloser(t *testing.T) {
|
||||
buf := new(bytes.Buffer)
|
||||
bw := bufio.NewWriter(buf)
|
||||
sw := &simpleWriterCloser{
|
||||
Writer: new(bytes.Buffer),
|
||||
closed: false,
|
||||
}
|
||||
bw.Flush()
|
||||
writer := BufioWriter32KPool.NewWriteCloserWrapper(bw, sw)
|
||||
if writer == nil {
|
||||
t.Fatalf("BufioReaderPool should not return a nil writer.")
|
||||
}
|
||||
written, err := writer.Write([]byte("foobar"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if written != 6 {
|
||||
t.Fatalf("Should have written 6 bytes, but wrote %v bytes", written)
|
||||
}
|
||||
writer.Close()
|
||||
if !sw.closed {
|
||||
t.Fatalf("The ReaderCloser should have been closed, it is not.")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user