khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 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 | "go.etcd.io/etcd/etcdserver" |
| 22 | pb "go.etcd.io/etcd/etcdserver/etcdserverpb" |
| 23 | |
| 24 | grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware" |
| 25 | grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus" |
| 26 | "go.etcd.io/etcd/clientv3/credentials" |
| 27 | "google.golang.org/grpc" |
| 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 | bundle := credentials.NewBundle(credentials.Config{TLSConfig: tls}) |
| 43 | opts = append(opts, grpc.Creds(bundle.TransportCredentials())) |
| 44 | } |
| 45 | opts = append(opts, grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer( |
| 46 | newLogUnaryInterceptor(s), |
| 47 | newUnaryInterceptor(s), |
| 48 | grpc_prometheus.UnaryServerInterceptor, |
| 49 | ))) |
| 50 | opts = append(opts, grpc.StreamInterceptor(grpc_middleware.ChainStreamServer( |
| 51 | newStreamInterceptor(s), |
| 52 | grpc_prometheus.StreamServerInterceptor, |
| 53 | ))) |
| 54 | opts = append(opts, grpc.MaxRecvMsgSize(int(s.Cfg.MaxRequestBytes+grpcOverheadBytes))) |
| 55 | opts = append(opts, grpc.MaxSendMsgSize(maxSendBytes)) |
| 56 | opts = append(opts, grpc.MaxConcurrentStreams(maxStreams)) |
| 57 | grpcServer := grpc.NewServer(append(opts, gopts...)...) |
| 58 | |
| 59 | pb.RegisterKVServer(grpcServer, NewQuotaKVServer(s)) |
| 60 | pb.RegisterWatchServer(grpcServer, NewWatchServer(s)) |
| 61 | pb.RegisterLeaseServer(grpcServer, NewQuotaLeaseServer(s)) |
| 62 | pb.RegisterClusterServer(grpcServer, NewClusterServer(s)) |
| 63 | pb.RegisterAuthServer(grpcServer, NewAuthServer(s)) |
| 64 | pb.RegisterMaintenanceServer(grpcServer, NewMaintenanceServer(s)) |
| 65 | |
| 66 | // server should register all the services manually |
| 67 | // use empty service name for all etcd services' health status, |
| 68 | // see https://github.com/grpc/grpc/blob/master/doc/health-checking.md for more |
| 69 | hsrv := health.NewServer() |
| 70 | hsrv.SetServingStatus("", healthpb.HealthCheckResponse_SERVING) |
| 71 | healthpb.RegisterHealthServer(grpcServer, hsrv) |
| 72 | |
| 73 | // set zero values for metrics registered for this grpc server |
| 74 | grpc_prometheus.Register(grpcServer) |
| 75 | |
| 76 | return grpcServer |
| 77 | } |