go-object-pool

Simple and efficient implementation of a generic Object Pool in Go

MIT License

Stars
12

go-object-pool

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