github.com/shuguocloud/go-zero@v1.3.0/zrpc/internal/server.go (about)

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