完善grpool功能,增加goroutine限制

This commit is contained in:
John
2018-01-17 13:12:35 +08:00
parent 886a1cdbfc
commit 799f855783
3 changed files with 15 additions and 17 deletions

View File

@ -121,5 +121,4 @@ func (p *Pool) Close() {
p.SetExpire(-1)
p.stopEvents <- struct{}{} // 通知workloop
p.stopEvents <- struct{}{} // 通知clearloop
}

View File

@ -58,7 +58,6 @@ func (p *Pool) startClearLoop() {
// 判断是否达到goroutine上上限
func (p *Pool) reachSizeLimit() bool {
return false
return atomic.LoadInt32(&p.number) >= atomic.LoadInt32(&p.size)
}
@ -70,13 +69,13 @@ func (p *Pool) getExpire() int32 {
// 创建一个空的任务对象
func (p *Pool) newJob() *PoolJob {
// 如果达到goroutine数限制那么阻塞等待有空闲goroutine后继续
//if p.reachSizeLimit() {
// // 阻塞等待空闲goroutine
// select {
// case <- p.freeEvents:
// return p.getJob()
// }
//}
if p.reachSizeLimit() {
// 阻塞等待空闲goroutine
select {
case <- p.freeEvents:
return p.getJob()
}
}
j := &PoolJob {
job : make(chan func(), 1),
pool : p,
@ -93,9 +92,9 @@ func (p *Pool) addJob(j *PoolJob) bool {
}
p.queue.PushBack(j)
// 如果当前的goroutine数量达到上线那么需要使用空闲goroutine通知事件
//if p.reachSizeLimit() {
// p.freeEvents <- struct{}{}
//}
if p.reachSizeLimit() {
p.freeEvents <- struct{}{}
}
return true
}

View File

@ -23,8 +23,8 @@ func BenchmarkGrpool_1(b *testing.B) {
}
}
//func BenchmarkGoroutine_1(b *testing.B) {
// for i := 0; i < b.N; i++ {
// go increment1()
// }
//}
func BenchmarkGoroutine_1(b *testing.B) {
for i := 0; i < b.N; i++ {
go increment1()
}
}