...

Source file src/google.golang.org/grpc/interop/alts/client/client.go

Documentation: google.golang.org/grpc/interop/alts/client

     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  	// Block until the server is ready.
    51  	conn, err := grpc.Dial(*serverAddr, grpc.WithTransportCredentials(altsTC), grpc.WithBlock())
    52  	if err != nil {
    53  		logger.Fatalf("gRPC Client: failed to dial the server at %v: %v", *serverAddr, err)
    54  	}
    55  	defer conn.Close()
    56  	grpcClient := testgrpc.NewTestServiceClient(conn)
    57  
    58  	// Call the EmptyCall API.
    59  	ctx := context.Background()
    60  	request := &testpb.Empty{}
    61  	if _, err := grpcClient.EmptyCall(ctx, request); 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  }
    70  

View as plain text