server.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package internal
  2. import (
  3. "git.i2edu.net/i2/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. options []grpc.ServerOption
  21. streamInterceptors []grpc.StreamServerInterceptor
  22. unaryInterceptors []grpc.UnaryServerInterceptor
  23. }
  24. )
  25. func newBaseRpcServer(address string, metrics *stat.Metrics) *baseRpcServer {
  26. return &baseRpcServer{
  27. address: address,
  28. metrics: metrics,
  29. }
  30. }
  31. func (s *baseRpcServer) AddOptions(options ...grpc.ServerOption) {
  32. s.options = append(s.options, options...)
  33. }
  34. func (s *baseRpcServer) AddStreamInterceptors(interceptors ...grpc.StreamServerInterceptor) {
  35. s.streamInterceptors = append(s.streamInterceptors, interceptors...)
  36. }
  37. func (s *baseRpcServer) AddUnaryInterceptors(interceptors ...grpc.UnaryServerInterceptor) {
  38. s.unaryInterceptors = append(s.unaryInterceptors, interceptors...)
  39. }
  40. func (s *baseRpcServer) SetName(name string) {
  41. s.metrics.SetName(name)
  42. }