grpc.go 2.4 KB

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