Skip to content

Commit

Permalink
Close connection after background is shut down
Browse files Browse the repository at this point in the history
That way we can make sure that there won't be a window between closing
a connection and waiting for the background goroutine to return, where
the new connection could be established.
  • Loading branch information
krnowak committed Nov 20, 2020
1 parent 38c6435 commit ab26835
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions exporters/otlp/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type grpcConnection struct {

// mu protects the connection as it is accessed by the
// exporter goroutines and background connection goroutine
mu sync.RWMutex
mu sync.Mutex
cc *grpc.ClientConn

// these fields are read-only after constructor is finished
Expand Down Expand Up @@ -236,15 +236,6 @@ func (oc *grpcConnection) contextWithMetadata(ctx context.Context) context.Conte
}

func (oc *grpcConnection) shutdown(ctx context.Context) error {
oc.mu.RLock()
cc := oc.cc
oc.mu.RUnlock()

var err error
if cc != nil {
err = cc.Close()
}

close(oc.stopCh)
// Ensure that the backgroundConnector returns
select {
Expand All @@ -255,7 +246,16 @@ func (oc *grpcConnection) shutdown(ctx context.Context) error {

close(oc.disconnectedCh)

return err
oc.mu.Lock()
cc := oc.cc
oc.cc = nil
oc.mu.Unlock()

if cc != nil {
return cc.Close()
}

return nil
}

func (oc *grpcConnection) contextWithStop(ctx context.Context) (context.Context, context.CancelFunc) {
Expand Down

0 comments on commit ab26835

Please sign in to comment.