1
0
mirror of https://github.com/tmrts/go-patterns.git synced 2024-11-24 22:16:12 +03:00
go-patterns/object_pool/pool.go

73 lines
1.1 KiB
Go
Raw Normal View History

2016-01-19 09:03:51 +03:00
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)
}