You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/zrpc/internal/server.go

63 lines
1.7 KiB
Go

4 years ago
package internal
4 years ago
import (
"time"
"github.com/zeromicro/go-zero/core/stat"
4 years ago
"google.golang.org/grpc"
"google.golang.org/grpc/health"
"google.golang.org/grpc/keepalive"
4 years ago
)
const defaultConnectionIdleDuration = time.Minute * 5
4 years ago
type (
// RegisterFn defines the method to register a server.
4 years ago
RegisterFn func(*grpc.Server)
// Server interface represents a rpc server.
4 years ago
Server interface {
AddOptions(options ...grpc.ServerOption)
AddStreamInterceptors(interceptors ...grpc.StreamServerInterceptor)
AddUnaryInterceptors(interceptors ...grpc.UnaryServerInterceptor)
SetName(string)
Start(register RegisterFn) error
}
baseRpcServer struct {
address string
health *health.Server
4 years ago
metrics *stat.Metrics
options []grpc.ServerOption
streamInterceptors []grpc.StreamServerInterceptor
unaryInterceptors []grpc.UnaryServerInterceptor
}
)
func newBaseRpcServer(address string, rpcServerOpts *rpcServerOptions) *baseRpcServer {
4 years ago
return &baseRpcServer{
address: address,
health: health.NewServer(),
metrics: rpcServerOpts.metrics,
options: []grpc.ServerOption{grpc.KeepaliveParams(keepalive.ServerParameters{
MaxConnectionIdle: defaultConnectionIdleDuration,
})},
4 years ago
}
}
func (s *baseRpcServer) AddOptions(options ...grpc.ServerOption) {
s.options = append(s.options, options...)
}
func (s *baseRpcServer) AddStreamInterceptors(interceptors ...grpc.StreamServerInterceptor) {
s.streamInterceptors = append(s.streamInterceptors, interceptors...)
}
func (s *baseRpcServer) AddUnaryInterceptors(interceptors ...grpc.UnaryServerInterceptor) {
s.unaryInterceptors = append(s.unaryInterceptors, interceptors...)
}
func (s *baseRpcServer) SetName(name string) {
s.metrics.SetName(name)
}