improve server startup

This commit is contained in:
Ryan McGuire 2025-03-07 17:19:42 -05:00
parent e178956eef
commit f0a699029a

View File

@ -2,6 +2,7 @@ package app
import ( import (
"errors" "errors"
"sync"
"github.com/rs/zerolog" "github.com/rs/zerolog"
"go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/attribute"
@ -41,26 +42,38 @@ func (a *App) MustRun() {
ctx, initSpan := a.tracer.Start(a.AppContext, "init") ctx, initSpan := a.tracer.Start(a.AppContext, "init")
defer initSpan.End() defer initSpan.End()
var serverWG sync.WaitGroup
// Start HTTP (does not block) // Start HTTP (does not block)
if a.cfg.HTTPEnabled() { if a.cfg.HTTPEnabled() {
serverWG.Add(1)
go func() {
defer serverWG.Done()
if err := a.initHTTP(ctx); err != nil { if err := a.initHTTP(ctx); err != nil {
initSpan.RecordError(err) initSpan.RecordError(err)
initSpan.SetStatus(codes.Error, err.Error()) initSpan.SetStatus(codes.Error, err.Error())
} }
initSpan.AddEvent("http server started") initSpan.AddEvent("http server started")
initSpan.SetAttributes(attribute.Int("http.handlers", len(a.HTTP.Funcs))) initSpan.SetAttributes(attribute.Int("http.handlers", len(a.HTTP.Funcs)))
}()
} }
// Start GRPC (does not block) // Start GRPC (does not block)
if a.cfg.GRPCEnabled() { if a.cfg.GRPCEnabled() {
serverWG.Add(1)
go func() {
defer serverWG.Done()
if err := a.initGRPC(ctx); err != nil { if err := a.initGRPC(ctx); err != nil {
initSpan.RecordError(err) initSpan.RecordError(err)
initSpan.SetStatus(codes.Error, err.Error()) initSpan.SetStatus(codes.Error, err.Error())
} }
initSpan.AddEvent("grpc server started") initSpan.AddEvent("grpc server started")
initSpan.SetAttributes(attribute.Int("grpc.services", len(a.GRPC.Services))) initSpan.SetAttributes(attribute.Int("grpc.services", len(a.GRPC.Services)))
}()
} }
serverWG.Wait()
// Monitor app lifecycle // Monitor app lifecycle
go a.run() go a.run()