grpc.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. "google.golang.org/grpc"
  21. "google.golang.org/grpc/credentials"
  22. "google.golang.org/grpc/grpclog"
  23. "google.golang.org/grpc/health"
  24. healthpb "google.golang.org/grpc/health/grpc_health_v1"
  25. )
  26. const (
  27. grpcOverheadBytes = 512 * 1024
  28. maxStreams = math.MaxUint32
  29. maxSendBytes = math.MaxInt32
  30. )
  31. func init() {
  32. grpclog.SetLogger(plog)
  33. }
  34. func Server(s *etcdserver.EtcdServer, tls *tls.Config) *grpc.Server {
  35. var opts []grpc.ServerOption
  36. opts = append(opts, grpc.CustomCodec(&codec{}))
  37. if tls != nil {
  38. opts = append(opts, grpc.Creds(credentials.NewTLS(tls)))
  39. }
  40. opts = append(opts, grpc.UnaryInterceptor(newUnaryInterceptor(s)))
  41. opts = append(opts, grpc.StreamInterceptor(newStreamInterceptor(s)))
  42. opts = append(opts, grpc.MaxRecvMsgSize(int(s.Cfg.MaxRequestBytes+grpcOverheadBytes)))
  43. opts = append(opts, grpc.MaxSendMsgSize(maxSendBytes))
  44. opts = append(opts, grpc.MaxConcurrentStreams(maxStreams))
  45. grpcServer := grpc.NewServer(opts...)
  46. pb.RegisterKVServer(grpcServer, NewQuotaKVServer(s))
  47. pb.RegisterWatchServer(grpcServer, NewWatchServer(s))
  48. pb.RegisterLeaseServer(grpcServer, NewQuotaLeaseServer(s))
  49. pb.RegisterClusterServer(grpcServer, NewClusterServer(s))
  50. pb.RegisterAuthServer(grpcServer, NewAuthServer(s))
  51. pb.RegisterMaintenanceServer(grpcServer, NewMaintenanceServer(s))
  52. // server should register all the services manually
  53. // use empty service name for all etcd services' health status,
  54. // see https://github.com/grpc/grpc/blob/master/doc/health-checking.md for more
  55. hsrv := health.NewServer()
  56. hsrv.SetServingStatus("", healthpb.HealthCheckResponse_SERVING)
  57. healthpb.RegisterHealthServer(grpcServer, hsrv)
  58. return grpcServer
  59. }