go_object_pool

package module
v0.0.0-...-eef0f2f Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2018 License: MIT Imports: 2 Imported by: 0

README

go-object-pool

GoDoc License

The object pool pattern is a software creational design pattern that uses a set of initialized objects kept ready to use – a "pool" – rather than allocating and destroying them on demand. A client of the pool will request an object from the pool and perform operations on the returned object. When the client has finished, it returns the object to the pool rather than destroying it; this can be done manually or automatically.

Installation

$ go get -u github.com/theodesp/go-object-pool

Usage

Provide the necessary interface implementations first and then create the Pool

type ByteBufferObject struct {
	buffer *bytes.Buffer
}

func (b *ByteBufferObject) Reset() {
	b.buffer.Reset()
}

type ByteBufferFactory struct{}

func (f ByteBufferFactory) Create() (PooledObject, error) {
	return &ByteBufferObject{
		buffer: bytes.NewBuffer(make([]byte, 1024)),
	}, nil
}

factory := &ByteBufferFactory{}
pool := NewFixedPool(16, factory)

obj, _ := pool.Get()
pool.Return(obj)

LICENCE

Copyright © 2017 Theo Despoudis MIT license

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FixedPool

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

*

  • Fixed size Object pool

func NewFixedPool

func NewFixedPool(capacity int, factory PooledObjectFactory) *FixedPool

Creates a new fixed pool with capacity

func (*FixedPool) Get

func (p *FixedPool) Get() (PooledObject, error)

func (*FixedPool) Return

func (p *FixedPool) Return(obj PooledObject) error

type Pool

type Pool interface {
	Get() (PooledObject, error)
	Return(obj PooledObject) error
}

All pool implementations must satisfy this interface

type PooledObject

type PooledObject interface {
	Reset()
}

type PooledObjectFactory

type PooledObjectFactory interface {
	Create() (PooledObject, error)
}

All pooled object factories must satisfy this interface

Jump to

Keyboard shortcuts

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