MCPcopy
hub / github.com/grpc/grpc-go / main

Function main

examples/features/gracefulstop/server/main.go:76–117  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

74}
75
76func 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 serverStopped := make(chan struct{}, 1)
89
90 go func() {
91 <-ss.streamStart // wait until server streaming starts
92 time.Sleep(1 * time.Second)
93 log.Println("Initiating graceful shutdown...")
94 timer := time.AfterFunc(10*time.Second, func() {
95 log.Println("Server couldn't stop gracefully in time. Doing force stop.")
96 s.Stop()
97 // Unblock the main function.
98 select {
99 case serverStopped <- struct{}{}:
100 default:
101 }
102 })
103 defer timer.Stop()
104 s.GracefulStop() // gracefully stop server after in-flight server streaming rpc finishes
105 log.Println("Server stopped gracefully.")
106 // Unblock the main function.
107 select {
108 case serverStopped <- struct{}{}:
109 default:
110 }
111 }()
112
113 if err := s.Serve(lis); err != nil {
114 log.Fatalf("failed to serve: %v", err)
115 }
116 <-serverStopped
117}

Callers

nothing calls this directly

Calls 8

StopMethod · 0.95
GracefulStopMethod · 0.95
ServeMethod · 0.95
NewServerFunction · 0.92
PrintlnMethod · 0.80
ParseMethod · 0.65
FatalfMethod · 0.65
StopMethod · 0.65

Tested by

no test coverage detected