Skip to content

Commit

Permalink
refactor rpcx, export WithDialOption and WithTimeout
Browse files Browse the repository at this point in the history
  • Loading branch information
kevwan committed Aug 11, 2020
1 parent 9522676 commit 08fa64d
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 20 deletions.
2 changes: 1 addition & 1 deletion example/graceful/dns/api/svc/servicecontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package svc
import "github.com/tal-tech/go-zero/rpcx"

type ServiceContext struct {
Client *rpcx.RpcClient
Client rpcx.Client
}
2 changes: 1 addition & 1 deletion example/graceful/etcd/api/svc/servicecontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package svc
import "github.com/tal-tech/go-zero/rpcx"

type ServiceContext struct {
Client *rpcx.RpcClient
Client rpcx.Client
}
2 changes: 1 addition & 1 deletion example/tracing/edge/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

var (
configFile = flag.String("f", "config.json", "the config file")
client *rpcx.RpcClient
client rpcx.Client
)

func handle(w http.ResponseWriter, r *http.Request) {
Expand Down
4 changes: 2 additions & 2 deletions example/tracing/portal/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ type (
}

PortalServer struct {
userRpc *rpcx.RpcClient
userRpc rpcx.Client
}
)

func NewPortalServer(client *rpcx.RpcClient) *PortalServer {
func NewPortalServer(client rpcx.Client) *PortalServer {
return &PortalServer{
userRpc: client,
}
Expand Down
29 changes: 20 additions & 9 deletions rpcx/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,22 @@ import (
"google.golang.org/grpc"
)

type RpcClient struct {
client internal.Client
}
var (
WithDialOption = internal.WithDialOption
WithTimeout = internal.WithTimeout
)

type (
Client interface {
Conn() *grpc.ClientConn
}

RpcClient struct {
client Client
}
)

func MustNewClient(c RpcClientConf, options ...internal.ClientOption) *RpcClient {
func MustNewClient(c RpcClientConf, options ...internal.ClientOption) Client {
cli, err := NewClient(c, options...)
if err != nil {
log.Fatal(err)
Expand All @@ -23,20 +34,20 @@ func MustNewClient(c RpcClientConf, options ...internal.ClientOption) *RpcClient
return cli
}

func NewClient(c RpcClientConf, options ...internal.ClientOption) (*RpcClient, error) {
func NewClient(c RpcClientConf, options ...internal.ClientOption) (Client, error) {
var opts []internal.ClientOption
if c.HasCredential() {
opts = append(opts, internal.WithDialOption(grpc.WithPerRPCCredentials(&auth.Credential{
opts = append(opts, WithDialOption(grpc.WithPerRPCCredentials(&auth.Credential{
App: c.App,
Token: c.Token,
})))
}
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...)

var client internal.Client
var client Client
var err error
if len(c.Server) > 0 {
client, err = internal.NewDirectClient(c.Server, opts...)
Expand All @@ -52,7 +63,7 @@ func NewClient(c RpcClientConf, options ...internal.ClientOption) (*RpcClient, e
}, nil
}

func NewClientNoAuth(c discov.EtcdConf) (*RpcClient, error) {
func NewClientNoAuth(c discov.EtcdConf) (Client, error) {
client, err := internal.NewDiscovClient(c.Hosts, c.Key)
if err != nil {
return nil, err
Expand Down
4 changes: 0 additions & 4 deletions rpcx/internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ type (
}

ClientOption func(options *ClientOptions)

Client interface {
Conn() *grpc.ClientConn
}
)

func WithDialOption(opt grpc.DialOption) ClientOption {
Expand Down
4 changes: 2 additions & 2 deletions rpcx/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

type RpcProxy struct {
backend string
clients map[string]*RpcClient
clients map[string]Client
options []internal.ClientOption
sharedCalls syncx.SharedCalls
lock sync.Mutex
Expand All @@ -21,7 +21,7 @@ type RpcProxy struct {
func NewRpcProxy(backend string, opts ...internal.ClientOption) *RpcProxy {
return &RpcProxy{
backend: backend,
clients: make(map[string]*RpcClient),
clients: make(map[string]Client),
options: opts,
sharedCalls: syncx.NewSharedCalls(),
}
Expand Down

0 comments on commit 08fa64d

Please sign in to comment.