refactor rpcx, export WithDialOption and WithTimeout
This commit is contained in:
@@ -3,5 +3,5 @@ package svc
|
|||||||
import "github.com/tal-tech/go-zero/rpcx"
|
import "github.com/tal-tech/go-zero/rpcx"
|
||||||
|
|
||||||
type ServiceContext struct {
|
type ServiceContext struct {
|
||||||
Client *rpcx.RpcClient
|
Client rpcx.Client
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,5 +3,5 @@ package svc
|
|||||||
import "github.com/tal-tech/go-zero/rpcx"
|
import "github.com/tal-tech/go-zero/rpcx"
|
||||||
|
|
||||||
type ServiceContext struct {
|
type ServiceContext struct {
|
||||||
Client *rpcx.RpcClient
|
Client rpcx.Client
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ import (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
configFile = flag.String("f", "config.json", "the config file")
|
configFile = flag.String("f", "config.json", "the config file")
|
||||||
client *rpcx.RpcClient
|
client rpcx.Client
|
||||||
)
|
)
|
||||||
|
|
||||||
func handle(w http.ResponseWriter, r *http.Request) {
|
func handle(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|||||||
@@ -20,11 +20,11 @@ type (
|
|||||||
}
|
}
|
||||||
|
|
||||||
PortalServer struct {
|
PortalServer struct {
|
||||||
userRpc *rpcx.RpcClient
|
userRpc rpcx.Client
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewPortalServer(client *rpcx.RpcClient) *PortalServer {
|
func NewPortalServer(client rpcx.Client) *PortalServer {
|
||||||
return &PortalServer{
|
return &PortalServer{
|
||||||
userRpc: client,
|
userRpc: client,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,11 +10,22 @@ import (
|
|||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
)
|
)
|
||||||
|
|
||||||
type RpcClient struct {
|
var (
|
||||||
client internal.Client
|
WithDialOption = internal.WithDialOption
|
||||||
}
|
WithTimeout = internal.WithTimeout
|
||||||
|
)
|
||||||
|
|
||||||
func MustNewClient(c RpcClientConf, options ...internal.ClientOption) *RpcClient {
|
type (
|
||||||
|
Client interface {
|
||||||
|
Conn() *grpc.ClientConn
|
||||||
|
}
|
||||||
|
|
||||||
|
RpcClient struct {
|
||||||
|
client Client
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func MustNewClient(c RpcClientConf, options ...internal.ClientOption) Client {
|
||||||
cli, err := NewClient(c, options...)
|
cli, err := NewClient(c, options...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
@@ -23,20 +34,20 @@ func MustNewClient(c RpcClientConf, options ...internal.ClientOption) *RpcClient
|
|||||||
return cli
|
return cli
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(c RpcClientConf, options ...internal.ClientOption) (*RpcClient, error) {
|
func NewClient(c RpcClientConf, options ...internal.ClientOption) (Client, error) {
|
||||||
var opts []internal.ClientOption
|
var opts []internal.ClientOption
|
||||||
if c.HasCredential() {
|
if c.HasCredential() {
|
||||||
opts = append(opts, internal.WithDialOption(grpc.WithPerRPCCredentials(&auth.Credential{
|
opts = append(opts, 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, internal.WithTimeout(time.Duration(c.Timeout)*time.Millisecond))
|
opts = append(opts, WithTimeout(time.Duration(c.Timeout)*time.Millisecond))
|
||||||
}
|
}
|
||||||
opts = append(opts, options...)
|
opts = append(opts, options...)
|
||||||
|
|
||||||
var client internal.Client
|
var client Client
|
||||||
var err error
|
var err error
|
||||||
if len(c.Server) > 0 {
|
if len(c.Server) > 0 {
|
||||||
client, err = internal.NewDirectClient(c.Server, opts...)
|
client, err = internal.NewDirectClient(c.Server, opts...)
|
||||||
@@ -52,7 +63,7 @@ func NewClient(c RpcClientConf, options ...internal.ClientOption) (*RpcClient, e
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClientNoAuth(c discov.EtcdConf) (*RpcClient, error) {
|
func NewClientNoAuth(c discov.EtcdConf) (Client, error) {
|
||||||
client, err := internal.NewDiscovClient(c.Hosts, c.Key)
|
client, err := internal.NewDiscovClient(c.Hosts, c.Key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
@@ -18,10 +18,6 @@ type (
|
|||||||
}
|
}
|
||||||
|
|
||||||
ClientOption func(options *ClientOptions)
|
ClientOption func(options *ClientOptions)
|
||||||
|
|
||||||
Client interface {
|
|
||||||
Conn() *grpc.ClientConn
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func WithDialOption(opt grpc.DialOption) ClientOption {
|
func WithDialOption(opt grpc.DialOption) ClientOption {
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import (
|
|||||||
|
|
||||||
type RpcProxy struct {
|
type RpcProxy struct {
|
||||||
backend string
|
backend string
|
||||||
clients map[string]*RpcClient
|
clients map[string]Client
|
||||||
options []internal.ClientOption
|
options []internal.ClientOption
|
||||||
sharedCalls syncx.SharedCalls
|
sharedCalls syncx.SharedCalls
|
||||||
lock sync.Mutex
|
lock sync.Mutex
|
||||||
@@ -21,7 +21,7 @@ type RpcProxy struct {
|
|||||||
func NewRpcProxy(backend string, opts ...internal.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]Client),
|
||||||
options: opts,
|
options: opts,
|
||||||
sharedCalls: syncx.NewSharedCalls(),
|
sharedCalls: syncx.NewSharedCalls(),
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user