rpcserver.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package internal
  2. import (
  3. "net"
  4. "github.com/zeromicro/go-zero/core/proc"
  5. "github.com/zeromicro/go-zero/core/stat"
  6. "github.com/zeromicro/go-zero/zrpc/internal/serverinterceptors"
  7. "google.golang.org/grpc"
  8. "google.golang.org/grpc/health/grpc_health_v1"
  9. )
  10. type (
  11. // ServerOption defines the method to customize a rpcServerOptions.
  12. ServerOption func(options *rpcServerOptions)
  13. rpcServerOptions struct {
  14. metrics *stat.Metrics
  15. health bool
  16. }
  17. rpcServer struct {
  18. name string
  19. *baseRpcServer
  20. }
  21. )
  22. // NewRpcServer returns a Server.
  23. func NewRpcServer(address string, opts ...ServerOption) Server {
  24. var options rpcServerOptions
  25. for _, opt := range opts {
  26. opt(&options)
  27. }
  28. if options.metrics == nil {
  29. options.metrics = stat.NewMetrics(address)
  30. }
  31. return &rpcServer{
  32. baseRpcServer: newBaseRpcServer(address, &options),
  33. }
  34. }
  35. func (s *rpcServer) SetName(name string) {
  36. s.name = name
  37. s.baseRpcServer.SetName(name)
  38. }
  39. func (s *rpcServer) Start(register RegisterFn) error {
  40. lis, err := net.Listen("tcp", s.address)
  41. if err != nil {
  42. return err
  43. }
  44. unaryInterceptors := []grpc.UnaryServerInterceptor{
  45. serverinterceptors.UnaryTracingInterceptor,
  46. serverinterceptors.UnaryCrashInterceptor,
  47. serverinterceptors.UnaryStatInterceptor(s.metrics),
  48. serverinterceptors.UnaryPrometheusInterceptor,
  49. serverinterceptors.UnaryBreakerInterceptor,
  50. }
  51. unaryInterceptors = append(unaryInterceptors, s.unaryInterceptors...)
  52. streamInterceptors := []grpc.StreamServerInterceptor{
  53. serverinterceptors.StreamTracingInterceptor,
  54. serverinterceptors.StreamCrashInterceptor,
  55. serverinterceptors.StreamBreakerInterceptor,
  56. }
  57. streamInterceptors = append(streamInterceptors, s.streamInterceptors...)
  58. options := append(s.options, WithUnaryServerInterceptors(unaryInterceptors...),
  59. WithStreamServerInterceptors(streamInterceptors...))
  60. server := grpc.NewServer(options...)
  61. register(server)
  62. // register the health check service
  63. if s.health != nil {
  64. grpc_health_v1.RegisterHealthServer(server, s.health)
  65. s.health.Resume()
  66. }
  67. // we need to make sure all others are wrapped up,
  68. // so we do graceful stop at shutdown phase instead of wrap up phase
  69. waitForCalled := proc.AddWrapUpListener(func() {
  70. if s.health != nil {
  71. s.health.Shutdown()
  72. }
  73. server.GracefulStop()
  74. })
  75. defer waitForCalled()
  76. return server.Serve(lis)
  77. }
  78. // WithMetrics returns a func that sets metrics to a Server.
  79. func WithMetrics(metrics *stat.Metrics) ServerOption {
  80. return func(options *rpcServerOptions) {
  81. options.metrics = metrics
  82. }
  83. }
  84. // WithRpcHealth returns a func that sets rpc health switch to a Server.
  85. func WithRpcHealth(health bool) ServerOption {
  86. return func(options *rpcServerOptions) {
  87. options.health = health
  88. }
  89. }