main.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. // Binary server is an example server.
  19. package main
  20. import (
  21. "context"
  22. "flag"
  23. "fmt"
  24. "io"
  25. "log"
  26. "net"
  27. "strings"
  28. "time"
  29. "google.golang.org/grpc"
  30. "google.golang.org/grpc/codes"
  31. pb "google.golang.org/grpc/examples/features/proto/echo"
  32. "google.golang.org/grpc/status"
  33. )
  34. var port = flag.Int("port", 50052, "port number")
  35. // server is used to implement EchoServer.
  36. type server struct {
  37. client pb.EchoClient
  38. cc *grpc.ClientConn
  39. }
  40. func (s *server) UnaryEcho(ctx context.Context, req *pb.EchoRequest) (*pb.EchoResponse, error) {
  41. message := req.Message
  42. if strings.HasPrefix(message, "[propagate me]") {
  43. time.Sleep(800 * time.Millisecond)
  44. message = strings.TrimPrefix(message, "[propagate me]")
  45. return s.client.UnaryEcho(ctx, &pb.EchoRequest{Message: message})
  46. }
  47. if message == "delay" {
  48. time.Sleep(1500 * time.Millisecond)
  49. }
  50. return &pb.EchoResponse{Message: req.Message}, nil
  51. }
  52. func (s *server) ServerStreamingEcho(req *pb.EchoRequest, stream pb.Echo_ServerStreamingEchoServer) error {
  53. return status.Error(codes.Unimplemented, "RPC unimplemented")
  54. }
  55. func (s *server) ClientStreamingEcho(stream pb.Echo_ClientStreamingEchoServer) error {
  56. return status.Error(codes.Unimplemented, "RPC unimplemented")
  57. }
  58. func (s *server) BidirectionalStreamingEcho(stream pb.Echo_BidirectionalStreamingEchoServer) error {
  59. for {
  60. req, err := stream.Recv()
  61. if err == io.EOF {
  62. return status.Error(codes.InvalidArgument, "request message not received")
  63. }
  64. if err != nil {
  65. return err
  66. }
  67. message := req.Message
  68. if strings.HasPrefix(message, "[propagate me]") {
  69. time.Sleep(800 * time.Millisecond)
  70. message = strings.TrimPrefix(message, "[propagate me]")
  71. res, err := s.client.UnaryEcho(stream.Context(), &pb.EchoRequest{Message: message})
  72. if err != nil {
  73. return err
  74. }
  75. stream.Send(res)
  76. }
  77. if message == "delay" {
  78. time.Sleep(1500 * time.Millisecond)
  79. }
  80. stream.Send(&pb.EchoResponse{Message: message})
  81. }
  82. }
  83. func (s *server) Close() {
  84. s.cc.Close()
  85. }
  86. func newEchoServer() *server {
  87. target := fmt.Sprintf("localhost:%v", *port)
  88. cc, err := grpc.Dial(target, grpc.WithInsecure())
  89. if err != nil {
  90. log.Fatalf("did not connect: %v", err)
  91. }
  92. return &server{client: pb.NewEchoClient(cc), cc: cc}
  93. }
  94. func main() {
  95. flag.Parse()
  96. address := fmt.Sprintf(":%v", *port)
  97. lis, err := net.Listen("tcp", address)
  98. if err != nil {
  99. log.Fatalf("failed to listen: %v", err)
  100. }
  101. echoServer := newEchoServer()
  102. defer echoServer.Close()
  103. grpcServer := grpc.NewServer()
  104. pb.RegisterEchoServer(grpcServer, echoServer)
  105. if err := grpcServer.Serve(lis); err != nil {
  106. log.Fatalf("failed to serve: %v", err)
  107. }
  108. }