|
|
@ -9,21 +9,20 @@ import (
|
|
|
|
"zero/core/load"
|
|
|
|
"zero/core/load"
|
|
|
|
"zero/core/logx"
|
|
|
|
"zero/core/logx"
|
|
|
|
"zero/core/netx"
|
|
|
|
"zero/core/netx"
|
|
|
|
"zero/core/rpc"
|
|
|
|
|
|
|
|
"zero/core/rpc/serverinterceptors"
|
|
|
|
|
|
|
|
"zero/core/stat"
|
|
|
|
"zero/core/stat"
|
|
|
|
"zero/rpcx/auth"
|
|
|
|
"zero/rpcx/internal"
|
|
|
|
"zero/rpcx/interceptors"
|
|
|
|
"zero/rpcx/internal/auth"
|
|
|
|
|
|
|
|
"zero/rpcx/internal/serverinterceptors"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
const envPodIp = "POD_IP"
|
|
|
|
const envPodIp = "POD_IP"
|
|
|
|
|
|
|
|
|
|
|
|
type RpcServer struct {
|
|
|
|
type RpcServer struct {
|
|
|
|
server rpc.Server
|
|
|
|
server internal.Server
|
|
|
|
register rpc.RegisterFn
|
|
|
|
register internal.RegisterFn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func MustNewServer(c RpcServerConf, register rpc.RegisterFn) *RpcServer {
|
|
|
|
func MustNewServer(c RpcServerConf, register internal.RegisterFn) *RpcServer {
|
|
|
|
server, err := NewServer(c, register)
|
|
|
|
server, err := NewServer(c, register)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
log.Fatal(err)
|
|
|
@ -32,22 +31,22 @@ func MustNewServer(c RpcServerConf, register rpc.RegisterFn) *RpcServer {
|
|
|
|
return server
|
|
|
|
return server
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func NewServer(c RpcServerConf, register rpc.RegisterFn) (*RpcServer, error) {
|
|
|
|
func NewServer(c RpcServerConf, register internal.RegisterFn) (*RpcServer, error) {
|
|
|
|
var err error
|
|
|
|
var err error
|
|
|
|
if err = c.Validate(); err != nil {
|
|
|
|
if err = c.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var server rpc.Server
|
|
|
|
var server internal.Server
|
|
|
|
metrics := stat.NewMetrics(c.ListenOn)
|
|
|
|
metrics := stat.NewMetrics(c.ListenOn)
|
|
|
|
if c.HasEtcd() {
|
|
|
|
if c.HasEtcd() {
|
|
|
|
listenOn := figureOutListenOn(c.ListenOn)
|
|
|
|
listenOn := figureOutListenOn(c.ListenOn)
|
|
|
|
server, err = rpc.NewRpcPubServer(c.Etcd.Hosts, c.Etcd.Key, listenOn, rpc.WithMetrics(metrics))
|
|
|
|
server, err = internal.NewRpcPubServer(c.Etcd.Hosts, c.Etcd.Key, listenOn, internal.WithMetrics(metrics))
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
server = rpc.NewRpcServer(c.ListenOn, rpc.WithMetrics(metrics))
|
|
|
|
server = internal.NewRpcServer(c.ListenOn, internal.WithMetrics(metrics))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
server.SetName(c.Name)
|
|
|
|
server.SetName(c.Name)
|
|
|
@ -99,7 +98,7 @@ func figureOutListenOn(listenOn string) string {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func setupInterceptors(server rpc.Server, c RpcServerConf, metrics *stat.Metrics) error {
|
|
|
|
func setupInterceptors(server internal.Server, c RpcServerConf, metrics *stat.Metrics) error {
|
|
|
|
if c.CpuThreshold > 0 {
|
|
|
|
if c.CpuThreshold > 0 {
|
|
|
|
shedder := load.NewAdaptiveShedder(load.WithCpuThreshold(c.CpuThreshold))
|
|
|
|
shedder := load.NewAdaptiveShedder(load.WithCpuThreshold(c.CpuThreshold))
|
|
|
|
server.AddUnaryInterceptors(serverinterceptors.UnarySheddingInterceptor(shedder, metrics))
|
|
|
|
server.AddUnaryInterceptors(serverinterceptors.UnarySheddingInterceptor(shedder, metrics))
|
|
|
@ -118,8 +117,8 @@ func setupInterceptors(server rpc.Server, c RpcServerConf, metrics *stat.Metrics
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
server.AddStreamInterceptors(interceptors.StreamAuthorizeInterceptor(authenticator))
|
|
|
|
server.AddStreamInterceptors(internal.StreamAuthorizeInterceptor(authenticator))
|
|
|
|
server.AddUnaryInterceptors(interceptors.UnaryAuthorizeInterceptor(authenticator))
|
|
|
|
server.AddUnaryInterceptors(internal.UnaryAuthorizeInterceptor(authenticator))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|