You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

69 lines
1.7 KiB

  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. "log"
  24. "os"
  25. "time"
  26. epb "google.golang.org/genproto/googleapis/rpc/errdetails"
  27. "google.golang.org/grpc"
  28. pb "google.golang.org/grpc/examples/helloworld/helloworld"
  29. "google.golang.org/grpc/status"
  30. )
  31. var addr = flag.String("addr", "localhost:50052", "the address to connect to")
  32. func main() {
  33. flag.Parse()
  34. // Set up a connection to the server.
  35. conn, err := grpc.Dial(*addr, grpc.WithInsecure())
  36. if err != nil {
  37. log.Fatalf("did not connect: %v", err)
  38. }
  39. defer func() {
  40. if e := conn.Close(); e != nil {
  41. log.Printf("failed to close connection: %s", e)
  42. }
  43. }()
  44. c := pb.NewGreeterClient(conn)
  45. ctx, cancel := context.WithTimeout(context.Background(), time.Second)
  46. defer cancel()
  47. r, err := c.SayHello(ctx, &pb.HelloRequest{Name: "world"})
  48. if err != nil {
  49. s := status.Convert(err)
  50. for _, d := range s.Details() {
  51. switch info := d.(type) {
  52. case *epb.QuotaFailure:
  53. log.Printf("Quota failure: %s", info)
  54. default:
  55. log.Printf("Unexpected type: %s", info)
  56. }
  57. }
  58. os.Exit(1)
  59. }
  60. log.Printf("Greeting: %s", r.Message)
  61. }