|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright 2018 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 | + |
| 19 | +package main |
| 20 | + |
| 21 | +import ( |
| 22 | + "log" |
| 23 | + "net" |
| 24 | + "os" |
| 25 | + "os/signal" |
| 26 | + "time" |
| 27 | + |
| 28 | + "golang.org/x/net/context" |
| 29 | + "google.golang.org/grpc" |
| 30 | + "google.golang.org/grpc/channelz/service" |
| 31 | + pb "google.golang.org/grpc/examples/helloworld/helloworld" |
| 32 | + "google.golang.org/grpc/internal/grpcrand" |
| 33 | +) |
| 34 | + |
| 35 | +var ( |
| 36 | + ports = []string{":10001", ":10002", ":10003"} |
| 37 | +) |
| 38 | + |
| 39 | +// server is used to implement helloworld.GreeterServer. |
| 40 | +type server struct{} |
| 41 | + |
| 42 | +// SayHello implements helloworld.GreeterServer |
| 43 | +func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) { |
| 44 | + return &pb.HelloReply{Message: "Hello " + in.Name}, nil |
| 45 | +} |
| 46 | + |
| 47 | +// slow server is used to simulate a server that has a variable delay in its response. |
| 48 | +type slowServer struct{} |
| 49 | + |
| 50 | +// SayHello implements helloworld.GreeterServer |
| 51 | +func (s *slowServer) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) { |
| 52 | + // Delay 100ms ~ 200ms before replying |
| 53 | + time.Sleep(time.Duration(100+grpcrand.Intn(100)) * time.Millisecond) |
| 54 | + return &pb.HelloReply{Message: "Hello " + in.Name}, nil |
| 55 | +} |
| 56 | + |
| 57 | +func main() { |
| 58 | + /***** Set up the server serving channelz service. *****/ |
| 59 | + lis, err := net.Listen("tcp", ":50051") |
| 60 | + if err != nil { |
| 61 | + log.Fatalf("failed to listen: %v", err) |
| 62 | + } |
| 63 | + defer lis.Close() |
| 64 | + s := grpc.NewServer() |
| 65 | + service.RegisterChannelzServiceToServer(s) |
| 66 | + go s.Serve(lis) |
| 67 | + defer s.Stop() |
| 68 | + |
| 69 | + /***** Start three GreeterServers(with one of them to be the slowServer). *****/ |
| 70 | + var listeners []net.Listener |
| 71 | + var svrs []*grpc.Server |
| 72 | + for i := 0; i < 3; i++ { |
| 73 | + lis, err := net.Listen("tcp", ports[i]) |
| 74 | + if err != nil { |
| 75 | + log.Fatalf("failed to listen: %v", err) |
| 76 | + } |
| 77 | + listeners = append(listeners, lis) |
| 78 | + s := grpc.NewServer() |
| 79 | + svrs = append(svrs, s) |
| 80 | + if i == 2 { |
| 81 | + pb.RegisterGreeterServer(s, &slowServer{}) |
| 82 | + } else { |
| 83 | + pb.RegisterGreeterServer(s, &server{}) |
| 84 | + } |
| 85 | + go s.Serve(lis) |
| 86 | + } |
| 87 | + |
| 88 | + /***** Wait for CTRL+C to exit *****/ |
| 89 | + ch := make(chan os.Signal, 1) |
| 90 | + signal.Notify(ch, os.Interrupt) |
| 91 | + // Block until a signal is received. |
| 92 | + <-ch |
| 93 | + for i := 0; i < 3; i++ { |
| 94 | + svrs[i].Stop() |
| 95 | + listeners[i].Close() |
| 96 | + } |
| 97 | +} |
0 commit comments