theodesp / go-object-pool

Simple and efficient implementation of a generic Object Pool in Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

About

Simple and efficient implementation of a generic Object Pool in Go

License:MIT License


Languages

Language:Go 90.0%Language:Makefile 10.0%