awesome-patterns/creational/object_pool.go

28 lines
297 B
Go
Raw Permalink Normal View History

2019-10-30 21:04:43 +03:00
package pool
type Pool chan *Object
func New(total int) *Pool {
p := make(Pool, total)
for i := 0; i < total; i++ {
p <- new(Object)
}
return &p
}
2019-10-30 21:07:57 +03:00
p := New(2)
select {
case obj := <-p:
obj.Do( /*...*/ )
p <- obj
default:
// No more objects left — retry later or fail
return
}