server.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package zrpc
  2. import (
  3. "log"
  4. "time"
  5. "github.com/tal-tech/go-zero/core/load"
  6. "github.com/tal-tech/go-zero/core/logx"
  7. "github.com/tal-tech/go-zero/core/stat"
  8. "github.com/tal-tech/go-zero/zrpc/internal"
  9. "github.com/tal-tech/go-zero/zrpc/internal/auth"
  10. "github.com/tal-tech/go-zero/zrpc/internal/serverinterceptors"
  11. "google.golang.org/grpc"
  12. )
  13. // A RpcServer is a rpc server.
  14. type RpcServer struct {
  15. server internal.Server
  16. register internal.RegisterFn
  17. }
  18. // MustNewServer returns a RpcSever, exits on any error.
  19. func MustNewServer(c RpcServerConf, register internal.RegisterFn) *RpcServer {
  20. server, err := NewServer(c, register)
  21. if err != nil {
  22. log.Fatal(err)
  23. }
  24. return server
  25. }
  26. // NewServer returns a RpcServer.
  27. func NewServer(c RpcServerConf, register internal.RegisterFn) (*RpcServer, error) {
  28. var err error
  29. if err = c.Validate(); err != nil {
  30. return nil, err
  31. }
  32. var server internal.Server
  33. metrics := stat.NewMetrics(c.ListenOn)
  34. if c.HasEtcd() {
  35. server, err = internal.NewRpcPubServer(c.Etcd.Hosts, c.Etcd.Key, c.ListenOn, internal.WithMetrics(metrics))
  36. if err != nil {
  37. return nil, err
  38. }
  39. } else {
  40. server = internal.NewRpcServer(c.ListenOn, internal.WithMetrics(metrics))
  41. }
  42. server.SetName(c.Name)
  43. if err = setupInterceptors(server, c, metrics); err != nil {
  44. return nil, err
  45. }
  46. rpcServer := &RpcServer{
  47. server: server,
  48. register: register,
  49. }
  50. if err = c.SetUp(); err != nil {
  51. return nil, err
  52. }
  53. return rpcServer, nil
  54. }
  55. // AddOptions adds given options.
  56. func (rs *RpcServer) AddOptions(options ...grpc.ServerOption) {
  57. rs.server.AddOptions(options...)
  58. }
  59. // AddStreamInterceptors adds given stream interceptors.
  60. func (rs *RpcServer) AddStreamInterceptors(interceptors ...grpc.StreamServerInterceptor) {
  61. rs.server.AddStreamInterceptors(interceptors...)
  62. }
  63. // AddUnaryInterceptors adds given unary interceptors.
  64. func (rs *RpcServer) AddUnaryInterceptors(interceptors ...grpc.UnaryServerInterceptor) {
  65. rs.server.AddUnaryInterceptors(interceptors...)
  66. }
  67. // Start starts the RpcServer.
  68. // Graceful shutdown is enabled by default.
  69. // Use proc.SetTimeToForceQuit to customize the graceful shutdown period.
  70. func (rs *RpcServer) Start() {
  71. if err := rs.server.Start(rs.register); err != nil {
  72. logx.Error(err)
  73. panic(err)
  74. }
  75. }
  76. // Stop stops the RpcServer.
  77. func (rs *RpcServer) Stop() {
  78. logx.Close()
  79. }
  80. func setupInterceptors(server internal.Server, c RpcServerConf, metrics *stat.Metrics) error {
  81. if c.CpuThreshold > 0 {
  82. shedder := load.NewAdaptiveShedder(load.WithCpuThreshold(c.CpuThreshold))
  83. server.AddUnaryInterceptors(serverinterceptors.UnarySheddingInterceptor(shedder, metrics))
  84. }
  85. if c.Timeout > 0 {
  86. server.AddUnaryInterceptors(serverinterceptors.UnaryTimeoutInterceptor(
  87. time.Duration(c.Timeout) * time.Millisecond))
  88. }
  89. if c.Auth {
  90. authenticator, err := auth.NewAuthenticator(c.Redis.NewRedis(), c.Redis.Key, c.StrictControl)
  91. if err != nil {
  92. return err
  93. }
  94. server.AddStreamInterceptors(serverinterceptors.StreamAuthorizeInterceptor(authenticator))
  95. server.AddUnaryInterceptors(serverinterceptors.UnaryAuthorizeInterceptor(authenticator))
  96. }
  97. return nil
  98. }