Skip to content

Commit

Permalink
[extension/httpforwarder] Shutdown should wait server exit
Browse files Browse the repository at this point in the history
  • Loading branch information
pjanotti committed Feb 6, 2025
1 parent 6638b83 commit 7638c56
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion extension/httpforwarderextension/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"io"
"net/http"
"net/url"
"sync"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componentstatus"
Expand All @@ -23,6 +24,7 @@ type httpForwarder struct {
server *http.Server
settings component.TelemetrySettings
config *Config
shutdownWG sync.WaitGroup
}

var _ extension.Extension = (*httpForwarder)(nil)
Expand All @@ -47,7 +49,9 @@ func (h *httpForwarder) Start(ctx context.Context, host component.Host) error {
return fmt.Errorf("failed to create HTTP Client: %w", err)
}

h.shutdownWG.Add(1)
go func() {
defer h.shutdownWG.Done()
if errHTTP := h.server.Serve(listener); !errors.Is(errHTTP, http.ErrServerClosed) && errHTTP != nil {
componentstatus.ReportStatus(host, componentstatus.NewFatalErrorEvent(errHTTP))
}
Expand All @@ -60,7 +64,9 @@ func (h *httpForwarder) Shutdown(_ context.Context) error {
if h.server == nil {
return nil
}
return h.server.Close()
err := h.server.Close()
h.shutdownWG.Wait()
return err
}

func (h *httpForwarder) forwardRequest(writer http.ResponseWriter, request *http.Request) {
Expand Down

0 comments on commit 7638c56

Please sign in to comment.