Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix server shutdown race #2958

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions services/horizon/internal/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (a *App) Serve() {
}()
go a.waitForDone()

err := a.webServer.Serve(uint16(a.config.Port), a.config.TLSCert, a.config.TLSKey, uint16(a.config.AdminPort))
err := a.webServer.Serve()
if err != nil && err != http.ErrServerClosed {
log.Fatal(err)
}
Expand Down Expand Up @@ -466,7 +466,8 @@ func (a *App) init() error {
}

var err error
a.webServer, err = httpx.NewServer(webConfig)
a.webServer, err = httpx.NewServer(
webConfig, uint16(a.config.Port), a.config.TLSCert, a.config.TLSKey, uint16(a.config.AdminPort))
2opremio marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
Expand Down
59 changes: 32 additions & 27 deletions services/horizon/internal/httpx/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@ type ServerMetrics struct {
// Web contains the http server related fields for horizon: the Router,
// rate limiter, etc.
type Server struct {
Router *Router
Metrics *ServerMetrics
server *http.Server
Router *Router
Metrics *ServerMetrics
server *http.Server
tlsFiles *struct {
certFile, keyFile string
}
internalServer *http.Server
sync.RWMutex
2opremio marked this conversation as resolved.
Show resolved Hide resolved
}

func init() {
Expand All @@ -46,7 +50,7 @@ func init() {
problem.RegisterError(db.ErrCancelled, hProblem.ServiceUnavailable)
}

func NewServer(config *RouterConfig) (*Server, error) {
func NewServer(config *RouterConfig, port uint16, certFile, keyFile string, adminPort uint16) (*Server, error) {
sm := &ServerMetrics{
RequestDurationSummary: prometheus.NewSummaryVec(
prometheus.SummaryOpts{
Expand All @@ -56,47 +60,48 @@ func NewServer(config *RouterConfig) (*Server, error) {
[]string{"status", "route", "streaming", "method"},
),
}

router, err := NewRouter(config, sm)
if err != nil {
return nil, err
}
addr := fmt.Sprintf(":%d", port)
result := &Server{
Router: router,
Metrics: sm,
server: &http.Server{
Addr: addr,
Handler: router,
ReadTimeout: 5 * time.Second,
},
}
return result, nil
}
func (s *Server) Serve(port uint16, certFile, keyFile string, adminPort uint16) error {
if s.server != nil {
return errors.New("server already started")
if certFile != "" && keyFile != "" {
result.tlsFiles = &struct {
certFile, keyFile string
}{keyFile, certFile}
}

if adminPort != 0 {
adminAddr := fmt.Sprintf(":%d", adminPort)
result.internalServer = &http.Server{
Addr: adminAddr,
Handler: result.Router.Internal,
ReadTimeout: 5 * time.Second,
}
}
return result, nil
}
func (s *Server) Serve() error {
if s.internalServer != nil {
go func() {
adminAddr := fmt.Sprintf(":%d", adminPort)
log.Infof("Starting internal server on %s", adminAddr)
s.internalServer = &http.Server{
Addr: adminAddr,
Handler: s.Router.Internal,
ReadTimeout: 5 * time.Second,
}
log.Infof("Starting internal server on %s", s.internalServer.Addr)
if err := s.internalServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Warn(errors.Wrap(err, "error in internalServer.ListenAndServe()"))
}
}()
}

addr := fmt.Sprintf(":%d", port)
s.server = &http.Server{
Addr: addr,
Handler: s.Router,
ReadTimeout: 5 * time.Second,
}

var err error
if certFile != "" {
err = s.server.ListenAndServeTLS(certFile, keyFile)
if s.tlsFiles != nil {
err = s.server.ListenAndServeTLS(s.tlsFiles.certFile, s.tlsFiles.keyFile)
} else {
err = s.server.ListenAndServe()
}
Expand Down