|
| 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