grpc.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. )
  28. const (
  29. grpcOverheadBytes = 512 * 1024
  30. maxStreams = math.MaxUint32
  31. maxSendBytes = math.MaxInt32
  32. )
  33. // integration tests call this multiple times, which is racey in gRPC side
  34. var grpclogOnce sync.Once
  35. func Server(s *etcdserver.EtcdServer, tls *tls.Config, gopts ...grpc.ServerOption) *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(append(opts, gopts...)...)
  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. grpclogOnce.Do(func() {
  54. if s.Cfg.Debug {
  55. grpc.EnableTracing = true
  56. // enable info, warning, error
  57. grpclog.SetLoggerV2(grpclog.NewLoggerV2(os.Stderr, os.Stderr, os.Stderr))
  58. } else {
  59. // only discard info
  60. grpclog.SetLoggerV2(grpclog.NewLoggerV2(ioutil.Discard, os.Stderr, os.Stderr))
  61. }
  62. })
  63. // to display all registered metrics with zero values
  64. grpc_prometheus.Register(grpcServer)
  65. return grpcServer
  66. }