grpc.go 2.3 KB

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