google.golang.org/grpc@v1.72.2/interop/alts/client/client.go (about) 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 // This binary can only run on Google Cloud Platform (GCP). 20 package main 21 22 import ( 23 "context" 24 "flag" 25 "time" 26 27 "google.golang.org/grpc" 28 "google.golang.org/grpc/credentials/alts" 29 "google.golang.org/grpc/grpclog" 30 31 testgrpc "google.golang.org/grpc/interop/grpc_testing" 32 testpb "google.golang.org/grpc/interop/grpc_testing" 33 ) 34 35 var ( 36 hsAddr = flag.String("alts_handshaker_service_address", "", "ALTS handshaker gRPC service address") 37 serverAddr = flag.String("server_address", ":8080", "The port on which the server is listening") 38 39 logger = grpclog.Component("interop") 40 ) 41 42 func main() { 43 flag.Parse() 44 45 opts := alts.DefaultClientOptions() 46 if *hsAddr != "" { 47 opts.HandshakerServiceAddress = *hsAddr 48 } 49 altsTC := alts.NewClientCreds(opts) 50 conn, err := grpc.NewClient(*serverAddr, grpc.WithTransportCredentials(altsTC)) 51 if err != nil { 52 logger.Fatalf("grpc.NewClient(%q) = %v", *serverAddr, err) 53 } 54 defer conn.Close() 55 grpcClient := testgrpc.NewTestServiceClient(conn) 56 57 // Call the EmptyCall API. 58 ctx := context.Background() 59 request := &testpb.Empty{} 60 // Block until the server is ready. 61 if _, err := grpcClient.EmptyCall(ctx, request, grpc.WaitForReady(true)); err != nil { 62 logger.Fatalf("grpc Client: EmptyCall(_, %v) failed: %v", request, err) 63 } 64 logger.Info("grpc Client: empty call succeeded") 65 66 // This sleep prevents the connection from being abruptly disconnected 67 // when running this binary (along with grpc_server) on GCP dev cluster. 68 time.Sleep(1 * time.Second) 69 }