master
kevin 4 years ago
parent ffddfa62bb
commit aa3ef20011

@ -4,18 +4,18 @@ import (
"log"
"time"
"zero/core/discov"
"zero/core/rpc"
"zero/rpcx/auth"
"google.golang.org/grpc"
"zero/core/discov"
"zero/rpcx/internal"
"zero/rpcx/internal/auth"
)
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...)
if err != nil {
log.Fatal(err)
@ -24,25 +24,25 @@ func MustNewClient(c RpcClientConf, options ...rpc.ClientOption) *RpcClient {
return cli
}
func NewClient(c RpcClientConf, options ...rpc.ClientOption) (*RpcClient, error) {
var opts []rpc.ClientOption
func NewClient(c RpcClientConf, options ...internal.ClientOption) (*RpcClient, error) {
var opts []internal.ClientOption
if c.HasCredential() {
opts = append(opts, rpc.WithDialOption(grpc.WithPerRPCCredentials(&auth.Credential{
opts = append(opts, internal.WithDialOption(grpc.WithPerRPCCredentials(&auth.Credential{
App: c.App,
Token: c.Token,
})))
}
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...)
var client rpc.Client
var client internal.Client
var err error
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 {
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 {
return nil, err
@ -54,7 +54,7 @@ func NewClient(c RpcClientConf, options ...rpc.ClientOption) (*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 {
return nil, err
}

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

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

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

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

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

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

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

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

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

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

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

Loading…
Cancel
Save