Skip to content

Commit

Permalink
add supportListener to split out metrics and profiling from the main …
Browse files Browse the repository at this point in the history
…listener

Signed-off-by: Sandor Szücs <[email protected]>
  • Loading branch information
szuecs authored and MustafaSaber committed Oct 27, 2023
1 parent 541eb2a commit 0dd3bbc
Showing 1 changed file with 32 additions and 8 deletions.
40 changes: 32 additions & 8 deletions routesrv/routesrv.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ import (
// RouteServer is used to serve eskip-formatted routes,
// that originate from the polled data source.
type RouteServer struct {
metrics metrics.Metrics
server *http.Server
poller *poller
wg *sync.WaitGroup
metrics metrics.Metrics
server *http.Server
supportServer *http.Server
poller *poller
wg *sync.WaitGroup
}

// New returns an initialized route server according to the passed options.
Expand Down Expand Up @@ -75,12 +76,13 @@ func New(opts skipper.Options) (*RouteServer, error) {
mux := http.NewServeMux()
mux.Handle("/health", bs)
mux.Handle("/routes", b)
mux.Handle("/metrics", metricsHandler)
mux.Handle("/metrics/", metricsHandler)
supportHandler := http.NewServeMux()
supportHandler.Handle("/metrics", metricsHandler)
supportHandler.Handle("/metrics/", metricsHandler)

if opts.EnableProfile {
mux.Handle("/debug/pprof", metricsHandler)
mux.Handle("/debug/pprof/", metricsHandler)
supportHandler.Handle("/debug/pprof", metricsHandler)
supportHandler.Handle("/debug/pprof/", metricsHandler)
}

dataclient, err := kubernetes.New(opts.KubernetesDataClientOptions())
Expand Down Expand Up @@ -113,6 +115,13 @@ func New(opts skipper.Options) (*RouteServer, error) {
ReadHeaderTimeout: 1 * time.Minute,
}

rs.supportServer = &http.Server{
Addr: opts.SupportListener,
Handler: supportHandler,
ReadTimeout: 1 * time.Minute,
ReadHeaderTimeout: 1 * time.Minute,
}

rs.poller = &poller{
client: dataclient,
timeout: opts.SourcePollTimeout,
Expand Down Expand Up @@ -149,6 +158,15 @@ func (rs *RouteServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
rs.server.Handler.ServeHTTP(w, r)
}

func (rs *RouteServer) startSupportListener() {
if rs.supportServer != nil {
err := rs.supportServer.ListenAndServe()
if err != nil {
log.Errorf("Failed support listener: %v", err)
}
}
}

func newShutdownFunc(rs *RouteServer) func(delay time.Duration) {
once := sync.Once{}
rs.wg.Add(1)
Expand Down Expand Up @@ -177,10 +195,16 @@ func run(rs *RouteServer, opts skipper.Options, sigs chan os.Signal) error {
go func() {
<-sigs
shutdown(opts.WaitForHealthcheckInterval)
if rs.supportServer != nil {
ctx, done := context.WithTimeout(context.Background(), 10*time.Second)
defer done()
rs.supportServer.Shutdown(ctx)
}
}()

rs.StartUpdates()

go rs.startSupportListener()
if err = rs.server.ListenAndServe(); err != http.ErrServerClosed {
go shutdown(0)
} else {
Expand Down

0 comments on commit 0dd3bbc

Please sign in to comment.