go-zero/zrpc/server.go

146 lines
3.9 KiB
Go
Raw Normal View History

2020-09-18 11:41:52 +08:00
package zrpc
2020-07-26 17:09:05 +08:00
import (
"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"
2020-07-26 17:09:05 +08:00
)
2021-03-01 23:52:44 +08:00
// A RpcServer is a rpc server.
2020-07-26 17:09:05 +08:00
type RpcServer struct {
2020-07-29 18:06:57 +08:00
server internal.Server
register internal.RegisterFn
2020-07-26 17:09:05 +08:00
}
2021-03-01 23:52:44 +08:00
// MustNewServer returns a RpcSever, exits on any error.
2020-07-29 18:06:57 +08:00
func MustNewServer(c RpcServerConf, register internal.RegisterFn) *RpcServer {
2020-07-26 17:09:05 +08:00
server, err := NewServer(c, register)
logx.Must(err)
2020-07-26 17:09:05 +08:00
return server
}
2021-03-01 23:52:44 +08:00
// NewServer returns a RpcServer.
2020-07-29 18:06:57 +08:00
func NewServer(c RpcServerConf, register internal.RegisterFn) (*RpcServer, error) {
2020-07-26 17:09:05 +08:00
var err error
if err = c.Validate(); err != nil {
return nil, err
}
2020-07-29 18:06:57 +08:00
var server internal.Server
2020-07-26 17:09:05 +08:00
metrics := stat.NewMetrics(c.ListenOn)
serverOptions := []internal.ServerOption{
internal.WithMetrics(metrics),
internal.WithRpcHealth(c.Health),
}
2020-07-26 17:09:05 +08:00
if c.HasEtcd() {
server, err = internal.NewRpcPubServer(c.Etcd, c.ListenOn, c.Middlewares, serverOptions...)
2020-07-26 17:09:05 +08:00
if err != nil {
return nil, err
}
} else {
server = internal.NewRpcServer(c.ListenOn, c.Middlewares, serverOptions...)
2020-07-26 17:09:05 +08:00
}
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
}
2021-03-01 23:52:44 +08:00
// AddOptions adds given options.
func (rs *RpcServer) AddOptions(options ...grpc.ServerOption) {
2020-09-29 14:30:22 +08:00
rs.server.AddOptions(options...)
}
2021-03-01 23:52:44 +08:00
// AddStreamInterceptors adds given stream interceptors.
func (rs *RpcServer) AddStreamInterceptors(interceptors ...grpc.StreamServerInterceptor) {
2020-09-29 14:30:22 +08:00
rs.server.AddStreamInterceptors(interceptors...)
}
2021-03-01 23:52:44 +08:00
// AddUnaryInterceptors adds given unary interceptors.
func (rs *RpcServer) AddUnaryInterceptors(interceptors ...grpc.UnaryServerInterceptor) {
2020-09-29 14:30:22 +08:00
rs.server.AddUnaryInterceptors(interceptors...)
}
2021-03-01 23:52:44 +08:00
// Start starts the RpcServer.
// Graceful shutdown is enabled by default.
// Use proc.SetTimeToForceQuit to customize the graceful shutdown period.
2020-07-26 17:09:05 +08:00
func (rs *RpcServer) Start() {
if err := rs.server.Start(rs.register); err != nil {
logx.Error(err)
panic(err)
}
}
2021-03-01 23:52:44 +08:00
// Stop stops the RpcServer.
2020-07-26 17:09:05 +08:00
func (rs *RpcServer) Stop() {
logx.Close()
}
2022-07-09 15:05:59 +08:00
// DontLogContentForMethod disable logging content for given method.
// Deprecated: use ServerMiddlewaresConf.IgnoreContentMethods instead.
2022-07-09 15:05:59 +08:00
func DontLogContentForMethod(method string) {
serverinterceptors.DontLogContentForMethod(method)
}
// SetServerSlowThreshold sets the slow threshold on server side.
// Deprecated: use ServerMiddlewaresConf.SlowThreshold instead.
func SetServerSlowThreshold(threshold time.Duration) {
serverinterceptors.SetSlowThreshold(threshold)
}
2023-03-25 20:40:21 +08:00
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 {
2020-07-26 17:09:05 +08:00
if c.CpuThreshold > 0 {
shedder := load.NewAdaptiveShedder(load.WithCpuThreshold(c.CpuThreshold))
2023-03-25 20:40:21 +08:00
svr.AddUnaryInterceptors(serverinterceptors.UnarySheddingInterceptor(shedder, metrics))
2020-07-26 17:09:05 +08:00
}
if c.Timeout > 0 {
2023-03-25 20:40:21 +08:00
svr.AddUnaryInterceptors(serverinterceptors.UnaryTimeoutInterceptor(
2020-07-26 17:09:05 +08:00
time.Duration(c.Timeout) * time.Millisecond))
}
if c.Auth {
2023-03-25 20:40:21 +08:00
if err := setupAuthInterceptors(svr, c); err != nil {
2020-07-26 17:09:05 +08:00
return err
}
}
return nil
}