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

coordinator: correct shutdown, report serve errors #779

Merged
merged 1 commit into from
Aug 1, 2024
Merged
Changes from all 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
25 changes: 22 additions & 3 deletions coordinator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package main

import (
"context"
"errors"
"fmt"
"log/slog"
"net"
Expand Down Expand Up @@ -38,6 +39,9 @@ func main() {
}

func run() (retErr error) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

logger, err := logger.Default()
if err != nil {
fmt.Fprintf(os.Stderr, "Error: creating logger: %v\n", err)
Expand Down Expand Up @@ -72,8 +76,10 @@ func run() (retErr error) {

userapi.RegisterUserAPIServer(grpcServer, meshAuth)
serverMetrics.InitializeMetrics(grpcServer)
meshAPI := newMeshAPIServer(meshAuth, meshAuth, promRegistry, serverMetrics, logger)
metricsServer := &http.Server{}

eg := errgroup.Group{}
eg, ctx := errgroup.WithContext(ctx)

eg.Go(func() error {
if metricsPort == "" {
Expand All @@ -90,7 +96,10 @@ func run() (retErr error) {
promhttp.HandlerOpts{Registry: promRegistry},
),
))
if err := http.ListenAndServe(":"+metricsPort, mux); err != nil {
metricsServer.Addr = ":" + metricsPort
metricsServer.Handler = mux
if err := metricsServer.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
logger.Error("Serving Prometheus /metrics endpoint", "err", err)
return fmt.Errorf("serving Prometheus endpoint: %w", err)
}
return nil
Expand All @@ -103,20 +112,30 @@ func run() (retErr error) {
return fmt.Errorf("failed to listen: %w", err)
}
if err := grpcServer.Serve(lis); err != nil {
logger.Error("Serving Coordinator API", "err", err)
return fmt.Errorf("serving Coordinator API: %w", err)
}
return nil
})

eg.Go(func() error {
meshAPI := newMeshAPIServer(meshAuth, meshAuth, promRegistry, serverMetrics, logger)
logger.Info("Coordinator mesh API listening")
if err := meshAPI.Serve(net.JoinHostPort("0.0.0.0", meshapi.Port)); err != nil {
logger.Error("Serving mesh API", "err", err)
return fmt.Errorf("serving mesh API: %w", err)
}
return nil
})

eg.Go(func() error {
<-ctx.Done()
logger.Info("Error detected, shutting down")
grpcServer.GracefulStop()
meshAPI.grpc.GracefulStop()
//nolint:contextcheck // fresh context for cleanup
return metricsServer.Shutdown(context.Background())
})

return eg.Wait()
}

Expand Down