gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/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/ks-custle/core-gm/grpc" 25 "gitee.com/ks-custle/core-gm/grpc/keepalive" 26 27 clusterservice "gitee.com/ks-custle/core-gm/go-control-plane/envoy/service/cluster/v3" 28 discoverygrpc "gitee.com/ks-custle/core-gm/go-control-plane/envoy/service/discovery/v3" 29 endpointservice "gitee.com/ks-custle/core-gm/go-control-plane/envoy/service/endpoint/v3" 30 listenerservice "gitee.com/ks-custle/core-gm/go-control-plane/envoy/service/listener/v3" 31 routeservice "gitee.com/ks-custle/core-gm/go-control-plane/envoy/service/route/v3" 32 runtimeservice "gitee.com/ks-custle/core-gm/go-control-plane/envoy/service/runtime/v3" 33 secretservice "gitee.com/ks-custle/core-gm/go-control-plane/envoy/service/secret/v3" 34 "gitee.com/ks-custle/core-gm/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 // 57 //goland:noinspection GoUnusedParameter 58 func RunServer(ctx context.Context, srv server.Server, port uint) { 59 // gRPC golang library sets a very small upper bound for the number gRPC/h2 60 // streams over a single TCP connection. If a proxy multiplexes requests over 61 // a single connection to the management server, then it might lead to 62 // availability problems. Keepalive timeouts based on connection_keepalive parameter https://www.envoyproxy.io/docs/envoy/latest/configuration/overview/examples#dynamic 63 var grpcOptions []grpc.ServerOption 64 grpcOptions = append(grpcOptions, 65 grpc.MaxConcurrentStreams(grpcMaxConcurrentStreams), 66 grpc.KeepaliveParams(keepalive.ServerParameters{ 67 Time: grpcKeepaliveTime, 68 Timeout: grpcKeepaliveTimeout, 69 }), 70 grpc.KeepaliveEnforcementPolicy(keepalive.EnforcementPolicy{ 71 MinTime: grpcKeepaliveMinTime, 72 PermitWithoutStream: true, 73 }), 74 ) 75 grpcServer := grpc.NewServer(grpcOptions...) 76 77 lis, err := net.Listen("tcp", fmt.Sprintf(":%d", port)) 78 if err != nil { 79 log.Fatal(err) 80 } 81 82 registerServer(grpcServer, srv) 83 84 log.Printf("management server listening on %d\n", port) 85 if err = grpcServer.Serve(lis); err != nil { 86 log.Println(err) 87 } 88 }