go.etcd.io/etcd@v3.3.27+incompatible/etcdserver/api/v3rpc/grpc.go (about)

     1  // Copyright 2016 The etcd 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 v3rpc
    16  
    17  import (
    18  	"crypto/tls"
    19  	"math"
    20  
    21  	"github.com/coreos/etcd/etcdserver"
    22  	pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
    23  
    24  	"github.com/grpc-ecosystem/go-grpc-middleware"
    25  	"github.com/grpc-ecosystem/go-grpc-prometheus"
    26  	"google.golang.org/grpc"
    27  	"google.golang.org/grpc/credentials"
    28  	"google.golang.org/grpc/health"
    29  	healthpb "google.golang.org/grpc/health/grpc_health_v1"
    30  )
    31  
    32  const (
    33  	grpcOverheadBytes = 512 * 1024
    34  	maxStreams        = math.MaxUint32
    35  	maxSendBytes      = math.MaxInt32
    36  )
    37  
    38  func Server(s *etcdserver.EtcdServer, tls *tls.Config, gopts ...grpc.ServerOption) *grpc.Server {
    39  	var opts []grpc.ServerOption
    40  	opts = append(opts, grpc.CustomCodec(&codec{}))
    41  	if tls != nil {
    42  		opts = append(opts, grpc.Creds(credentials.NewTLS(tls)))
    43  	}
    44  	opts = append(opts, grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
    45  		newLogUnaryInterceptor(s),
    46  		newUnaryInterceptor(s),
    47  		grpc_prometheus.UnaryServerInterceptor,
    48  	)))
    49  	opts = append(opts, grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(
    50  		newStreamInterceptor(s),
    51  		grpc_prometheus.StreamServerInterceptor,
    52  	)))
    53  	opts = append(opts, grpc.MaxRecvMsgSize(int(s.Cfg.MaxRequestBytes+grpcOverheadBytes)))
    54  	opts = append(opts, grpc.MaxSendMsgSize(maxSendBytes))
    55  	opts = append(opts, grpc.MaxConcurrentStreams(maxStreams))
    56  	grpcServer := grpc.NewServer(append(opts, gopts...)...)
    57  
    58  	pb.RegisterKVServer(grpcServer, NewQuotaKVServer(s))
    59  	pb.RegisterWatchServer(grpcServer, NewWatchServer(s))
    60  	pb.RegisterLeaseServer(grpcServer, NewQuotaLeaseServer(s))
    61  	pb.RegisterClusterServer(grpcServer, NewClusterServer(s))
    62  	pb.RegisterAuthServer(grpcServer, NewAuthServer(s))
    63  	pb.RegisterMaintenanceServer(grpcServer, NewMaintenanceServer(s))
    64  
    65  	// server should register all the services manually
    66  	// use empty service name for all etcd services' health status,
    67  	// see https://github.com/grpc/grpc/blob/master/doc/health-checking.md for more
    68  	hsrv := health.NewServer()
    69  	hsrv.SetServingStatus("", healthpb.HealthCheckResponse_SERVING)
    70  	healthpb.RegisterHealthServer(grpcServer, hsrv)
    71  
    72  	// set zero values for metrics registered for this grpc server
    73  	grpc_prometheus.Register(grpcServer)
    74  
    75  	return grpcServer
    76  }