fix: NewClientWithTarget miss default config (#3358)

This commit is contained in:
MarkJoyMa
2023-06-16 23:29:52 +08:00
committed by GitHub
parent 71e8230e65
commit 92f6c48349
2 changed files with 19 additions and 7 deletions

View File

@@ -3,6 +3,7 @@ package zrpc
import ( import (
"time" "time"
"github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/zrpc/internal" "github.com/zeromicro/go-zero/zrpc/internal"
"github.com/zeromicro/go-zero/zrpc/internal/auth" "github.com/zeromicro/go-zero/zrpc/internal/auth"
@@ -85,15 +86,14 @@ func NewClient(c RpcClientConf, options ...ClientOption) (Client, error) {
// NewClientWithTarget returns a Client with connecting to given target. // NewClientWithTarget returns a Client with connecting to given target.
func NewClientWithTarget(target string, opts ...ClientOption) (Client, error) { func NewClientWithTarget(target string, opts ...ClientOption) (Client, error) {
middlewares := ClientMiddlewaresConf{ var config RpcClientConf
Trace: true, if err := conf.FillDefault(&config); err != nil {
Duration: true, return nil, err
Prometheus: true,
Breaker: true,
Timeout: true,
} }
return internal.NewClient(target, middlewares, opts...) config.Target = target
return NewClient(config, opts...)
} }
// Conn returns the underlying grpc.ClientConn. // Conn returns the underlying grpc.ClientConn.

View File

@@ -215,3 +215,15 @@ func TestNewClientWithError(t *testing.T) {
) )
assert.NotNil(t, err) assert.NotNil(t, err)
} }
func TestNewClientWithTarget(t *testing.T) {
_, err := NewClientWithTarget("",
WithDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())),
WithDialOption(grpc.WithContextDialer(dialer())),
WithUnaryClientInterceptor(func(ctx context.Context, method string, req, reply any,
cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
return invoker(ctx, method, req, reply, cc, opts...)
}))
assert.NotNil(t, err)
}