Skip to content

Commit

Permalink
fix: ensure proper application reload on configuration change and reg…
Browse files Browse the repository at this point in the history
…ister Prometheus metrics once
  • Loading branch information
armin committed Aug 19, 2024
1 parent eca5b17 commit 096b00e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
9 changes: 6 additions & 3 deletions api/v2/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ type customValidator struct {
validator *validator.Validate
}

var registerMetricsMiddleware sync.Once

func (cv *customValidator) Validate(i interface{}) error {
if err := cv.validator.Struct(i); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
Expand Down Expand Up @@ -64,9 +66,10 @@ func V2() (*echo.Echo, error) {

e.Validator = &customValidator{validator: validator.New()}

// Prometheus middleware
e.Use(echoprometheus.NewMiddleware("myapp"))
e.GET("/metrics", echoprometheus.NewHandler())
registerMetricsMiddleware.Do(func() {
e.Use(echoprometheus.NewMiddleware("myapp"))
e.GET("/metrics", echoprometheus.NewHandler())
})

s := &server{}
l := linkedlist.NewLinkedList()
Expand Down
4 changes: 2 additions & 2 deletions config/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ var MapLevel = map[string]slog.Level{
"ERROR": slog.LevelError,
}

var Confs config
var Confs Config

type config struct {
type Config struct {
Server server `yaml:"server"`
Logger logger `yaml:"logger"`
}
Expand Down
28 changes: 22 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,24 @@ func run(ctx context.Context) (*api.Api, error) {

server, err := api.New()
if err != nil {
slog.Error("Bootin api", "error", err)
slog.Error("Booting api", "error", err)
return nil, err
}

go func() {
if err := server.Start(ctx); err != nil && !errors.Is(err, http.ErrServerClosed) {
slog.Error("Starting server", "errror", err)
slog.Error("Starting server", "error", err)
os.Exit(1)
}
}()

return server, nil
}

func configChanged(oldConfig *config.Config) bool {
return oldConfig.Server.Port != config.Confs.Server.Port || oldConfig.Logger.Level != config.Confs.Logger.Level
}

func main() {
flag.Parse()

Expand All @@ -65,10 +69,23 @@ func main() {
sig := <-sigs
switch sig {
case syscall.SIGHUP:
slog.Info("Received SIGHUP, reloading configuration...")
slog.Info("Received SIGHUP, checking configuration...")

err = server.Shutdown(ctx)
if err != nil {
oldConfig := config.Confs

if err := config.Load(*cfg); err != nil {
slog.Error("Reading new configuration", "error", err)
continue
}

if !configChanged(&oldConfig) {
slog.Info("Configuration unchanged, no reload needed.")
continue
}

slog.Info("Configuration has changed, reloading server...")

if err = server.Shutdown(ctx); err != nil {
slog.Error("could not stop server", "error", err)
continue
}
Expand All @@ -87,5 +104,4 @@ func main() {
os.Exit(0)
}
}

}

0 comments on commit 096b00e

Please sign in to comment.