概述
我们知道,Go 语言已经提供了 sync.Pool
,但是做的不怎么好,所以有必要来实现一个 Pool
代码
@H_301_12@type Pool @H_301_12@struct {
pool @H_301_12@chan *Client
}
// Create a new Pool
@H_301_12@func NewPool(max int) *Pool{
@H_301_12@return &Pool{
pool: make(@H_301_12@chan *Client,max)
}
}
// Get a Client from Pool
@H_301_12@func (p *Pool) Borrow() *Client {
@H_301_12@var cl *Client
@H_301_12@select {
@H_301_12@case cl = <-p.pool:
@H_301_12@default:
c1 = newClient()
}
@H_301_12@return cl
}
// Return the Cliet to the pool
@H_301_12@func (p *Pool) Return(cl *Client){
@H_301_12@select {
@H_301_12@case p.pool<- cl:
@H_301_12@default:
// let it go,let it go....
}
}
现在是 Golang1.4了,sync.Pool
还是不可以用吗?