type Pool struct {
pool chan *Client
}
// 创建一个新的 pool
func NewPool(max int) *Pool {
return Pool{
pool: make(chan *Client, max),
}
}
// 从 pool 里借一个 Client
func (p *Pool) Borrow() *Client {
var cl *Client
select {
case cl = -p.pool:
default:
cl = newClient()
}
return cl
}
// 还回去
func (p *Pool) Return(cl *Client) {
select {
case p.pool - cl:
default:
// let it go, let it go...
}
}