server.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. type RpcServer struct {
  14. server internal.Server
  15. register internal.RegisterFn
  16. }
  17. func MustNewServer(c RpcServerConf, register internal.RegisterFn) *RpcServer {
  18. server, err := NewServer(c, register)
  19. if err != nil {
  20. log.Fatal(err)
  21. }
  22. return server
  23. }
  24. func NewServer(c RpcServerConf, register internal.RegisterFn) (*RpcServer, error) {
  25. var err error
  26. if err = c.Validate(); err != nil {
  27. return nil, err
  28. }
  29. var server internal.Server
  30. metrics := stat.NewMetrics(c.ListenOn)
  31. if c.HasEtcd() {
  32. server, err = internal.NewRpcPubServer(c.Etcd.Hosts, c.Etcd.Key, c.ListenOn, internal.WithMetrics(metrics))
  33. if err != nil {
  34. return nil, err
  35. }
  36. } else {
  37. server = internal.NewRpcServer(c.ListenOn, internal.WithMetrics(metrics))
  38. }
  39. server.SetName(c.Name)
  40. if err = setupInterceptors(server, c, metrics); err != nil {
  41. return nil, err
  42. }
  43. rpcServer := &RpcServer{
  44. server: server,
  45. register: register,
  46. }
  47. if err = c.SetUp(); err != nil {
  48. return nil, err
  49. }
  50. return rpcServer, nil
  51. }
  52. func (rs *RpcServer) AddOptions(options ...grpc.ServerOption) {
  53. rs.server.AddOptions(options...)
  54. }
  55. func (rs *RpcServer) AddStreamInterceptors(interceptors ...grpc.StreamServerInterceptor) {
  56. rs.server.AddStreamInterceptors(interceptors...)
  57. }
  58. func (rs *RpcServer) AddUnaryInterceptors(interceptors ...grpc.UnaryServerInterceptor) {
  59. rs.server.AddUnaryInterceptors(interceptors...)
  60. }
  61. func (rs *RpcServer) Start() {
  62. if err := rs.server.Start(rs.register); err != nil {
  63. logx.Error(err)
  64. panic(err)
  65. }
  66. }
  67. func (rs *RpcServer) Stop() {
  68. logx.Close()
  69. }
  70. func setupInterceptors(server internal.Server, c RpcServerConf, metrics *stat.Metrics) error {
  71. if c.CpuThreshold > 0 {
  72. shedder := load.NewAdaptiveShedder(load.WithCpuThreshold(c.CpuThreshold))
  73. server.AddUnaryInterceptors(serverinterceptors.UnarySheddingInterceptor(shedder, metrics))
  74. }
  75. if c.Timeout > 0 {
  76. server.AddUnaryInterceptors(serverinterceptors.UnaryTimeoutInterceptor(
  77. time.Duration(c.Timeout) * time.Millisecond))
  78. }
  79. if c.Auth {
  80. authenticator, err := auth.NewAuthenticator(c.Redis.NewRedis(), c.Redis.Key, c.StrictControl)
  81. if err != nil {
  82. return err
  83. }
  84. server.AddStreamInterceptors(serverinterceptors.StreamAuthorizeInterceptor(authenticator))
  85. server.AddUnaryInterceptors(serverinterceptors.UnaryAuthorizeInterceptor(authenticator))
  86. }
  87. return nil
  88. }