gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/grpc/interop/fake_grpclb/fake_grpclb.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 file is for testing only. Runs a fake grpclb balancer server.
    20  // The name of the service to load balance for and the addresses
    21  // of that service are provided by command line flags.
    22  package main
    23  
    24  import (
    25  	"flag"
    26  	"net"
    27  	"strconv"
    28  	"strings"
    29  	"time"
    30  
    31  	grpc "gitee.com/ks-custle/core-gm/grpc"
    32  	lbpb "gitee.com/ks-custle/core-gm/grpc/balancer/grpclb/grpc_lb_v1"
    33  	"gitee.com/ks-custle/core-gm/grpc/codes"
    34  	"gitee.com/ks-custle/core-gm/grpc/credentials"
    35  	"gitee.com/ks-custle/core-gm/grpc/credentials/alts"
    36  	"gitee.com/ks-custle/core-gm/grpc/grpclog"
    37  	"gitee.com/ks-custle/core-gm/grpc/status"
    38  	"gitee.com/ks-custle/core-gm/grpc/testdata"
    39  )
    40  
    41  var (
    42  	port         = flag.Int("port", 10000, "Port to listen on.")
    43  	backendAddrs = flag.String("backend_addrs", "", "Comma separated list of backend IP/port addresses.")
    44  	useALTS      = flag.Bool("use_alts", false, "Listen on ALTS credentials.")
    45  	useTLS       = flag.Bool("use_tls", false, "Listen on TLS credentials, using a test certificate.")
    46  	shortStream  = flag.Bool("short_stream", false, "End the balancer stream immediately after sending the first server list.")
    47  	serviceName  = flag.String("service_name", "UNSET", "Name of the service being load balanced for.")
    48  
    49  	logger = grpclog.Component("interop")
    50  )
    51  
    52  type loadBalancerServer struct {
    53  	lbpb.UnimplementedLoadBalancerServer
    54  	serverListResponse *lbpb.LoadBalanceResponse
    55  }
    56  
    57  func (l *loadBalancerServer) BalanceLoad(stream lbpb.LoadBalancer_BalanceLoadServer) error {
    58  	logger.Info("Begin handling new BalancerLoad request.")
    59  	var lbReq *lbpb.LoadBalanceRequest
    60  	var err error
    61  	if lbReq, err = stream.Recv(); err != nil {
    62  		logger.Errorf("Error receiving LoadBalanceRequest: %v", err)
    63  		return err
    64  	}
    65  	logger.Info("LoadBalancerRequest received.")
    66  	initialReq := lbReq.GetInitialRequest()
    67  	if initialReq == nil {
    68  		logger.Info("Expected first request to be an InitialRequest. Got: %v", lbReq)
    69  		return status.Error(codes.Unknown, "First request not an InitialRequest")
    70  	}
    71  	// gRPC clients targeting foo.bar.com:443 can sometimes include the ":443" suffix in
    72  	// their requested names; handle this case. TODO: make 443 configurable?
    73  	var cleanedName string
    74  	var requestedNamePortNumber string
    75  	if cleanedName, requestedNamePortNumber, err = net.SplitHostPort(initialReq.Name); err != nil {
    76  		cleanedName = initialReq.Name
    77  	} else {
    78  		if requestedNamePortNumber != "443" {
    79  			logger.Info("Bad requested service name port number: %v.", requestedNamePortNumber)
    80  			return status.Error(codes.Unknown, "Bad requested service name port number")
    81  		}
    82  	}
    83  	if cleanedName != *serviceName {
    84  		logger.Info("Expected requested service name: %v. Got: %v", *serviceName, initialReq.Name)
    85  		return status.Error(codes.NotFound, "Bad requested service name")
    86  	}
    87  	if err := stream.Send(&lbpb.LoadBalanceResponse{
    88  		LoadBalanceResponseType: &lbpb.LoadBalanceResponse_InitialResponse{
    89  			InitialResponse: &lbpb.InitialLoadBalanceResponse{},
    90  		},
    91  	}); err != nil {
    92  		logger.Errorf("Error sending initial LB response: %v", err)
    93  		return status.Error(codes.Unknown, "Error sending initial response")
    94  	}
    95  	logger.Info("Send LoadBalanceResponse: %v", l.serverListResponse)
    96  	if err := stream.Send(l.serverListResponse); err != nil {
    97  		logger.Errorf("Error sending LB response: %v", err)
    98  		return status.Error(codes.Unknown, "Error sending response")
    99  	}
   100  	if *shortStream {
   101  		return nil
   102  	}
   103  	for {
   104  		logger.Info("Send LoadBalanceResponse: %v", l.serverListResponse)
   105  		if err := stream.Send(l.serverListResponse); err != nil {
   106  			logger.Errorf("Error sending LB response: %v", err)
   107  			return status.Error(codes.Unknown, "Error sending response")
   108  		}
   109  		time.Sleep(10 * time.Second)
   110  	}
   111  }
   112  
   113  func main() {
   114  	flag.Parse()
   115  	var opts []grpc.ServerOption
   116  	if *useTLS {
   117  		certFile := testdata.Path("server1.pem")
   118  		keyFile := testdata.Path("server1.key")
   119  		creds, err := credentials.NewServerTLSFromFile(certFile, keyFile)
   120  		if err != nil {
   121  			logger.Fatalf("Failed to generate credentials %v", err)
   122  		}
   123  		opts = append(opts, grpc.Creds(creds))
   124  	} else if *useALTS {
   125  		altsOpts := alts.DefaultServerOptions()
   126  		altsTC := alts.NewServerCreds(altsOpts)
   127  		opts = append(opts, grpc.Creds(altsTC))
   128  	}
   129  	var serverList []*lbpb.Server
   130  	if len(*backendAddrs) == 0 {
   131  		serverList = make([]*lbpb.Server, 0)
   132  	} else {
   133  		rawBackendAddrs := strings.Split(*backendAddrs, ",")
   134  		serverList = make([]*lbpb.Server, len(rawBackendAddrs))
   135  		for i := range rawBackendAddrs {
   136  			rawIP, rawPort, err := net.SplitHostPort(rawBackendAddrs[i])
   137  			if err != nil {
   138  				logger.Fatalf("Failed to parse --backend_addrs[%d]=%v, error: %v", i, rawBackendAddrs[i], err)
   139  			}
   140  			ip := net.ParseIP(rawIP)
   141  			if ip == nil {
   142  				logger.Fatalf("Failed to parse ip: %v", rawIP)
   143  			}
   144  			numericPort, err := strconv.Atoi(rawPort)
   145  			if err != nil {
   146  				logger.Fatalf("Failed to convert port %v to int", rawPort)
   147  			}
   148  			logger.Infof("Adding backend ip: %v, port: %d", ip.String(), numericPort)
   149  			serverList[i] = &lbpb.Server{
   150  				IpAddress: ip,
   151  				Port:      int32(numericPort),
   152  			}
   153  		}
   154  	}
   155  	serverListResponse := &lbpb.LoadBalanceResponse{
   156  		LoadBalanceResponseType: &lbpb.LoadBalanceResponse_ServerList{
   157  			ServerList: &lbpb.ServerList{
   158  				Servers: serverList,
   159  			},
   160  		},
   161  	}
   162  	server := grpc.NewServer(opts...)
   163  	logger.Infof("Begin listening on %d.", *port)
   164  	lis, err := net.Listen("tcp", ":"+strconv.Itoa(*port))
   165  	if err != nil {
   166  		logger.Fatalf("Failed to listen on port %v: %v", *port, err)
   167  	}
   168  	lbpb.RegisterLoadBalancerServer(server, &loadBalancerServer{
   169  		serverListResponse: serverListResponse,
   170  	})
   171  	server.Serve(lis)
   172  }