mirror of
https://github.com/crazybber/awesome-patterns.git
synced 2024-11-22 20:56:02 +03:00
28 lines
297 B
Go
28 lines
297 B
Go
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
|
|
}
|
|
|
|
|
|
|
|
p := New(2)
|
|
|
|
select {
|
|
case obj := <-p:
|
|
obj.Do( /*...*/ )
|
|
|
|
p <- obj
|
|
default:
|
|
// No more objects left — retry later or fail
|
|
return
|
|
}
|