Documentation ¶
Index ¶
Constants ¶
const DefaultBufSize = 4 * 1024
DefaultBufSize is the default size used to create new buffers.
const DefaultMaxKeep = 16 * 1024
DefaultMaxKeep is the default size used to determine whether to keep buffers returned to the pool.
const DefaultPoolSize = 100
DefaultPoolSize is the default number of buffers that the free-list will maintain.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ChanPool ¶
type ChanPool struct {
// contains filtered or unexported fields
}
ChanPool maintains a free-list of buffers.
type Configurator ¶
type Configurator func(*poolConfig) error
Configurator is a function that modifies a pool configuration structure.
func BufSize ¶
func BufSize(size int) Configurator
BufSize specifies the size of newly allocated buffers.
package main import ( "log" "github.com/karrick/bufpool" ) func main() { bp, err := bufpool.NewChanPool(bufpool.BufSize(1024)) if err != nil { log.Fatal(err) } for i := 0; i < 4*bufpool.DefaultPoolSize; i++ { go func() { for j := 0; j < 1000; j++ { bb := bp.Get() for k := 0; k < 3*bufpool.DefaultBufSize; k++ { bb.WriteByte(byte(k % 256)) } bp.Put(bb) } }() } }
func MaxKeep ¶
func MaxKeep(size int) Configurator
MaxKeep specifies the maximum size of buffers that ought to be kept when returned to the free list. Buffers with a capacity larger than this size will be discarded, and their memory returned to the runtime.
package main import ( "log" "github.com/karrick/bufpool" ) func main() { bp, err := bufpool.NewChanPool(bufpool.MaxKeep(32 * 1024)) if err != nil { log.Fatal(err) } for i := 0; i < 4*bufpool.DefaultPoolSize; i++ { go func() { for j := 0; j < 1000; j++ { bb := bp.Get() for k := 0; k < 3*bufpool.DefaultBufSize; k++ { bb.WriteByte(byte(k % 256)) } bp.Put(bb) } }() } }
func PoolSize ¶
func PoolSize(size int) Configurator
PoolSize specifies the number of buffers to maintain in the pool. This option has no effect, however, on free-lists created with NewSyncPool, because the Go runtime dynamically maintains the size of pools created using sync.Pool.
package main import ( "log" "github.com/karrick/bufpool" ) func main() { bp, err := bufpool.NewChanPool(bufpool.PoolSize(25)) if err != nil { log.Fatal(err) } for i := 0; i < 4*bufpool.DefaultPoolSize; i++ { go func() { for j := 0; j < 1000; j++ { bb := bp.Get() for k := 0; k < 3*bufpool.DefaultBufSize; k++ { bb.WriteByte(byte(k % 256)) } bp.Put(bb) } }() } }
type FreeList ¶
FreeList represents a data structure that maintains a free-list of buffers, accesible via Get and Put methods.
func NewChanPool ¶
func NewChanPool(setters ...Configurator) (FreeList, error)
NewChanPool creates a new FreeList. The pool size, size of new buffers, and max size of buffers to keep when returned to the pool can all be customized.
package main import ( "log" "github.com/karrick/bufpool" ) func main() { bp, err := bufpool.NewChanPool() if err != nil { log.Fatal(err) } for i := 0; i < 4*bufpool.DefaultPoolSize; i++ { go func() { for j := 0; j < 1000; j++ { bb := bp.Get() for k := 0; k < 3*bufpool.DefaultBufSize; k++ { bb.WriteByte(byte(k % 256)) } bp.Put(bb) } }() } }
func NewLockPool ¶
func NewLockPool(setters ...Configurator) (FreeList, error)
NewLockPool creates a new FreeList. The pool size, size of new buffers, and max size of buffers to keep when returned to the pool can all be customized.
package main import ( "log" "github.com/karrick/bufpool" ) func main() { bp, err := bufpool.NewLockPool() if err != nil { log.Fatal(err) } for i := 0; i < 4*bufpool.DefaultPoolSize; i++ { go func() { for j := 0; j < 1000; j++ { bb := bp.Get() for k := 0; k < 3*bufpool.DefaultBufSize; k++ { bb.WriteByte(byte(k % 256)) } bp.Put(bb) } }() } }
func NewPreAllocatedPool ¶ added in v1.1.0
func NewPreAllocatedPool(setters ...Configurator) (FreeList, error)
func NewSyncPool ¶
func NewSyncPool(setters ...Configurator) (FreeList, error)
NewSyncPool creates a new FreeList. The pool size, size of new buffers, and max size of buffers to keep when returned to the pool can all be customized. Note that the PoolSize method has no effect for free-lists created with NewSyncPool, because the Go runtime dynamically maintains the size of pools created using sync.Pool.
package main import ( "log" "github.com/karrick/bufpool" ) func main() { bp, err := bufpool.NewSyncPool() if err != nil { log.Fatal(err) } for i := 0; i < 4*bufpool.DefaultPoolSize; i++ { go func() { for j := 0; j < 1000; j++ { bb := bp.Get() for k := 0; k < 3*bufpool.DefaultBufSize; k++ { bb.WriteByte(byte(k % 256)) } bp.Put(bb) } }() } }
type LockPool ¶
type LockPool struct {
// contains filtered or unexported fields
}
LockPool maintains a free-list of buffers.