github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/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  package example
    15  
    16  import (
    17  	"context"
    18  	"fmt"
    19  	"log"
    20  	"net"
    21  	"time"
    22  
    23  	"github.com/hxx258456/ccgo/grpc"
    24  	"github.com/hxx258456/ccgo/grpc/keepalive"
    25  
    26  	clusterservice "github.com/hxx258456/ccgo/go-control-plane/envoy/service/cluster/v3"
    27  	discoverygrpc "github.com/hxx258456/ccgo/go-control-plane/envoy/service/discovery/v3"
    28  	endpointservice "github.com/hxx258456/ccgo/go-control-plane/envoy/service/endpoint/v3"
    29  	listenerservice "github.com/hxx258456/ccgo/go-control-plane/envoy/service/listener/v3"
    30  	routeservice "github.com/hxx258456/ccgo/go-control-plane/envoy/service/route/v3"
    31  	runtimeservice "github.com/hxx258456/ccgo/go-control-plane/envoy/service/runtime/v3"
    32  	secretservice "github.com/hxx258456/ccgo/go-control-plane/envoy/service/secret/v3"
    33  	"github.com/hxx258456/ccgo/go-control-plane/pkg/server/v3"
    34  )
    35  
    36  const (
    37  	grpcKeepaliveTime        = 30 * time.Second
    38  	grpcKeepaliveTimeout     = 5 * time.Second
    39  	grpcKeepaliveMinTime     = 30 * time.Second
    40  	grpcMaxConcurrentStreams = 1000000
    41  )
    42  
    43  func registerServer(grpcServer *grpc.Server, server server.Server) {
    44  	// register services
    45  	discoverygrpc.RegisterAggregatedDiscoveryServiceServer(grpcServer, server)
    46  	endpointservice.RegisterEndpointDiscoveryServiceServer(grpcServer, server)
    47  	clusterservice.RegisterClusterDiscoveryServiceServer(grpcServer, server)
    48  	routeservice.RegisterRouteDiscoveryServiceServer(grpcServer, server)
    49  	listenerservice.RegisterListenerDiscoveryServiceServer(grpcServer, server)
    50  	secretservice.RegisterSecretDiscoveryServiceServer(grpcServer, server)
    51  	runtimeservice.RegisterRuntimeDiscoveryServiceServer(grpcServer, server)
    52  }
    53  
    54  // RunServer starts an xDS server at the given port.
    55  func RunServer(ctx context.Context, srv server.Server, port uint) {
    56  	// gRPC golang library sets a very small upper bound for the number gRPC/h2
    57  	// streams over a single TCP connection. If a proxy multiplexes requests over
    58  	// a single connection to the management server, then it might lead to
    59  	// availability problems. Keepalive timeouts based on connection_keepalive parameter https://www.envoyproxy.io/docs/envoy/latest/configuration/overview/examples#dynamic
    60  	var grpcOptions []grpc.ServerOption
    61  	grpcOptions = append(grpcOptions,
    62  		grpc.MaxConcurrentStreams(grpcMaxConcurrentStreams),
    63  		grpc.KeepaliveParams(keepalive.ServerParameters{
    64  			Time:    grpcKeepaliveTime,
    65  			Timeout: grpcKeepaliveTimeout,
    66  		}),
    67  		grpc.KeepaliveEnforcementPolicy(keepalive.EnforcementPolicy{
    68  			MinTime:             grpcKeepaliveMinTime,
    69  			PermitWithoutStream: true,
    70  		}),
    71  	)
    72  	grpcServer := grpc.NewServer(grpcOptions...)
    73  
    74  	lis, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
    75  	if err != nil {
    76  		log.Fatal(err)
    77  	}
    78  
    79  	registerServer(grpcServer, srv)
    80  
    81  	log.Printf("management server listening on %d\n", port)
    82  	if err = grpcServer.Serve(lis); err != nil {
    83  		log.Println(err)
    84  	}
    85  }