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

Example code doesn't gracefully shutdown #7843

Open
bkane-msft opened this issue Nov 14, 2024 · 3 comments
Open

Example code doesn't gracefully shutdown #7843

bkane-msft opened this issue Nov 14, 2024 · 3 comments
Assignees
Labels
Area: Documentation Includes examples and docs. Type: Documentation Documentation or examples

Comments

@bkane-msft
Copy link

bkane-msft commented Nov 14, 2024

What version of gRPC are you using?

https://github.com/grpc/grpc-go/blob/89737ae09d06cf745ebeb3c414a83c94b58709e5/examples/helloworld/greeter_server/main.go

What version of Go are you using (go version)?

n/a

What operating system (Linux, Windows, …) and version?

n/a

What did you do?

I looked to see a good example for graceful shutdown

What did you expect to see?

The example ( https://github.com/grpc/grpc-go/blob/89737ae09d06cf745ebeb3c414a83c94b58709e5/examples/helloworld/greeter_server/main.go ) to show graceful shutdown on a signal

What did you see instead?

The following:

	s := grpc.NewServer()
	pb.RegisterGreeterServer(s, &server{})
	log.Printf("server listening at %v", lis.Addr())
	if err := s.Serve(lis); err != nil {
		log.Fatalf("failed to serve: %v", err)
	}

There is also nothing in the Go docs ( https://grpc.io/docs/languages/go/basics/#starting-the-server ) about draining requests / etc on shutdown or signal.

I think this should be in the docs/example as every app needs this. Would you accept a PR to add it?

@bkane-msft
Copy link
Author

I think the correct code is:

	// graceful stop
	ch := make(chan os.Signal, 1)
	signal.Notify(ch, os.Interrupt, syscall.SIGTERM)
	go func() {
		<-ch
		server.GracefulStop()
	}()

	// startup
	lis, err := net.Listen("tcp", ":27166")
	if err != nil {
		panic(fmt.Sprintf("failed to listen: %v", err))
	}
	slog.Info("server listening", "addr", lis.Addr())
	if err := server.Serve(lis); err != nil {
		panic(fmt.Sprintf("failed to serve: %v", err))
	}

but I'm new to gRPC and would like an authoritative example

@arjan-bal arjan-bal added Type: Documentation Documentation or examples and removed Type: Bug labels Nov 15, 2024
@arjan-bal arjan-bal self-assigned this Nov 15, 2024
@purnesh42H purnesh42H added the Area: Documentation Includes examples and docs. label Nov 18, 2024
@purnesh42H
Copy link
Contributor

@bkane-msft yes, GracefulStop() https://github.com/grpc/grpc-go/blob/master/server.go#L1901 is the correct way of stopping the gRPC server gracefully. We don't have any official documentation mentioning it though. I will ask the other maintainers if there is any and if not, should we add it.

@purnesh42H
Copy link
Contributor

purnesh42H commented Nov 22, 2024

@bkane-msft we have decided to add a separate example to demonstrate graceful stop for server

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Documentation Includes examples and docs. Type: Documentation Documentation or examples
Projects
None yet
Development

No branches or pull requests

3 participants