gitee.com/zhaochuninhefei/gmgo@v0.0.31-0.20240209061119-069254a02979/go-control-plane/internal/example/server.go (about)

     1  // Copyright 2020 Envoyproxy Authors
     2  //
     3  //   Licensed under the Apache License, Version 2.0 (the "License");
     4  //   you may not use this file except in compliance with the License.
     5  //   You may obtain a copy of the License at
     6  //
     7  //       http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  //   Unless required by applicable law or agreed to in writing, software
    10  //   distributed under the License is distributed on an "AS IS" BASIS,
    11  //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  //   See the License for the specific language governing permissions and
    13  //   limitations under the License.
    14  
    15  package example
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"log"
    21  	"net"
    22  	"time"
    23  
    24  	"gitee.com/zhaochuninhefei/gmgo/grpc"
    25  	"gitee.com/zhaochuninhefei/gmgo/grpc/keepalive"
    26  
    27  	clusterservice "gitee.com/zhaochuninhefei/gmgo/go-control-plane/envoy/service/cluster/v3"
    28  	discoverygrpc "gitee.com/zhaochuninhefei/gmgo/go-control-plane/envoy/service/discovery/v3"
    29  	endpointservice "gitee.com/zhaochuninhefei/gmgo/go-control-plane/envoy/service/endpoint/v3"
    30  	listenerservice "gitee.com/zhaochuninhefei/gmgo/go-control-plane/envoy/service/listener/v3"
    31  	routeservice "gitee.com/zhaochuninhefei/gmgo/go-control-plane/envoy/service/route/v3"
    32  	runtimeservice "gitee.com/zhaochuninhefei/gmgo/go-control-plane/envoy/service/runtime/v3"
    33  	secretservice "gitee.com/zhaochuninhefei/gmgo/go-control-plane/envoy/service/secret/v3"
    34  	"gitee.com/zhaochuninhefei/gmgo/go-control-plane/pkg/server/v3"
    35  )
    36  
    37  const (
    38  	grpcKeepaliveTime        = 30 * time.Second
    39  	grpcKeepaliveTimeout     = 5 * time.Second
    40  	grpcKeepaliveMinTime     = 30 * time.Second
    41  	grpcMaxConcurrentStreams = 1000000
    42  )
    43  
    44  func registerServer(grpcServer *grpc.Server, server server.Server) {
    45  	// register services
    46  	discoverygrpc.RegisterAggregatedDiscoveryServiceServer(grpcServer, server)
    47  	endpointservice.RegisterEndpointDiscoveryServiceServer(grpcServer, server)
    48  	clusterservice.RegisterClusterDiscoveryServiceServer(grpcServer, server)
    49  	routeservice.RegisterRouteDiscoveryServiceServer(grpcServer, server)
    50  	listenerservice.RegisterListenerDiscoveryServiceServer(grpcServer, server)
    51  	secretservice.RegisterSecretDiscoveryServiceServer(grpcServer, server)
    52  	runtimeservice.RegisterRuntimeDiscoveryServiceServer(grpcServer, server)
    53  }
    54  
    55  // RunServer starts an xDS server at the given port.
    56  //goland:noinspection GoUnusedParameter
    57  func RunServer(ctx context.Context, srv server.Server, port uint) {
    58  	// gRPC golang library sets a very small upper bound for the number gRPC/h2
    59  	// streams over a single TCP connection. If a proxy multiplexes requests over
    60  	// a single connection to the management server, then it might lead to
    61  	// availability problems. Keepalive timeouts based on connection_keepalive parameter https://www.envoyproxy.io/docs/envoy/latest/configuration/overview/examples#dynamic
    62  	var grpcOptions []grpc.ServerOption
    63  	grpcOptions = append(grpcOptions,
    64  		grpc.MaxConcurrentStreams(grpcMaxConcurrentStreams),
    65  		grpc.KeepaliveParams(keepalive.ServerParameters{
    66  			Time:    grpcKeepaliveTime,
    67  			Timeout: grpcKeepaliveTimeout,
    68  		}),
    69  		grpc.KeepaliveEnforcementPolicy(keepalive.EnforcementPolicy{
    70  			MinTime:             grpcKeepaliveMinTime,
    71  			PermitWithoutStream: true,
    72  		}),
    73  	)
    74  	grpcServer := grpc.NewServer(grpcOptions...)
    75  
    76  	lis, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
    77  	if err != nil {
    78  		log.Fatal(err)
    79  	}
    80  
    81  	registerServer(grpcServer, srv)
    82  
    83  	log.Printf("management server listening on %d\n", port)
    84  	if err = grpcServer.Serve(lis); err != nil {
    85  		log.Println(err)
    86  	}
    87  }