Skip to content

Commit 66ba4b2

Browse files
authored
examples/features/gracefulstop: add example to demonstrate server graceful stop (#7865)
1 parent adad26d commit 66ba4b2

File tree

4 files changed

+233
-0
lines changed

4 files changed

+233
-0
lines changed

examples/examples_test.sh

+3
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ EXAMPLES=(
7171
"features/orca"
7272
"features/retry"
7373
"features/unix_abstract"
74+
"features/gracefulstop"
7475
)
7576

7677
declare -A SERVER_ARGS=(
@@ -129,6 +130,7 @@ declare -A EXPECTED_SERVER_OUTPUT=(
129130
["features/retry"]="request succeeded count: 4"
130131
["features/unix_abstract"]="serving on @abstract-unix-socket"
131132
["features/advancedtls"]=""
133+
["features/gracefulstop"]="Server stopped gracefully."
132134
)
133135

134136
declare -A EXPECTED_CLIENT_OUTPUT=(
@@ -154,6 +156,7 @@ declare -A EXPECTED_CLIENT_OUTPUT=(
154156
["features/retry"]="UnaryEcho reply: message:\"Try and Success\""
155157
["features/unix_abstract"]="calling echo.Echo/UnaryEcho to unix-abstract:abstract-unix-socket"
156158
["features/advancedtls"]=""
159+
["features/gracefulstop"]="Successful unary requests processed by server and made by client are same."
157160
)
158161

159162
cd ./examples
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Graceful Stop
2+
3+
This example demonstrates how to gracefully stop a gRPC server using
4+
`Server.GracefulStop()`. The graceful shutdown process involves two key steps:
5+
6+
- Initiate `Server.GracefulStop()`. This function blocks until all currently
7+
running RPCs have completed. This ensures that in-flight requests are
8+
allowed to finish processing.
9+
10+
- It's crucial to call `Server.Stop()` with a timeout before calling
11+
`GracefulStop()`. This acts as a safety net, ensuring that the server
12+
eventually shuts down even if some in-flight RPCs don't complete within a
13+
reasonable timeframe. This prevents indefinite blocking.
14+
15+
## Try it
16+
17+
```
18+
go run server/main.go
19+
```
20+
21+
```
22+
go run client/main.go
23+
```
24+
25+
## Explanation
26+
27+
The server starts with a client streaming and unary request handler. When
28+
client streaming is started, client streaming handler signals the server to
29+
initiate graceful stop and waits for the stream to be closed or aborted. Until
30+
the`Server.GracefulStop()` is initiated, server will continue to accept unary
31+
requests. Once `Server.GracefulStop()` is initiated, server will not accept
32+
new unary requests.
33+
34+
Client will start the client stream to the server and starts making unary
35+
requests until receiving an error. Error will indicate that the server graceful
36+
shutdown is initiated so client will stop making further unary requests and
37+
closes the client stream.
38+
39+
Server and client will keep track of number of unary requests processed on
40+
their side. Once the client has successfully closed the stream, server returns
41+
the total number of unary requests processed as response. The number from
42+
stream response should be equal to the number of unary requests tracked by
43+
client. This indicates that server has processed all in-flight requests before
44+
shutting down.
45+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
*
3+
* Copyright 2024 gRPC authors.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
// Binary client demonstrates sending multiple requests to server and observe
19+
// graceful stop.
20+
package main
21+
22+
import (
23+
"context"
24+
"flag"
25+
"fmt"
26+
"log"
27+
"time"
28+
29+
"google.golang.org/grpc"
30+
"google.golang.org/grpc/credentials/insecure"
31+
pb "google.golang.org/grpc/examples/features/proto/echo"
32+
)
33+
34+
var addr = flag.String("addr", "localhost:50052", "the address to connect to")
35+
36+
func main() {
37+
flag.Parse()
38+
39+
conn, err := grpc.NewClient(*addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
40+
if err != nil {
41+
log.Fatalf("Failed to create new client: %v", err)
42+
}
43+
defer conn.Close()
44+
c := pb.NewEchoClient(conn)
45+
46+
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
47+
defer cancel()
48+
49+
// Start a client stream and keep calling the `c.UnaryEcho` until receiving
50+
// an error. Error will indicate that server graceful stop is initiated and
51+
// it won't accept any new requests.
52+
stream, err := c.ClientStreamingEcho(ctx)
53+
if err != nil {
54+
log.Fatalf("Error starting stream: %v", err)
55+
}
56+
57+
// Keep track of successful unary requests which can be compared later to
58+
// the successful unary requests reported by the server.
59+
unaryRequests := 0
60+
for {
61+
r, err := c.UnaryEcho(ctx, &pb.EchoRequest{Message: "Hello"})
62+
if err != nil {
63+
log.Printf("Error calling `UnaryEcho`. Server graceful stop initiated: %v", err)
64+
break
65+
}
66+
unaryRequests++
67+
time.Sleep(200 * time.Millisecond)
68+
log.Printf(r.Message)
69+
}
70+
log.Printf("Successful unary requests made by client: %d", unaryRequests)
71+
72+
r, err := stream.CloseAndRecv()
73+
if err != nil {
74+
log.Fatalf("Error closing stream: %v", err)
75+
}
76+
if fmt.Sprintf("%d", unaryRequests) != r.Message {
77+
log.Fatalf("Got %s successful unary requests processed from server, want: %d", r.Message, unaryRequests)
78+
}
79+
log.Printf("Successful unary requests processed by server and made by client are same.")
80+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
*
3+
* Copyright 2024 gRPC authors.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
// Binary server demonstrates how to gracefully stop a gRPC server.
19+
package main
20+
21+
import (
22+
"context"
23+
"errors"
24+
"flag"
25+
"fmt"
26+
"io"
27+
"log"
28+
"net"
29+
"sync/atomic"
30+
"time"
31+
32+
"google.golang.org/grpc"
33+
pb "google.golang.org/grpc/examples/features/proto/echo"
34+
)
35+
36+
var (
37+
port = flag.Int("port", 50052, "port number")
38+
)
39+
40+
type server struct {
41+
pb.UnimplementedEchoServer
42+
43+
unaryRequests atomic.Int32 // to track number of unary RPCs processed
44+
streamStart chan struct{} // to signal if server streaming started
45+
}
46+
47+
// ClientStreamingEcho implements the EchoService.ClientStreamingEcho method.
48+
// It signals the server that streaming has started and waits for the stream to
49+
// be done or aborted. If `io.EOF` is received on stream that means client
50+
// has successfully closed the stream using `stream.CloseAndRecv()`, so it
51+
// returns an `EchoResponse` with the total number of unary RPCs processed
52+
// otherwise, it returns the error indicating stream is aborted.
53+
func (s *server) ClientStreamingEcho(stream pb.Echo_ClientStreamingEchoServer) error {
54+
// Signal streaming start to initiate graceful stop which should wait until
55+
// server streaming finishes.
56+
s.streamStart <- struct{}{}
57+
58+
if err := stream.RecvMsg(&pb.EchoResponse{}); err != nil {
59+
if errors.Is(err, io.EOF) {
60+
stream.SendAndClose(&pb.EchoResponse{Message: fmt.Sprintf("%d", s.unaryRequests.Load())})
61+
return nil
62+
}
63+
return err
64+
}
65+
66+
return nil
67+
}
68+
69+
// UnaryEcho implements the EchoService.UnaryEcho method. It increments
70+
// `s.unaryRequests` on every call and returns it as part of `EchoResponse`.
71+
func (s *server) UnaryEcho(_ context.Context, req *pb.EchoRequest) (*pb.EchoResponse, error) {
72+
s.unaryRequests.Add(1)
73+
return &pb.EchoResponse{Message: req.Message}, nil
74+
}
75+
76+
func main() {
77+
flag.Parse()
78+
79+
address := fmt.Sprintf(":%v", *port)
80+
lis, err := net.Listen("tcp", address)
81+
if err != nil {
82+
log.Fatalf("failed to listen: %v", err)
83+
}
84+
85+
s := grpc.NewServer()
86+
ss := &server{streamStart: make(chan struct{})}
87+
pb.RegisterEchoServer(s, ss)
88+
89+
go func() {
90+
<-ss.streamStart // wait until server streaming starts
91+
time.Sleep(1 * time.Second)
92+
log.Println("Initiating graceful shutdown...")
93+
timer := time.AfterFunc(10*time.Second, func() {
94+
log.Println("Server couldn't stop gracefully in time. Doing force stop.")
95+
s.Stop()
96+
})
97+
defer timer.Stop()
98+
s.GracefulStop() // gracefully stop server after in-flight server streaming rpc finishes
99+
log.Println("Server stopped gracefully.")
100+
}()
101+
102+
if err := s.Serve(lis); err != nil {
103+
log.Fatalf("failed to serve: %v", err)
104+
}
105+
}

0 commit comments

Comments
 (0)