Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/theodesp/go-object-pool

Simple and efficient implementation of a generic Object Pool in Go
https://github.com/theodesp/go-object-pool

design-patterns golang object-pool resource-pool

Last synced: about 1 month ago
JSON representation

Simple and efficient implementation of a generic Object Pool in Go

Awesome Lists containing this project

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
```bash
$ go get -u github.com/theodesp/go-object-pool
```

## Usage

Provide the necessary interface implementations first and then
create the Pool

```go
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