Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BufferPool ¶
type BufferPool struct {
// contains filtered or unexported fields
}
BufferPool provides a pool of byte slice, made from a single huge byte array.
Example ¶
pool := New(10, 1000)
func() {
buffer := pool.Take()
defer pool.Put(buffer)
}()
Example (Concurrent) ¶
// run with go test -race
pool := New(10, 1000)
start := make(chan struct{})
var done sync.WaitGroup
for i := 0; i < 1000; i++ {
done.Add(1)
go func() {
defer done.Done()
<-start
buffer := pool.Take()
defer pool.Put(buffer)
for k := range buffer {
buffer[k] = byte(k & 0xFF)
}
}()
}
close(start)
done.Wait()
func (*BufferPool) Expand ¶
func (bf *BufferPool) Expand(bufferCount int)
Expand expands the pool bufferCount times, creating a new underlying array.
func (*BufferPool) Put ¶
func (bf *BufferPool) Put(buffer []byte) bool
Put puts back a []byte into the pool unless the pool if full or the provided buffer has a different length than the initial buffer size.
func (*BufferPool) Take ¶
func (bf *BufferPool) Take() (buffer []byte)
Take returns a byte slice from the pool or nil if the pool is depleted.
Click to show internal directories.
Click to hide internal directories.