grpc.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. "io/ioutil"
  18. "math"
  19. "os"
  20. "sync"
  21. "github.com/coreos/etcd/etcdserver"
  22. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  23. "github.com/grpc-ecosystem/go-grpc-prometheus"
  24. "google.golang.org/grpc"
  25. "google.golang.org/grpc/credentials"
  26. "google.golang.org/grpc/grpclog"
  27. "google.golang.org/grpc/health"
  28. healthpb "google.golang.org/grpc/health/grpc_health_v1"
  29. )
  30. const (
  31. grpcOverheadBytes = 512 * 1024
  32. maxStreams = math.MaxUint32
  33. maxSendBytes = math.MaxInt32
  34. )
  35. // integration tests call this multiple times, which is racey in gRPC side
  36. var grpclogOnce sync.Once
  37. func Server(s *etcdserver.EtcdServer, tls *tls.Config, gopts ...grpc.ServerOption) *grpc.Server {
  38. var opts []grpc.ServerOption
  39. opts = append(opts, grpc.CustomCodec(&codec{}))
  40. if tls != nil {
  41. opts = append(opts, grpc.Creds(credentials.NewTLS(tls)))
  42. }
  43. opts = append(opts, grpc.UnaryInterceptor(newUnaryInterceptor(s)))
  44. opts = append(opts, grpc.StreamInterceptor(newStreamInterceptor(s)))
  45. opts = append(opts, grpc.MaxRecvMsgSize(int(s.Cfg.MaxRequestBytes+grpcOverheadBytes)))
  46. opts = append(opts, grpc.MaxSendMsgSize(maxSendBytes))
  47. opts = append(opts, grpc.MaxConcurrentStreams(maxStreams))
  48. grpcServer := grpc.NewServer(append(opts, gopts...)...)
  49. pb.RegisterKVServer(grpcServer, NewQuotaKVServer(s))
  50. pb.RegisterWatchServer(grpcServer, NewWatchServer(s))
  51. pb.RegisterLeaseServer(grpcServer, NewQuotaLeaseServer(s))
  52. pb.RegisterClusterServer(grpcServer, NewClusterServer(s))
  53. pb.RegisterAuthServer(grpcServer, NewAuthServer(s))
  54. pb.RegisterMaintenanceServer(grpcServer, NewMaintenanceServer(s))
  55. // server should register all the services manually
  56. // use empty service name for all etcd services' health status,
  57. // see https://github.com/grpc/grpc/blob/master/doc/health-checking.md for more
  58. hsrv := health.NewServer()
  59. hsrv.SetServingStatus("", healthpb.HealthCheckResponse_SERVING)
  60. healthpb.RegisterHealthServer(grpcServer, hsrv)
  61. // set zero values for metrics registered for this grpc server
  62. grpc_prometheus.Register(grpcServer)
  63. grpclogOnce.Do(func() {
  64. if s.Cfg.Debug {
  65. grpc.EnableTracing = true
  66. // enable info, warning, error
  67. grpclog.SetLoggerV2(grpclog.NewLoggerV2(os.Stderr, os.Stderr, os.Stderr))
  68. } else {
  69. // only discard info
  70. grpclog.SetLoggerV2(grpclog.NewLoggerV2(ioutil.Discard, os.Stderr, os.Stderr))
  71. }
  72. })
  73. return grpcServer
  74. }