grpc.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. "github.com/coreos/etcd/etcdserver"
  19. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  20. "github.com/grpc-ecosystem/go-grpc-middleware"
  21. "github.com/grpc-ecosystem/go-grpc-prometheus"
  22. "google.golang.org/grpc"
  23. "google.golang.org/grpc/credentials"
  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. opts = append(opts, grpc.Creds(credentials.NewTLS(tls)))
  37. }
  38. opts = append(opts, grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
  39. newLogUnaryInterceptor(s),
  40. newUnaryInterceptor(s),
  41. grpc_prometheus.UnaryServerInterceptor,
  42. )))
  43. opts = append(opts, grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(
  44. newStreamInterceptor(s),
  45. grpc_prometheus.StreamServerInterceptor,
  46. )))
  47. opts = append(opts, grpc.MaxRecvMsgSize(int(s.Cfg.MaxRequestBytes+grpcOverheadBytes)))
  48. opts = append(opts, grpc.MaxSendMsgSize(maxSendBytes))
  49. opts = append(opts, grpc.MaxConcurrentStreams(maxStreams))
  50. grpcServer := grpc.NewServer(append(opts, gopts...)...)
  51. pb.RegisterKVServer(grpcServer, NewQuotaKVServer(s))
  52. pb.RegisterWatchServer(grpcServer, NewWatchServer(s))
  53. pb.RegisterLeaseServer(grpcServer, NewQuotaLeaseServer(s))
  54. pb.RegisterClusterServer(grpcServer, NewClusterServer(s))
  55. pb.RegisterAuthServer(grpcServer, NewAuthServer(s))
  56. pb.RegisterMaintenanceServer(grpcServer, NewMaintenanceServer(s))
  57. // server should register all the services manually
  58. // use empty service name for all etcd services' health status,
  59. // see https://github.com/grpc/grpc/blob/master/doc/health-checking.md for more
  60. hsrv := health.NewServer()
  61. hsrv.SetServingStatus("", healthpb.HealthCheckResponse_SERVING)
  62. healthpb.RegisterHealthServer(grpcServer, hsrv)
  63. // set zero values for metrics registered for this grpc server
  64. grpc_prometheus.Register(grpcServer)
  65. return grpcServer
  66. }