From e7644d4b4767a25ed83e33c0c5deed6d43acdd7a Mon Sep 17 00:00:00 2001 From: Ville Valkonen Date: Mon, 23 Jun 2025 21:40:51 +0300 Subject: [PATCH 1/3] Inline shutdown naming. While here, move it before the main web server since this should be fairly fast to shut down. Also the timeout is shorter. Signed-off-by: Ville Valkonen --- cmd/webserver/main.go | 2 +- pkg/profiling/http.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/webserver/main.go b/cmd/webserver/main.go index c46945f..d363a13 100644 --- a/cmd/webserver/main.go +++ b/cmd/webserver/main.go @@ -102,8 +102,8 @@ func main() { recvSignal := <-sig l.Logger.Info().Str("received_signal", recvSignal.String()).Msg("Performing a graceful shutdown") + pprofServer.Shutdown(ctx) httpServer.Shutdown(ctx) - pprofServer.Stop(ctx) l.Logger.Info().Msgf("Service %s exiting", serviceName) } diff --git a/pkg/profiling/http.go b/pkg/profiling/http.go index e493016..daaf011 100644 --- a/pkg/profiling/http.go +++ b/pkg/profiling/http.go @@ -45,7 +45,7 @@ func (p *PprofServer) Start() { }() } -func (p *PprofServer) Stop(ctx context.Context) { +func (p *PprofServer) Shutdown(ctx context.Context) { p.once.Do(func() { cCtx, cancel := context.WithTimeoutCause( ctx, From bee0e2c4a316b52de65f9fdb82cf8c9813bae9e0 Mon Sep 17 00:00:00 2001 From: Ville Valkonen Date: Mon, 23 Jun 2025 21:41:44 +0300 Subject: [PATCH 2/3] Fix panic when shutting down the service. Trying to access method in nil pointer trips the flow. Who would have guessed? Signed-off-by: Ville Valkonen --- pkg/profiling/http.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/profiling/http.go b/pkg/profiling/http.go index daaf011..ed92cdd 100644 --- a/pkg/profiling/http.go +++ b/pkg/profiling/http.go @@ -27,6 +27,7 @@ func NewPprofServer() *PprofServer { listenAddress := net.JoinHostPort(hostname, port) return &PprofServer{ listenAddr: listenAddress, + once: &sync.Once{}, server: &http.Server{ Addr: listenAddress, Handler: http.DefaultServeMux, From 9364cd694343d5b0cb56b133862f0537e9e6d491 Mon Sep 17 00:00:00 2001 From: Ville Valkonen Date: Mon, 23 Jun 2025 21:50:45 +0300 Subject: [PATCH 3/3] Fix otel connection issues. Same as in the previous commit: uninitialized sync.Once. Also the connection closing order was wrong, socket must the fast to be closed. That caused issue with graceful shutdown. Signed-off-by: Ville Valkonen --- pkg/tracer/otel.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/pkg/tracer/otel.go b/pkg/tracer/otel.go index 9b6cbb8..e40bb17 100644 --- a/pkg/tracer/otel.go +++ b/pkg/tracer/otel.go @@ -6,6 +6,7 @@ import ( "fmt" "net" "sync" + "time" l "weezel/example-gin/pkg/logger" @@ -59,6 +60,7 @@ func NewOtelTracerMetrics( serviceName: serviceName, res: res, modes: modes, + closeOnce: &sync.Once{}, } otelTracerMetrics.connection, err = grpc.NewClient( @@ -98,14 +100,19 @@ func (o *OtelTracerMetrics) Close(ctx context.Context) { var connErr error var tracerErr error var metricsErr error - if o.connection != nil { - connErr = o.connection.Close() - } + + timeout := 4 * time.Second + cCtx, cancel := context.WithTimeout(ctx, timeout) + defer cancel() + if o.tracer != nil { - tracerErr = o.tracer.Shutdown(ctx) + tracerErr = o.tracer.Shutdown(cCtx) } if o.metrics != nil { - metricsErr = o.metrics.Shutdown(ctx) + metricsErr = o.metrics.Shutdown(cCtx) + } + if o.connection != nil { + connErr = o.connection.Close() } errs := errors.Join(connErr, tracerErr, metricsErr) if errs != nil {