Go语言中使用 buffered channel 实现线程安全的 pool
概述
我们已经知道Go语言提供了sync.Pool,但是做的不怎么好,所以有必要自己来实现一个pool。
给我看代码:
typePoolstruct{
poolchan*Client
}
//创建一个新的pool funcNewPool(maxint)*Pool{ return&Pool{ pool:make(chan*Client,max), } }
//从pool里借一个Client func(p*Pool)Borrow()*Client{ varcl*Client select{ casecl=<-p.pool: default: cl=newClient() } returncl }
//还回去 func(p*Pool)Return(cl*Client){ select{ casep.pool<-cl: default: //letitgo,letitgo... } }
热门推荐
10 感谢韩语老师祝福语简短
11 升职文案高级祝福语简短
12 花童结婚祝福语大全简短
13 向国庆送祝福语简短
14 什么寄语简短祝福语女生
15 客户回礼祝福语大全简短
16 女孩生日祝福语简短创意
17 家长回复节日祝福语简短
18 给外婆敬酒祝福语简短