master
kevin 4 years ago
parent ffddfa62bb
commit aa3ef20011

@ -4,18 +4,18 @@ import (
"log" "log"
"time" "time"
"zero/core/discov"
"zero/core/rpc"
"zero/rpcx/auth"
"google.golang.org/grpc" "google.golang.org/grpc"
"zero/core/discov"
"zero/rpcx/internal"
"zero/rpcx/internal/auth"
) )
type RpcClient struct { type RpcClient struct {
client rpc.Client client internal.Client
} }
func MustNewClient(c RpcClientConf, options ...rpc.ClientOption) *RpcClient { func MustNewClient(c RpcClientConf, options ...internal.ClientOption) *RpcClient {
cli, err := NewClient(c, options...) cli, err := NewClient(c, options...)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
@ -24,25 +24,25 @@ func MustNewClient(c RpcClientConf, options ...rpc.ClientOption) *RpcClient {
return cli return cli
} }
func NewClient(c RpcClientConf, options ...rpc.ClientOption) (*RpcClient, error) { func NewClient(c RpcClientConf, options ...internal.ClientOption) (*RpcClient, error) {
var opts []rpc.ClientOption var opts []internal.ClientOption
if c.HasCredential() { if c.HasCredential() {
opts = append(opts, rpc.WithDialOption(grpc.WithPerRPCCredentials(&auth.Credential{ opts = append(opts, internal.WithDialOption(grpc.WithPerRPCCredentials(&auth.Credential{
App: c.App, App: c.App,
Token: c.Token, Token: c.Token,
}))) })))
} }
if c.Timeout > 0 { if c.Timeout > 0 {
opts = append(opts, rpc.WithTimeout(time.Duration(c.Timeout)*time.Millisecond)) opts = append(opts, internal.WithTimeout(time.Duration(c.Timeout)*time.Millisecond))
} }
opts = append(opts, options...) opts = append(opts, options...)
var client rpc.Client var client internal.Client
var err error var err error
if len(c.Server) > 0 { if len(c.Server) > 0 {
client, err = rpc.NewDirectClient(c.Server, opts...) client, err = internal.NewDirectClient(c.Server, opts...)
} else if err = c.Etcd.Validate(); err == nil { } else if err = c.Etcd.Validate(); err == nil {
client, err = rpc.NewRoundRobinRpcClient(c.Etcd.Hosts, c.Etcd.Key, opts...) client, err = internal.NewRoundRobinRpcClient(c.Etcd.Hosts, c.Etcd.Key, opts...)
} }
if err != nil { if err != nil {
return nil, err return nil, err
@ -54,7 +54,7 @@ func NewClient(c RpcClientConf, options ...rpc.ClientOption) (*RpcClient, error)
} }
func NewClientNoAuth(c discov.EtcdConf) (*RpcClient, error) { func NewClientNoAuth(c discov.EtcdConf) (*RpcClient, error) {
client, err := rpc.NewRoundRobinRpcClient(c.Hosts, c.Key) client, err := internal.NewRoundRobinRpcClient(c.Hosts, c.Key)
if err != nil { if err != nil {
return nil, err return nil, err
} }

@ -1,9 +1,9 @@
package interceptors package internal
import ( import (
"context" "context"
"zero/rpcx/auth" "zero/rpcx/internal/auth"
"google.golang.org/grpc" "google.golang.org/grpc"
) )

@ -1,4 +1,4 @@
package rpc package internal
import ( import (
"context" "context"

@ -1,4 +1,4 @@
package rpc package internal
import ( import (
"context" "context"

@ -1,11 +1,11 @@
package rpc package internal
import ( import (
"context" "context"
"fmt" "fmt"
"time" "time"
"zero/core/rpc/clientinterceptors" "zero/rpcx/internal/clientinterceptors"
"google.golang.org/grpc" "google.golang.org/grpc"
) )

@ -1,4 +1,4 @@
package rpc package internal
import ( import (
"google.golang.org/grpc" "google.golang.org/grpc"

@ -1,4 +1,4 @@
package rpc package internal
import ( import (
"sync" "sync"

@ -1,4 +1,4 @@
package rpc package internal
import "zero/core/discov" import "zero/core/discov"

@ -1,11 +1,11 @@
package rpc package internal
import ( import (
"net" "net"
"zero/core/proc" "zero/core/proc"
"zero/core/rpc/serverinterceptors"
"zero/core/stat" "zero/core/stat"
"zero/rpcx/internal/serverinterceptors"
"google.golang.org/grpc" "google.golang.org/grpc"
) )

@ -1,4 +1,4 @@
package rpc package internal
import ( import (
"time" "time"

@ -1,4 +1,4 @@
package rpc package internal
import ( import (
"math/rand" "math/rand"

@ -1,4 +1,4 @@
package rpc package internal
import ( import (
"zero/core/stat" "zero/core/stat"

@ -4,22 +4,22 @@ import (
"context" "context"
"sync" "sync"
"zero/core/rpc"
"zero/core/syncx"
"zero/rpcx/auth"
"google.golang.org/grpc" "google.golang.org/grpc"
"zero/core/syncx"
"zero/rpcx/internal"
"zero/rpcx/internal/auth"
) )
type RpcProxy struct { type RpcProxy struct {
backend string backend string
clients map[string]*RpcClient clients map[string]*RpcClient
options []rpc.ClientOption options []internal.ClientOption
sharedCalls syncx.SharedCalls sharedCalls syncx.SharedCalls
lock sync.Mutex lock sync.Mutex
} }
func NewRpcProxy(backend string, opts ...rpc.ClientOption) *RpcProxy { func NewRpcProxy(backend string, opts ...internal.ClientOption) *RpcProxy {
return &RpcProxy{ return &RpcProxy{
backend: backend, backend: backend,
clients: make(map[string]*RpcClient), clients: make(map[string]*RpcClient),

@ -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

Loading…
Cancel
Save