server.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package internal
  2. import (
  3. "github.com/tal-tech/go-zero/core/stat"
  4. "google.golang.org/grpc"
  5. )
  6. type (
  7. // RegisterFn defines the method to register a server.
  8. RegisterFn func(*grpc.Server)
  9. // Server interface represents a rpc server.
  10. Server interface {
  11. AddOptions(options ...grpc.ServerOption)
  12. AddStreamInterceptors(interceptors ...grpc.StreamServerInterceptor)
  13. AddUnaryInterceptors(interceptors ...grpc.UnaryServerInterceptor)
  14. SetName(string)
  15. Start(register RegisterFn) error
  16. }
  17. baseRpcServer struct {
  18. address string
  19. metrics *stat.Metrics
  20. maxRetries int
  21. options []grpc.ServerOption
  22. streamInterceptors []grpc.StreamServerInterceptor
  23. unaryInterceptors []grpc.UnaryServerInterceptor
  24. }
  25. )
  26. func newBaseRpcServer(address string, rpcServerOpts *rpcServerOptions) *baseRpcServer {
  27. return &baseRpcServer{
  28. address: address,
  29. metrics: rpcServerOpts.metrics,
  30. maxRetries: rpcServerOpts.MaxRetries,
  31. }
  32. }
  33. func (s *baseRpcServer) AddOptions(options ...grpc.ServerOption) {
  34. s.options = append(s.options, options...)
  35. }
  36. func (s *baseRpcServer) AddStreamInterceptors(interceptors ...grpc.StreamServerInterceptor) {
  37. s.streamInterceptors = append(s.streamInterceptors, interceptors...)
  38. }
  39. func (s *baseRpcServer) AddUnaryInterceptors(interceptors ...grpc.UnaryServerInterceptor) {
  40. s.unaryInterceptors = append(s.unaryInterceptors, interceptors...)
  41. }
  42. func (s *baseRpcServer) SetName(name string) {
  43. s.metrics.SetName(name)
  44. }