improve grpc lifecycle
This commit is contained in:
parent
d414754e14
commit
c5da5f7887
@ -17,6 +17,8 @@ func (a *App) Done() <-chan any {
|
|||||||
func (a *App) MustRun() {
|
func (a *App) MustRun() {
|
||||||
if a.cfg != nil {
|
if a.cfg != nil {
|
||||||
panic(errors.New("already ran app trying to run"))
|
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
|
// Set up app
|
||||||
@ -36,20 +38,24 @@ func (a *App) MustRun() {
|
|||||||
defer initSpan.End()
|
defer initSpan.End()
|
||||||
|
|
||||||
// Start HTTP (does not block)
|
// Start HTTP (does not block)
|
||||||
|
if a.cfg.HTTP.Enabled {
|
||||||
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.GRPC.Enabled {
|
||||||
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)))
|
||||||
|
}
|
||||||
|
|
||||||
// Monitor app lifecycle
|
// Monitor app lifecycle
|
||||||
go a.run()
|
go a.run()
|
||||||
|
@ -62,6 +62,8 @@ func InitGRPCServer(ctx context.Context, opts *opts.GRPCOpts) (
|
|||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
appGRPC.logger.Debug().Msg("GRPC Server Started")
|
||||||
|
|
||||||
span.SetStatus(codes.Ok, "")
|
span.SetStatus(codes.Ok, "")
|
||||||
return appGRPC.shutdownFunc, appGRPC.doneChan, nil
|
return appGRPC.shutdownFunc, appGRPC.doneChan, nil
|
||||||
}
|
}
|
||||||
|
@ -56,8 +56,11 @@ func (a *appGRPCServer) getShutdownFunc() func(context.Context) error {
|
|||||||
|
|
||||||
select {
|
select {
|
||||||
case <-stoppedChan:
|
case <-stoppedChan:
|
||||||
|
a.logger.Warn().Msg("GRPC server shut down gracefully")
|
||||||
return nil
|
return nil
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
|
a.logger.Warn().Msg("GRPC server shut down hard")
|
||||||
|
a.server.Stop()
|
||||||
return ctx.Err()
|
return ctx.Err()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user