1
0
mirror of https://github.com/tmrts/go-patterns.git synced 2024-11-25 14:36:06 +03:00
go-patterns/creational/object_pool.go

73 lines
1.1 KiB
Go

package main
import "container/list"
type Object struct {
ID string
}
type Pool interface {
Borrow() (*Object, error)
Return(*Object) error
}
type Allocate func() (Object, error)
type implementation struct {
Size int
SizeLimit int
Allocate Allocate
Objects map[Object]bool
FreeList *list.List
}
func New(initSize int, limit int, alloc Allocate) (*Pool, error) {
p := &implementation{
Size: initSize,
SizeLimit: limit,
Allocate: alloc,
Objects: nil,
FreeList: list.New(),
}
for i := 0; i < initSize; i++ {
obj, err := p.Allocate()
if err != nil {
return nil, err
}
p.FreeList.PushFront(&obj)
p.Objects[obj] = true
}
return nil
}
func (p *implementation) Borrow() (*Object, error) {
elem := p.FreeList.Front()
obj := p.FreeList.Remove(elem)
p.Objects[obj] = false
return obj, nil
}
func (p *implementation) Return(ref *Object) error {
list.PushBack(ref)
p.Objects[*ref] = true
return nil
}
func main() {
p := New(0, 0, func() (Object, error) {
return []string{}
})
s, _ := p.Borrow()
s = append(s, "string")
_ = p.Return(s)
}