From c5da5f7887afd47b22c068796185c788139fb09f Mon Sep 17 00:00:00 2001 From: Ryan McGuire Date: Fri, 7 Mar 2025 16:45:50 -0500 Subject: [PATCH] improve grpc lifecycle --- pkg/app/app.go | 26 ++++++++++++++++---------- pkg/srv/grpc/grpc.go | 2 ++ pkg/srv/grpc/grpc_run.go | 3 +++ 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/pkg/app/app.go b/pkg/app/app.go index 946f373..88781d3 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -17,6 +17,8 @@ func (a *App) Done() <-chan any { func (a *App) MustRun() { if a.cfg != nil { panic(errors.New("already ran app trying to run")) + } else if !a.cfg.HTTP.Enabled && !a.cfg.GRPC.Enabled { + panic(errors.New("neither http nor grpc enabled, nothing to do")) } // Set up app @@ -36,20 +38,24 @@ func (a *App) MustRun() { defer initSpan.End() // Start HTTP (does not block) - if err := a.initHTTP(ctx); err != nil { - initSpan.RecordError(err) - initSpan.SetStatus(codes.Error, err.Error()) + if a.cfg.HTTP.Enabled { + if err := a.initHTTP(ctx); err != nil { + initSpan.RecordError(err) + initSpan.SetStatus(codes.Error, err.Error()) + } + initSpan.AddEvent("http server started") + initSpan.SetAttributes(attribute.Int("http.handlers", len(a.HTTP.Funcs))) } - initSpan.AddEvent("http server started") - initSpan.SetAttributes(attribute.Int("http.handlers", len(a.HTTP.Funcs))) // Start GRPC (does not block) - if err := a.initGRPC(ctx); err != nil { - initSpan.RecordError(err) - initSpan.SetStatus(codes.Error, err.Error()) + if a.cfg.GRPC.Enabled { + if err := a.initGRPC(ctx); err != nil { + initSpan.RecordError(err) + initSpan.SetStatus(codes.Error, err.Error()) + } + initSpan.AddEvent("grpc server started") + initSpan.SetAttributes(attribute.Int("grpc.services", len(a.GRPC.Services))) } - initSpan.AddEvent("grpc server started") - initSpan.SetAttributes(attribute.Int("grpc.services", len(a.GRPC.Services))) // Monitor app lifecycle go a.run() diff --git a/pkg/srv/grpc/grpc.go b/pkg/srv/grpc/grpc.go index 14949f0..8b30d8c 100644 --- a/pkg/srv/grpc/grpc.go +++ b/pkg/srv/grpc/grpc.go @@ -62,6 +62,8 @@ func InitGRPCServer(ctx context.Context, opts *opts.GRPCOpts) ( return nil, nil, err } + appGRPC.logger.Debug().Msg("GRPC Server Started") + span.SetStatus(codes.Ok, "") return appGRPC.shutdownFunc, appGRPC.doneChan, nil } diff --git a/pkg/srv/grpc/grpc_run.go b/pkg/srv/grpc/grpc_run.go index 3f4780f..765aa05 100644 --- a/pkg/srv/grpc/grpc_run.go +++ b/pkg/srv/grpc/grpc_run.go @@ -56,8 +56,11 @@ func (a *appGRPCServer) getShutdownFunc() func(context.Context) error { select { case <-stoppedChan: + a.logger.Warn().Msg("GRPC server shut down gracefully") return nil case <-ctx.Done(): + a.logger.Warn().Msg("GRPC server shut down hard") + a.server.Stop() return ctx.Err() } }