grpc.go 2.8 KB

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