server.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. func (rs *RpcServer) Start() {
  69. if err := rs.server.Start(rs.register); err != nil {
  70. logx.Error(err)
  71. panic(err)
  72. }
  73. }
  74. // Stop stops the RpcServer.
  75. func (rs *RpcServer) Stop() {
  76. logx.Close()
  77. }
  78. func setupInterceptors(server internal.Server, c RpcServerConf, metrics *stat.Metrics) error {
  79. if c.CpuThreshold > 0 {
  80. shedder := load.NewAdaptiveShedder(load.WithCpuThreshold(c.CpuThreshold))
  81. server.AddUnaryInterceptors(serverinterceptors.UnarySheddingInterceptor(shedder, metrics))
  82. }
  83. if c.Timeout > 0 {
  84. server.AddUnaryInterceptors(serverinterceptors.UnaryTimeoutInterceptor(
  85. time.Duration(c.Timeout) * time.Millisecond))
  86. }
  87. if c.Auth {
  88. authenticator, err := auth.NewAuthenticator(c.Redis.NewRedis(), c.Redis.Key, c.StrictControl)
  89. if err != nil {
  90. return err
  91. }
  92. server.AddStreamInterceptors(serverinterceptors.StreamAuthorizeInterceptor(authenticator))
  93. server.AddUnaryInterceptors(serverinterceptors.UnaryAuthorizeInterceptor(authenticator))
  94. }
  95. return nil
  96. }