grpc.go 2.6 KB

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