Add selecting on stop chan to avoid goroutine leak

This commit is contained in:
Menghan Li
2016-04-29 10:58:11 -07:00
parent db85417dd0
commit ceacfbcbc1

View File

@ -200,8 +200,8 @@ func (bc *benchmarkClient) doCloseLoopUnary(conns []*grpc.ClientConn, rpcCountPe
for j := 0; j < rpcCountPerConn; j++ { for j := 0; j < rpcCountPerConn; j++ {
go func(client testpb.BenchmarkServiceClient) { go func(client testpb.BenchmarkServiceClient) {
defer wg.Done() defer wg.Done()
for {
done := make(chan bool) done := make(chan bool)
for {
go func() { go func() {
start := time.Now() start := time.Now()
if err := benchmark.DoUnaryCall(client, reqSize, respSize); err != nil { if err := benchmark.DoUnaryCall(client, reqSize, respSize); err != nil {
@ -212,7 +212,10 @@ func (bc *benchmarkClient) doCloseLoopUnary(conns []*grpc.ClientConn, rpcCountPe
bc.mu.Lock() bc.mu.Lock()
bc.histogram.Add(int64(elapse / time.Nanosecond)) bc.histogram.Add(int64(elapse / time.Nanosecond))
bc.mu.Unlock() bc.mu.Unlock()
done <- true select {
case <-bc.stop:
case done <- true:
}
}() }()
select { select {
case <-bc.stop: case <-bc.stop:
@ -259,8 +262,8 @@ func (bc *benchmarkClient) doCloseLoopStreaming(conns []*grpc.ClientConn, rpcCou
for j := 0; j < rpcCountPerConn; j++ { for j := 0; j < rpcCountPerConn; j++ {
go func(stream testpb.BenchmarkService_StreamingCallClient) { go func(stream testpb.BenchmarkService_StreamingCallClient) {
defer wg.Done() defer wg.Done()
for {
done := make(chan bool) done := make(chan bool)
for {
go func() { go func() {
start := time.Now() start := time.Now()
if err := doRPC(stream, reqSize, respSize); err != nil { if err := doRPC(stream, reqSize, respSize); err != nil {
@ -271,7 +274,10 @@ func (bc *benchmarkClient) doCloseLoopStreaming(conns []*grpc.ClientConn, rpcCou
bc.mu.Lock() bc.mu.Lock()
bc.histogram.Add(int64(elapse / time.Nanosecond)) bc.histogram.Add(int64(elapse / time.Nanosecond))
bc.mu.Unlock() bc.mu.Unlock()
done <- true select {
case <-bc.stop:
case done <- true:
}
}() }()
select { select {
case <-bc.stop: case <-bc.stop: