Skip to content

Commit

Permalink
refactor fatals
Browse files Browse the repository at this point in the history
  • Loading branch information
FZambia committed Dec 22, 2024
1 parent cb2892d commit 8dfa2e6
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 38 deletions.
4 changes: 2 additions & 2 deletions internal/app/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func runGRPCAPIServer(cfg config.Config, node *centrifuge.Node, useAPIOpenteleme
log.Info().Msgf("serving GRPC API service on %s", grpcAPIAddr)
go func() {
if err := grpcAPIServer.Serve(grpcAPIConn); err != nil {
log.Fatal().Msgf("serve GRPC API: %v", err)
log.Fatal().Err(err).Msg("serve GRPC API")
}
}()
return grpcAPIServer, nil
Expand Down Expand Up @@ -106,7 +106,7 @@ func runGRPCUniServer(cfg config.Config, node *centrifuge.Node) (*grpc.Server, e
log.Info().Msgf("serving unidirectional GRPC on %s", grpcUniAddr)
go func() {
if err := grpcUniServer.Serve(grpcUniConn); err != nil {
log.Fatal().Msgf("serve uni GRPC: %v", err)
log.Fatal().Err(err).Msg("serve uni GRPC")
}
}()
return grpcUniServer, nil
Expand Down
22 changes: 11 additions & 11 deletions internal/app/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func Mux(
// register HTTP API endpoints.
httpErrorMode, err := tools.OptionalStringChoice(cfg.HttpAPI.ErrorMode, []string{config.TransportErrorMode})
if err != nil {
log.Fatal().Msgf("error in config: %v", err)
log.Fatal().Err(err).Msg("error in config")
}
useOpenTelemetry := cfg.OpenTelemetry.Enabled && cfg.OpenTelemetry.API
apiHandler := api.NewHandler(n, apiExecutor, api.Config{
Expand Down Expand Up @@ -441,13 +441,13 @@ func runHTTPServers(

tlsConfig, err := GetTLSConfig(cfg)
if err != nil {
log.Fatal().Msgf("can not get TLS config: %v", err)
log.Fatal().Err(err).Msg("can not get TLS config")
}
var internalTLSConfig *tls.Config
if cfg.InternalTLS.Enabled {
internalTLSConfig, err = cfg.InternalTLS.ToGoTLSConfig("internal_tls")
if err != nil {
log.Fatal().Msgf("can not get internal TLS config: %v", err)
log.Fatal().Err(err).Msg("can not get internal TLS config")
}
}

Expand Down Expand Up @@ -518,21 +518,21 @@ func runHTTPServers(

udpAddr, err := net.ResolveUDPAddr("udp", addr)
if err != nil {
log.Fatal().Msgf("can not start HTTP/3, resolve UDP: %v", err)
log.Fatal().Err(err).Msg("can not start HTTP/3, resolve UDP")
}
udpConn, err := net.ListenUDP("udp", udpAddr)
if err != nil {
log.Fatal().Msgf("can not start HTTP/3, listen UDP: %v", err)
log.Fatal().Err(err).Msg("can not start HTTP/3, listen UDP")
}
defer func() { _ = udpConn.Close() }()

tcpAddr, err := net.ResolveTCPAddr("tcp", addr)
if err != nil {
log.Fatal().Msgf("can not start HTTP/3, resolve TCP: %v", err)
log.Fatal().Err(err).Msg("can not start HTTP/3, resolve TCP")
}
tcpConn, err := net.ListenTCP("tcp", tcpAddr)
if err != nil {
log.Fatal().Msgf("can not start HTTP/3, listen TCP: %v", err)
log.Fatal().Err(err).Msg("can not start HTTP/3, listen TCP")
}
defer func() { _ = tcpConn.Close() }()

Expand All @@ -552,23 +552,23 @@ func runHTTPServers(
case err := <-hErr:
_ = wtServer.Close()
if !errors.Is(err, http.ErrServerClosed) {
log.Fatal().Msgf("ListenAndServe: %v", err)
log.Fatal().Err(err).Msg("error ListenAndServe")
}
case err := <-qErr:
// Cannot close the HTTP server or wait for requests to complete properly.
log.Fatal().Msgf("ListenAndServe HTTP/3: %v", err)
log.Fatal().Err(err).Msg("error ListenAndServe HTTP/3")
}
} else {
if addrTLSConfig != nil {
if err := server.ListenAndServeTLS("", ""); err != nil {
if !errors.Is(err, http.ErrServerClosed) {
log.Fatal().Msgf("ListenAndServe: %v", err)
log.Fatal().Err(err).Msg("error ListenAndServeTLS")
}
}
} else {
if err := server.ListenAndServe(); err != nil {
if !errors.Is(err, http.ErrServerClosed) {
log.Fatal().Msgf("ListenAndServe: %v", err)
log.Fatal().Err(err).Msgf("error ListenAndServe")
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/app/origin.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func getCheckOrigin(cfg config.Config) func(r *http.Request) bool {
}
originChecker, err := origin.NewPatternChecker(allowedOrigins)
if err != nil {
log.Fatal().Msgf("error creating origin checker: %v", err)
log.Fatal().Err(err).Msg("error creating origin checker")
}
if len(allowedOrigins) == 1 && allowedOrigins[0] == "*" {
// Fast path for *.
Expand Down
37 changes: 18 additions & 19 deletions internal/app/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const edition = "oss"
func Run(cmd *cobra.Command, configFile string) {
cfg, cfgMeta, err := config.GetConfig(cmd, configFile)
if err != nil {
log.Fatal().Msgf("error getting config: %v", err)
log.Fatal().Err(err).Msg("error getting config")
}

ctx, serviceCancel := context.WithCancel(context.Background())
Expand All @@ -59,7 +59,7 @@ func Run(cmd *cobra.Command, configFile string) {
}
err = tools.WritePidFile(cfg.PidFile)
if err != nil {
log.Fatal().Msgf("error writing PID: %v", err)
log.Fatal().Err(err).Msg("error writing PID")
}
_, _ = maxprocs.Set(maxprocs.Logger(func(s string, i ...interface{}) {
log.Info().Msgf(strings.ToLower(s), i...)
Expand Down Expand Up @@ -88,65 +88,64 @@ func Run(cmd *cobra.Command, configFile string) {

err = cfg.Validate()
if err != nil {
log.Fatal().Msgf("error validating config: %v", err)
log.Fatal().Err(err).Msg("error validating config")
}
cfgContainer, err := config.NewContainer(cfg)
if err != nil {
log.Fatal().Msgf("error creating config: %v", err)
log.Fatal().Err(err).Msg("error creating config")
}
cfgContainer.ChannelOptionsCacheTTL = 200 * time.Millisecond

proxyMap, keepHeadersInContext, err := buildProxyMap(cfg)
if err != nil {
log.Fatal().Msgf("error building proxy map: %v", err)
log.Fatal().Err(err).Msg("error building proxy map")
}

nodeCfg := centrifugeNodeConfig(build.Version, cfgContainer, centrifugeLogHandler)

node, err := centrifuge.New(nodeCfg)
if err != nil {
log.Fatal().Msgf("error creating Centrifuge Node: %v", err)
log.Fatal().Err(err).Msg("error creating Centrifuge Node")
}

if cfg.OpenTelemetry.Enabled {
_, err := telemetry.SetupTracing(context.Background())
if err != nil {
log.Fatal().Msgf("error setting up opentelemetry tracing: %v", err)
log.Fatal().Err(err).Msg("error setting up opentelemetry tracing")
}
}

modes, err := configureEngines(node, cfgContainer)
if err != nil {
log.Fatal().Msgf("%v", err)
log.Fatal().Err(err).Msg("configure engines error")
}

verifierConfig, err := confighelpers.MakeVerifierConfig(cfg.Client.Token)
if err != nil {
log.Fatal().Msgf("error creating JWT verifier config: %v", err)
log.Fatal().Err(err).Msg("error creating JWT verifier config")
}

tokenVerifier, err := jwtverify.NewTokenVerifierJWT(verifierConfig, cfgContainer)
if err != nil {
log.Fatal().Msgf("error creating token verifier: %v", err)
log.Fatal().Err(err).Msg("error creating token verifier")
}

var subTokenVerifier *jwtverify.VerifierJWT
if cfg.Client.SubscriptionToken.Enabled {
log.Info().Msg("initializing separate verifier for subscription tokens")
subVerifier, err := confighelpers.MakeVerifierConfig(cfg.Client.SubscriptionToken.Token)
if err != nil {
log.Fatal().Msgf("error creating subscription JWT verifier config: %v", err)
log.Fatal().Err(err).Msg("error creating subscription JWT verifier config")
}
subTokenVerifier, err = jwtverify.NewTokenVerifierJWT(subVerifier, cfgContainer)
if err != nil {
log.Fatal().Msgf("error creating token verifier: %v", err)
log.Fatal().Err(err).Msg("error creating token verifier")
}
}

clientHandler := client.NewHandler(node, cfgContainer, tokenVerifier, subTokenVerifier, proxyMap)
err = clientHandler.Setup()
if err != nil {
log.Fatal().Msgf("error setting up client handler: %v", err)
log.Fatal().Err(err).Msg("error setting up client handler")
}
if cfg.RPC.Ping {
log.Info().Str("method", cfg.RPC.PingMethod).Msg("RPC ping extension enabled")
Expand Down Expand Up @@ -179,7 +178,7 @@ func Run(cmd *cobra.Command, configFile string) {

consumingServices, err := consuming.New(node.ID(), consumingHandler, cfg.Consumers)
if err != nil {
log.Fatal().Msgf("error initializing consumers: %v", err)
log.Fatal().Err(err).Msg("error initializing consumers")
}

serviceManager.Register(consumingServices...)
Expand Down Expand Up @@ -236,7 +235,7 @@ func Run(cmd *cobra.Command, configFile string) {
notify.RegisterHandlers(node, statsSender)

if err = node.Run(); err != nil {
log.Fatal().Msgf("error running node: %v", err)
log.Fatal().Err(err).Msg("error running node")
}

serviceManager.Run(ctx)
Expand All @@ -246,7 +245,7 @@ func Run(cmd *cobra.Command, configFile string) {
var err error
grpcAPIServer, err = runGRPCAPIServer(cfg, node, useAPIOpentelemetry, grpcAPIExecutor)
if err != nil {
log.Fatal().Msgf("error creating GRPC API server: %v", err)
log.Fatal().Err(err).Msg("error creating GRPC API server")
}
}

Expand All @@ -255,13 +254,13 @@ func Run(cmd *cobra.Command, configFile string) {
var err error
grpcAPIServer, err = runGRPCUniServer(cfg, node)
if err != nil {
log.Fatal().Msgf("error creating GRPC API server: %v", err)
log.Fatal().Err(err).Msg("error creating GRPC API server")
}
}

httpServers, err := runHTTPServers(node, cfgContainer, httpAPIExecutor, keepHeadersInContext)
if err != nil {
log.Fatal().Msgf("error running HTTP server: %v", err)
log.Fatal().Err(err).Msg("error running HTTP server")
}

logStartWarnings(cfg, cfgMeta)
Expand Down
2 changes: 1 addition & 1 deletion internal/app/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func GetTLSConfig(cfg config.Config) (*tls.Config, error) {
go func() {
log.Info().Msgf("serving ACME http_01 challenge on %s", tlsAutocertHTTPAddr)
if err := acmeHTTPServer.ListenAndServe(); err != nil {
log.Fatal().Msgf("can't create server on %s to serve acme http challenge: %v", tlsAutocertHTTPAddr, err)
log.Fatal().Err(err).Msgf("can't create server on %s to serve acme http challenge", tlsAutocertHTTPAddr)
}
}()
})
Expand Down
6 changes: 4 additions & 2 deletions internal/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"github.com/rs/zerolog/log"
)

type Level = zerolog.Level

const (
TraceLevel = zerolog.TraceLevel
DebugLevel = zerolog.DebugLevel
Expand Down Expand Up @@ -119,7 +121,7 @@ func Setup(cfg config.Config) (centrifuge.LogHandler, func()) {
if cfg.LogFile != "" {
f, err := os.OpenFile(cfg.LogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
log.Fatal().Msgf("error opening log file: %v", err)
log.Fatal().Err(err).Msg("error opening log file")
}
log.Logger = log.Output(f)
return newCentrifugeLogHandler().Handle, func() {
Expand All @@ -130,6 +132,6 @@ func Setup(cfg config.Config) (centrifuge.LogHandler, func()) {
}

// Enabled checks if a specific logging level is enabled
func Enabled(level zerolog.Level) bool {
func Enabled(level Level) bool {
return level >= zerolog.GlobalLevel()
}
3 changes: 1 addition & 2 deletions internal/usage/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ import (
"sync"
"time"

"github.com/rs/zerolog/log"

"github.com/centrifugal/centrifugo/v5/internal/build"
"github.com/centrifugal/centrifugo/v5/internal/config"
"github.com/centrifugal/centrifugo/v5/internal/configtypes"
"github.com/centrifugal/centrifugo/v5/internal/consuming"

"github.com/centrifugal/centrifuge"
"github.com/rs/zerolog/log"
)

var statsRand *rand.Rand
Expand Down

0 comments on commit 8dfa2e6

Please sign in to comment.