Skip to content

Commit 215d8af

Browse files
lim123123123Kirill Garmanov
authored andcommitted
fix: use wg instead of chan in http server
1 parent 3c8d65a commit 215d8af

File tree

1 file changed

+6
-4
lines changed
  • extension/healthcheckv2extension/internal/http

1 file changed

+6
-4
lines changed

extension/healthcheckv2extension/internal/http/server.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"errors"
1010
"fmt"
1111
"net/http"
12+
"sync"
1213
"sync/atomic"
1314
"time"
1415

@@ -32,7 +33,7 @@ type Server struct {
3233
colconf atomic.Value
3334
aggregator *status.Aggregator
3435
startTimestamp time.Time
35-
doneCh chan struct{}
36+
doneWg sync.WaitGroup
3637
}
3738

3839
var (
@@ -52,7 +53,6 @@ func NewServer(
5253
telemetry: telemetry,
5354
mux: http.NewServeMux(),
5455
aggregator: aggregator,
55-
doneCh: make(chan struct{}),
5656
}
5757

5858
if legacyConfig.UseV2 {
@@ -96,8 +96,10 @@ func (s *Server) Start(ctx context.Context, host component.Host) error {
9696
return fmt.Errorf("failed to bind to address %s: %w", s.httpConfig.Endpoint, err)
9797
}
9898

99+
s.doneWg.Add(1)
99100
go func() {
100-
defer close(s.doneCh)
101+
defer s.doneWg.Done()
102+
101103
if err = s.httpServer.Serve(ln); !errors.Is(err, http.ErrServerClosed) && err != nil {
102104
componentstatus.ReportStatus(host, componentstatus.NewPermanentErrorEvent(err))
103105
}
@@ -112,7 +114,7 @@ func (s *Server) Shutdown(context.Context) error {
112114
return nil
113115
}
114116
s.httpServer.Close()
115-
<-s.doneCh
117+
s.doneWg.Wait()
116118
return nil
117119
}
118120

0 commit comments

Comments
 (0)