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 生日广州男生祝福语简短