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/server.go

148 lines
3.8 KiB
Go

package zrpc
4 years ago
import (
"log"
"time"
"github.com/zeromicro/go-zero/core/load"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/stat"
"github.com/zeromicro/go-zero/core/stores/redis"
"github.com/zeromicro/go-zero/zrpc/internal"
"github.com/zeromicro/go-zero/zrpc/internal/auth"
"github.com/zeromicro/go-zero/zrpc/internal/serverinterceptors"
"google.golang.org/grpc"
4 years ago
)
// A RpcServer is a rpc server.
4 years ago
type RpcServer struct {
4 years ago
server internal.Server
register internal.RegisterFn
4 years ago
}
// MustNewServer returns a RpcSever, exits on any error.
4 years ago
func MustNewServer(c RpcServerConf, register internal.RegisterFn) *RpcServer {
4 years ago
server, err := NewServer(c, register)
if err != nil {
log.Fatal(err)
}
return server
}
// NewServer returns a RpcServer.
4 years ago
func NewServer(c RpcServerConf, register internal.RegisterFn) (*RpcServer, error) {
4 years ago
var err error
if err = c.Validate(); err != nil {
return nil, err
}
4 years ago
var server internal.Server
4 years ago
metrics := stat.NewMetrics(c.ListenOn)
serverOptions := []internal.ServerOption{
internal.WithMetrics(metrics),
internal.WithRpcHealth(c.Health),
}
4 years ago
if c.HasEtcd() {
server, err = internal.NewRpcPubServer(c.Etcd, c.ListenOn, c.Middlewares, serverOptions...)
4 years ago
if err != nil {
return nil, err
}
} else {
server = internal.NewRpcServer(c.ListenOn, c.Middlewares, serverOptions...)
4 years ago
}
server.SetName(c.Name)
if err = setupInterceptors(server, c, metrics); err != nil {
return nil, err
}
rpcServer := &RpcServer{
server: server,
register: register,
}
if err = c.SetUp(); err != nil {
return nil, err
}
return rpcServer, nil
}
// AddOptions adds given options.
func (rs *RpcServer) AddOptions(options ...grpc.ServerOption) {
rs.server.AddOptions(options...)
}
// AddStreamInterceptors adds given stream interceptors.
func (rs *RpcServer) AddStreamInterceptors(interceptors ...grpc.StreamServerInterceptor) {
rs.server.AddStreamInterceptors(interceptors...)
}
// AddUnaryInterceptors adds given unary interceptors.
func (rs *RpcServer) AddUnaryInterceptors(interceptors ...grpc.UnaryServerInterceptor) {
rs.server.AddUnaryInterceptors(interceptors...)
}
// Start starts the RpcServer.
// Graceful shutdown is enabled by default.
// Use proc.SetTimeToForceQuit to customize the graceful shutdown period.
4 years ago
func (rs *RpcServer) Start() {
if err := rs.server.Start(rs.register); err != nil {
logx.Error(err)
panic(err)
}
}
// Stop stops the RpcServer.
4 years ago
func (rs *RpcServer) Stop() {
logx.Close()
}
// DontLogContentForMethod disable logging content for given method.
func DontLogContentForMethod(method string) {
serverinterceptors.DontLogContentForMethod(method)
}
// SetServerSlowThreshold sets the slow threshold on server side.
func SetServerSlowThreshold(threshold time.Duration) {
serverinterceptors.SetSlowThreshold(threshold)
}
func setupAuthInterceptors(svr internal.Server, c RpcServerConf) error {
rds, err := redis.NewRedis(c.Redis.RedisConf)
if err != nil {
return err
}
authenticator, err := auth.NewAuthenticator(rds, c.Redis.Key, c.StrictControl)
if err != nil {
return err
}
svr.AddStreamInterceptors(serverinterceptors.StreamAuthorizeInterceptor(authenticator))
svr.AddUnaryInterceptors(serverinterceptors.UnaryAuthorizeInterceptor(authenticator))
return nil
}
func setupInterceptors(svr internal.Server, c RpcServerConf, metrics *stat.Metrics) error {
4 years ago
if c.CpuThreshold > 0 {
shedder := load.NewAdaptiveShedder(load.WithCpuThreshold(c.CpuThreshold))
svr.AddUnaryInterceptors(serverinterceptors.UnarySheddingInterceptor(shedder, metrics))
4 years ago
}
if c.Timeout > 0 {
svr.AddUnaryInterceptors(serverinterceptors.UnaryTimeoutInterceptor(
4 years ago
time.Duration(c.Timeout) * time.Millisecond))
}
if c.Auth {
if err := setupAuthInterceptors(svr, c); err != nil {
4 years ago
return err
}
}
return nil
}