// 这里的资源是chan []byte,因此缓冲池的类型就是chan chan []byte
type Pool struct {
chch chan chan []byte
back chan chan []byte
exit chan bool
}
func NewPool(count int) *Pool {
p := new(Pool)
p.back = make(chan chan []byte, count)
p.chch = make(chan chan []byte, count)
for i := 0; i < count; i++ {
p.chch <- make(chan []byte, 1)
}
p.exit = make(chan bool)
go p.run()
return p
}
func (p *Pool) Alloc() chan []byte {
select {
case ch := <-p.chch:
return ch
default:
break
}
return make(chan []byte, 1)
}
func (p *Pool) Free(ch chan []byte) {
select {
case p.chch <- ch:
return
default:
p.back <- ch
}
}
func (p *Pool) Close() {
if p.exit != nil {
close(p.exit)
p.exit = nil
}
}
func (p *Pool) run() {
var chs []chan []byte
var chch chan chan []byte
var next chan []byte
for {
select {
case <-p.exit:
return
case ch := <-p.back:
if chch == nil {
chch = p.chch
next = ch
} else {
chs = append(chs, ch)
}
case chch <- next:
if len(chs) == 0 {
chch = nil
next = nil
} else {
next = chs[len(chs)-1]
chs = chs[:len(chs)-1]
}
}
}
}
有疑问加站长微信联系(非本文作者)