server.go 1.3 KB

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