main.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 client is an example client.
  19. package main
  20. import (
  21. "context"
  22. "flag"
  23. "fmt"
  24. "log"
  25. "time"
  26. "google.golang.org/grpc"
  27. "google.golang.org/grpc/codes"
  28. pb "google.golang.org/grpc/examples/features/proto/echo"
  29. "google.golang.org/grpc/status"
  30. )
  31. var addr = flag.String("addr", "localhost:50052", "the address to connect to")
  32. func unaryCall(c pb.EchoClient, requestID int, message string, want codes.Code) {
  33. // Creates a context with a one second deadline for the RPC.
  34. ctx, cancel := context.WithTimeout(context.Background(), time.Second)
  35. defer cancel()
  36. req := &pb.EchoRequest{Message: message}
  37. _, err := c.UnaryEcho(ctx, req)
  38. got := status.Code(err)
  39. fmt.Printf("[%v] wanted = %v, got = %v\n", requestID, want, got)
  40. }
  41. func streamingCall(c pb.EchoClient, requestID int, message string, want codes.Code) {
  42. // Creates a context with a one second deadline for the RPC.
  43. ctx, cancel := context.WithTimeout(context.Background(), time.Second)
  44. defer cancel()
  45. stream, err := c.BidirectionalStreamingEcho(ctx)
  46. if err != nil {
  47. log.Printf("Stream err: %v", err)
  48. return
  49. }
  50. err = stream.Send(&pb.EchoRequest{Message: message})
  51. if err != nil {
  52. log.Printf("Send error: %v", err)
  53. return
  54. }
  55. _, err = stream.Recv()
  56. got := status.Code(err)
  57. fmt.Printf("[%v] wanted = %v, got = %v\n", requestID, want, got)
  58. }
  59. func main() {
  60. flag.Parse()
  61. conn, err := grpc.Dial(*addr, grpc.WithInsecure())
  62. if err != nil {
  63. log.Fatalf("did not connect: %v", err)
  64. }
  65. defer conn.Close()
  66. c := pb.NewEchoClient(conn)
  67. // A successful request
  68. unaryCall(c, 1, "world", codes.OK)
  69. // Exceeds deadline
  70. unaryCall(c, 2, "delay", codes.DeadlineExceeded)
  71. // A successful request with propagated deadline
  72. unaryCall(c, 3, "[propagate me]world", codes.OK)
  73. // Exceeds propagated deadline
  74. unaryCall(c, 4, "[propagate me][propagate me]world", codes.DeadlineExceeded)
  75. // Receives a response from the stream successfully.
  76. streamingCall(c, 5, "[propagate me]world", codes.OK)
  77. // Exceeds propagated deadline before receiving a response
  78. streamingCall(c, 6, "[propagate me][propagate me]world", codes.DeadlineExceeded)
  79. }