pool

package
v2.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 10, 2022 License: LGPL-3.0 Imports: 2 Imported by: 0

README

Overview

First of all, sorry for my poor English, I've tried my best.

Package pool offers goroutine-safe object pools such as ObjectPool and BufferPool.

ObjectPool is a generic object pool that can be used to pool objects of any type, it performs about 36% slower than the other specialized object pools such as BufferPool.

ObjectPool

ObjectPool is a goroutine-safe generic pool for objects of any type. It performs about 36% slower than the other specialized object pools such as BufferPool.

Basic example

// create an ObjectPool for bytes.Buffer
op := pool.NewObjectPool(10000,
                          func() interface{} { return new(bytes.Buffer) },
                          func(obj interface{}) { obj.(*bytes.Buffer).Reset() })
obj := op.Get() // get a ready-to-use bytes.Buffer
buf := obj.(*bytes.Buffer)
// do something with `buf`
op.Put(obj) // return obj to ObjectPool. `op.Put(buf)` is OK too.

BufferPool

BufferPool is a goroutine-safe pool for bytes.Buffer.

Basic example

bp := pool.NewBufferPool(1000, 512)
buf := bp.Get() // get a ready-to-use bytes.Buffer
// do something with `buf`
bp.Put(buf) // return buf to BufferPool

Documentation

Overview

Package pool offers goroutine-safe object pools such as ObjectPool and BufferPool.

ObjectPool is a generic object pool that can be used to pool objects of any type, it performs about 36% slower than the other specialized object pools such as BufferPool.

Sorry for my poor English, I've tried my best.

Index

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 is a goroutine-safe pool for bytes.Buffer.

func NewBufferPool

func NewBufferPool(maxBufferNum, initBufferSize int) *BufferPool

NewBufferPool is the only way to get a new, ready-to-use BufferPool from which bytes.Buffer could be get.

If you use `var bp pool.BufferPool`, or `new(pool.BufferPool)`, or the like to obtain a BufferPool, it'll still work, but it won't pool even a single bytes.Buffer.

maxBufferNum: Maximum number of bytes.Buffer that will be pooled in BufferPool
initBufferSize: Initial size in bytes for a newly created bytes.Buffer

Example:

bp := pool.NewBufferPool(1000, 512)
buf := bp.Get() // get a ready-to-use bytes.Buffer
// do something with `buf`
bp.Put(buf) // return buf to BufferPool

func (*BufferPool) Get

func (bp *BufferPool) Get() *bytes.Buffer

Get returns a ready-to-use bytes.Buffer.

func (*BufferPool) Put

func (bp *BufferPool) Put(buf *bytes.Buffer)

Put returns a bytes.Buffer to the BufferPool.

type ClearFunc

type ClearFunc func(interface{})

ClearFunc is used by ObjectPool to reset a used object to it's initial state for reuse.

type CreateFunc

type CreateFunc func() interface{}

CreateFunc is used by ObjectPool to create a new object when it's empty.

type GoRoutinePool

type GoRoutinePool struct {
	// contains filtered or unexported fields
}

GoRoutinePool is a goroutine-safe pool for goroutines.

After benchmarking, I found that use raw `go` keyword performs much better than this GoRoutinePool. So it makes no sense to use this GoRoutinePool.

func NewGoRoutinePool

func NewGoRoutinePool(maxGoRoutineNum int) *GoRoutinePool

NewGoRoutinePool is the only way to get a new, ready-to-use GoRoutinePool.

If you use `var goPool pool.GoRoutinePool`, or `new(pool.GoRoutinePool)`, or the like to obtain a GoRoutinePool, it'll still work, but it won't pool even a single goroutine.

maxGoRoutineNum: Maximum number of goroutines that will be pooled in GoRoutinePool

Example:

goPool := pool.NewGoRoutinePool(100)
goPool.Run(func(){ fmt.Println("Hello, GoRoutinePool!") }) // runs a function using a pooled goroutine

func (*GoRoutinePool) Run

func (goPool *GoRoutinePool) Run(f func())

Run executes a function using a pooled goroutine.

type ObjectPool

type ObjectPool struct {
	// contains filtered or unexported fields
}

ObjectPool is a goroutine-safe generic pool for objects of any type. It performs about 36% slower than the other specialized object pools such as BufferPool.

func NewObjectPool

func NewObjectPool(maxObjectNum int, createObj CreateFunc, clearObj ClearFunc) *ObjectPool

NewObjectPool is the only way to get a new, ready-to-use ObjectPool for objects of a specified type.

If you use `var op pool.ObjectPool`, or `new(pool.ObjectPool)`, or the like to obtain an ObjectPool, it'll crash when you call Get().

maxObjectNum: Maximum number of objects that will be pooled in ObjectPool.
createObj: Called to create a new object when ObjectPool is empty. Cannot be nil.
clearObj: Called to reset a used object to it's initial state for reuse. Could be nil if it need not to be reset.
          ObjectPool will perform about 12% faster if `clearObj` is nil.

Example:

// create an ObjectPool for bytes.Buffer
op := pool.NewObjectPool(10000,
                         func() interface{} { return new(bytes.Buffer) },
                         func(obj interface{}) { obj.(*bytes.Buffer).Reset() })
obj := op.Get() // get a ready-to-use bytes.Buffer
buf := obj.(*bytes.Buffer)
// do something with `buf`
op.Put(obj) // return obj to ObjectPool. `op.Put(buf)` is OK too.

func (*ObjectPool) Get

func (op *ObjectPool) Get() interface{}

Get returns a ready-to-use object.

func (*ObjectPool) Put

func (op *ObjectPool) Put(obj interface{})

Put returns an object to ObjectPool.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL