使用 Bufferd channel 实现 线程安全的 pool

从 文章 进行转载,再次感谢

概述

我们知道,Go 语言已经提供了 sync.Pool,但是做的不怎么好,所以有必要来实现一个 Pool

代码type Pool struct {pool chan *Client}// Create a new Poolfunc NewPool(max int) *Pool{return &Pool{pool: make(chan *Client, max)}}// Get a Client from Poolfunc (p *Pool) Borrow() *Client {var cl *Clientselect {case cl = <-p.pool:default:c1 = newClient()}return cl}// Return the Cliet to the poolfunc (p *Pool) Return(cl *Client){select {case p.pool<- cl:default:// let it go, let it go….}}

现在是 Golang1.4了,,sync.Pool还是不可以用吗?

勇于接受自己的不完美,认清自己不足的地方,

使用 Bufferd channel 实现 线程安全的 pool

相关文章:

你感兴趣的文章:

标签云: