Skip to content

Simple and efficient implementation of a generic Object Pool in Go

License

Notifications You must be signed in to change notification settings

theodesp/go-object-pool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published