Skip to content

Commit

Permalink
fix(server/v2): avoid server stop get call before start for multi com…
Browse files Browse the repository at this point in the history
…ponents (#22811)
  • Loading branch information
mmsqe authored Dec 10, 2024
1 parent e07fee5 commit cb26ce3
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 26 deletions.
14 changes: 6 additions & 8 deletions server/v2/api/grpcgateway/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,13 @@ func New[T transaction.Tx](

s.logger = logger.With(log.ModuleKey, s.Name())
s.config = serverCfg
mux := http.NewServeMux()
mux.Handle("/", s.GRPCGatewayRouter)

s.server = &http.Server{
Addr: s.config.Address,
Handler: mux,
}
return s, nil
}

Expand Down Expand Up @@ -110,14 +116,6 @@ func (s *Server[T]) Start(ctx context.Context) error {
return nil
}

mux := http.NewServeMux()
mux.Handle("/", s.GRPCGatewayRouter)

s.server = &http.Server{
Addr: s.config.Address,
Handler: mux,
}

s.logger.Info("starting gRPC-Gateway server...", "address", s.config.Address)
if err := s.server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
return fmt.Errorf("failed to start gRPC-Gateway server: %w", err)
Expand Down
10 changes: 4 additions & 6 deletions server/v2/api/rest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ func New[T transaction.Tx](
}
}
srv.config = serverCfg

srv.httpServer = &http.Server{
Addr: srv.config.Address,
Handler: srv.router,
}
return srv, nil
}

Expand All @@ -69,11 +72,6 @@ func (s *Server[T]) Start(ctx context.Context) error {
return nil
}

s.httpServer = &http.Server{
Addr: s.config.Address,
Handler: s.router,
}

s.logger.Info("starting HTTP server", "address", s.config.Address)
if err := s.httpServer.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
s.logger.Error("failed to start HTTP server", "error", err)
Expand Down
23 changes: 11 additions & 12 deletions server/v2/api/telemetry/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ func New[T transaction.Tx](cfg server.ConfigMap, logger log.Logger, enableTeleme
return nil, fmt.Errorf("failed to initialize metrics: %w", err)
}
srv.metrics = metrics
mux := http.NewServeMux()
// /metrics is the default standard path for Prometheus metrics.
mux.HandleFunc("/metrics", srv.metricsHandler)
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/metrics", http.StatusMovedPermanently)
})

srv.server = &http.Server{
Addr: srv.config.Address,
Handler: mux,
}
return srv, nil
}

Expand All @@ -74,18 +85,6 @@ func (s *Server[T]) Start(ctx context.Context) error {
return nil
}

mux := http.NewServeMux()
// /metrics is the default standard path for Prometheus metrics.
mux.HandleFunc("/metrics", s.metricsHandler)
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/metrics", http.StatusMovedPermanently)
})

s.server = &http.Server{
Addr: s.config.Address,
Handler: mux,
}

s.logger.Info("starting telemetry server...", "address", s.config.Address)
if err := s.server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
return fmt.Errorf("failed to start telemetry server: %w", err)
Expand Down
1 change: 1 addition & 0 deletions systemtests/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,7 @@ func (s *SystemUnderTest) AddFullnode(t *testing.T, beforeStart ...func(nodeNumb
if tomlFile == "app.toml" && IsV2() {
file := filepath.Join(WorkDir, s.nodePath(nodeNumber), "config", tomlFile)
EditToml(file, func(doc *tomledit.Document) {
SetValue(doc, fmt.Sprintf("%s:%d", node.IP, DefaultApiPort+nodeNumber), "grpc-gateway", "address")
SetValue(doc, fmt.Sprintf("%s:%d", node.IP, DefaultRestPort+nodeNumber), "rest", "address")
})
}
Expand Down

0 comments on commit cb26ce3

Please sign in to comment.