Skip to content

Commit

Permalink
Handle errors when stopping the listener for gRPC server
Browse files Browse the repository at this point in the history
Graceful shutdown of gRPC server
  • Loading branch information
mostafa committed Dec 26, 2024
1 parent 73afdd2 commit e1f48e2
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 6 deletions.
8 changes: 5 additions & 3 deletions api/grpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ func (s *GRPCServer) Start() {
}

// Shutdown shuts down the gRPC server.
func (s *GRPCServer) Shutdown(_ context.Context) {
s.listener.Close()
s.grpcServer.Stop()
func (s *GRPCServer) Shutdown(context.Context) {
if err := s.listener.Close(); err != nil && !errors.Is(err, net.ErrClosed) {
s.API.Options.Logger.Err(err).Msg("failed to close listener")
}
s.grpcServer.GracefulStop()
}

// createGRPCAPI creates a new gRPC API server and listener.
Expand Down
2 changes: 1 addition & 1 deletion api/grpc_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ func Test_GRPC_Server(t *testing.T) {
assert.Equal(t, config.Version, resp.GetVersion())
assert.Equal(t, config.VersionInfo(), resp.GetVersionInfo())

grpcServer.Shutdown(context.Background())
grpcServer.Shutdown(nil)

Check failure on line 42 in api/grpc_server_test.go

View workflow job for this annotation

GitHub Actions / Test GatewayD

SA1012: do not pass a nil Context, even if a function permits it; pass context.TODO if you are unsure about which Context to use (staticcheck)
}
2 changes: 1 addition & 1 deletion api/http_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,6 @@ func Test_HTTP_Server(t *testing.T) {
assert.Equal(t, len(config.Version), len(respBodyBytes))
assert.Equal(t, config.Version, string(respBodyBytes))

grpcServer.Shutdown(context.Background())
grpcServer.Shutdown(nil)

Check failure on line 88 in api/http_server_test.go

View workflow job for this annotation

GitHub Actions / Test GatewayD

SA1012: do not pass a nil Context, even if a function permits it; pass context.TODO if you are unsure about which Context to use (staticcheck)
httpServer.Shutdown(context.Background())
}
2 changes: 1 addition & 1 deletion cmd/gatewayd_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -1109,7 +1109,7 @@ func (app *GatewayDApp) stopGracefully(runCtx context.Context, sig os.Signal) {
}

if app.grpcServer != nil {
app.grpcServer.Shutdown(runCtx)
app.grpcServer.Shutdown(nil)

Check failure on line 1112 in cmd/gatewayd_app.go

View workflow job for this annotation

GitHub Actions / Test GatewayD

Non-inherited new context, use function like `context.WithXXX` instead (contextcheck)
logger.Info().Msg("Stopped gRPC Server")
span.AddEvent("Stopped gRPC Server")
}
Expand Down

0 comments on commit e1f48e2

Please sign in to comment.