When the server exits (^C or because docker/systemd exits it), streaming gRPC clients must be

closed. Currently, the server does not exit until the gRPC client disconnects.
This commit is contained in:
2026-04-11 02:18:44 +02:00
parent 7ad183320c
commit d8ad89d115
3 changed files with 50 additions and 17 deletions

View File

@@ -17,12 +17,15 @@ import (
// Server implements the MaglevServer gRPC interface.
type Server struct {
UnimplementedMaglevServer
ctx context.Context
checker *checker.Checker
}
// NewServer creates a Server backed by the given Checker.
func NewServer(c *checker.Checker) *Server {
return &Server{checker: c}
// NewServer creates a Server backed by the given Checker. The provided context
// controls the lifetime of streaming RPCs: cancelling it closes all active
// WatchBackendEvents streams so that grpc.Server.GracefulStop can complete.
func NewServer(ctx context.Context, c *checker.Checker) *Server {
return &Server{ctx: ctx, checker: c}
}
// ListFrontends returns the names of all configured frontends.
@@ -112,6 +115,8 @@ func (s *Server) WatchBackendEvents(_ *WatchRequest, stream Maglev_WatchBackendE
for {
select {
case <-s.ctx.Done():
return status.Error(codes.Unavailable, "server shutting down")
case <-stream.Context().Done():
return nil
case e, ok := <-ch: