defer wg.Done so it's called even if there's a panic

This commit is contained in:
Asim Aslam 2017-06-12 14:18:59 +01:00
parent 32b7ebcfbc
commit 3812cbbcb6
2 changed files with 2 additions and 5 deletions

View File

@ -104,16 +104,13 @@ func (s *rpcServer) accept(sock transport.Socket) {
// add to wait group // add to wait group
s.wg.Add(1) s.wg.Add(1)
defer s.wg.Done()
// TODO: needs better error handling // TODO: needs better error handling
if err := s.rpc.serveRequest(ctx, codec, ct); err != nil { if err := s.rpc.serveRequest(ctx, codec, ct); err != nil {
log.Logf("Unexpected error serving request, closing socket: %v", err) log.Logf("Unexpected error serving request, closing socket: %v", err)
s.wg.Done()
return return
} }
// finish request
s.wg.Done()
} }
} }

View File

@ -228,12 +228,12 @@ func (s *rpcServer) createSubHandler(sb *subscriber, opts Options) broker.Handle
s.wg.Add(1) s.wg.Add(1)
go func() { go func() {
defer s.wg.Done()
fn(ctx, &rpcPublication{ fn(ctx, &rpcPublication{
topic: sb.topic, topic: sb.topic,
contentType: ct, contentType: ct,
message: req.Interface(), message: req.Interface(),
}) })
s.wg.Done()
}() }()
} }
return nil return nil