Golang -- 使用 Bufferd channel 实现 线程安全的 pool
发布时间:2020-12-16 18:44:01 所属栏目:大数据 来源:网络整理
导读:从 文章 进行转载,再次感谢 概述 我们知道,Go 语言已经提供了 sync.Pool ,但是做的不怎么好,所以有必要来实现一个 Pool 代码 type Pool struct { pool chan *Client} // Create a new Pool func NewPool(max int ) *Pool{ return Pool{ pool: make ( cha
从 文章 进行转载,再次感谢 概述我们知道,Go 语言已经提供了 代码type Pool struct {
pool chan *Client
}
// Create a new Pool
func NewPool(max int) *Pool{
return &Pool{
pool: make(chan *Client,max)
}
}
// Get a Client from Pool
func (p *Pool) Borrow() *Client {
var cl *Client
select {
case cl = <-p.pool:
default:
c1 = newClient()
}
return cl
}
// Return the Cliet to the pool
func (p *Pool) Return(cl *Client){
select {
case p.pool<- cl:
default:
// let it go,let it go....
}
}
现在是 Golang1.4了, (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |