fix: remove deprecated dependencies (#1837)

* fix: remove deprecated dependencies

* backup

* fix test error
master
Kevin Wan 3 years ago committed by GitHub
parent c3a49ece8d
commit f7a60cdc24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1298,8 +1298,9 @@ func (s *Redis) Pipelined(fn func(Pipeliner) error) error {
} }
// PipelinedCtx lets fn execute pipelined commands. // PipelinedCtx lets fn execute pipelined commands.
func (s *Redis) PipelinedCtx(ctx context.Context, fn func(Pipeliner) error) (err error) { // Results need to be retrieved by calling Pipeline.Exec()
err = s.brk.DoWithAcceptable(func() error { func (s *Redis) PipelinedCtx(ctx context.Context, fn func(Pipeliner) error) error {
return s.brk.DoWithAcceptable(func() error {
conn, err := getRedis(s) conn, err := getRedis(s)
if err != nil { if err != nil {
return err return err
@ -1308,8 +1309,6 @@ func (s *Redis) PipelinedCtx(ctx context.Context, fn func(Pipeliner) error) (err
_, err = conn.Pipelined(ctx, fn) _, err = conn.Pipelined(ctx, fn)
return err return err
}, acceptable) }, acceptable)
return
} }
// Rpop is the implementation of redis rpop command. // Rpop is the implementation of redis rpop command.

@ -41,7 +41,7 @@ type (
AuthorizeOption func(opts *AuthorizeOptions) AuthorizeOption func(opts *AuthorizeOptions)
) )
// Authorize returns an authorize middleware. // Authorize returns an authorization middleware.
func Authorize(secret string, opts ...AuthorizeOption) func(http.Handler) http.Handler { func Authorize(secret string, opts ...AuthorizeOption) func(http.Handler) http.Handler {
var authOpts AuthorizeOptions var authOpts AuthorizeOptions
for _, opt := range opts { for _, opt := range opts {

@ -14,6 +14,7 @@ import (
"github.com/zeromicro/go-zero/zrpc/internal/mock" "github.com/zeromicro/go-zero/zrpc/internal/mock"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/codes" "google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/status" "google.golang.org/grpc/status"
"google.golang.org/grpc/test/bufconn" "google.golang.org/grpc/test/bufconn"
) )
@ -103,14 +104,15 @@ func TestDepositServer_Deposit(t *testing.T) {
Token: "bar", Token: "bar",
Timeout: 1000, Timeout: 1000,
}, },
WithDialOption(grpc.WithInsecure()), WithDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())),
WithDialOption(grpc.WithContextDialer(dialer())), WithDialOption(grpc.WithContextDialer(dialer())),
WithUnaryClientInterceptor(func(ctx context.Context, method string, req, reply interface{}, WithUnaryClientInterceptor(func(ctx context.Context, method string, req, reply interface{},
cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
return invoker(ctx, method, req, reply, cc, opts...) return invoker(ctx, method, req, reply, cc, opts...)
}), }),
) )
targetClient, err := NewClientWithTarget("foo", WithDialOption(grpc.WithInsecure()), targetClient, err := NewClientWithTarget("foo",
WithDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())),
WithDialOption(grpc.WithContextDialer(dialer())), WithUnaryClientInterceptor( WithDialOption(grpc.WithContextDialer(dialer())), WithUnaryClientInterceptor(
func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn,
invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
@ -162,7 +164,7 @@ func TestNewClientWithError(t *testing.T) {
Token: "bar", Token: "bar",
Timeout: 1000, Timeout: 1000,
}, },
WithDialOption(grpc.WithInsecure()), WithDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())),
WithDialOption(grpc.WithContextDialer(dialer())), WithDialOption(grpc.WithContextDialer(dialer())),
WithUnaryClientInterceptor(func(ctx context.Context, method string, req, reply interface{}, WithUnaryClientInterceptor(func(ctx context.Context, method string, req, reply interface{},
cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
@ -181,7 +183,7 @@ func TestNewClientWithError(t *testing.T) {
Token: "bar", Token: "bar",
Timeout: 1, Timeout: 1,
}, },
WithDialOption(grpc.WithInsecure()), WithDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())),
WithDialOption(grpc.WithContextDialer(dialer())), WithDialOption(grpc.WithContextDialer(dialer())),
WithUnaryClientInterceptor(func(ctx context.Context, method string, req, reply interface{}, WithUnaryClientInterceptor(func(ctx context.Context, method string, req, reply interface{},
cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {

@ -9,6 +9,7 @@ import (
"github.com/zeromicro/go-zero/zrpc/internal/mock" "github.com/zeromicro/go-zero/zrpc/internal/mock"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/codes" "google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/status" "google.golang.org/grpc/status"
) )
@ -36,7 +37,7 @@ func TestProxy(t *testing.T) {
}, },
} }
proxy := NewProxy("foo", WithDialOption(grpc.WithInsecure()), proxy := NewProxy("foo", WithDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())),
WithDialOption(grpc.WithContextDialer(dialer()))) WithDialOption(grpc.WithContextDialer(dialer())))
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
@ -66,7 +67,8 @@ func TestProxy(t *testing.T) {
} }
func TestRpcProxy_TakeConnNewClientFailed(t *testing.T) { func TestRpcProxy_TakeConnNewClientFailed(t *testing.T) {
proxy := NewProxy("foo", WithDialOption(grpc.WithInsecure()), WithDialOption(grpc.WithBlock())) proxy := NewProxy("foo", WithDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())),
WithDialOption(grpc.WithBlock()))
_, err := proxy.TakeConn(context.Background()) _, err := proxy.TakeConn(context.Background())
assert.NotNil(t, err) assert.NotNil(t, err)
} }

@ -3,15 +3,16 @@ package internal
import ( import (
"strings" "strings"
"github.com/zeromicro/go-zero/zrpc/resolver/internal/targets"
"google.golang.org/grpc/resolver" "google.golang.org/grpc/resolver"
) )
type directBuilder struct{} type directBuilder struct{}
func (d *directBuilder) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOptions) ( func (d *directBuilder) Build(target resolver.Target, cc resolver.ClientConn, _ resolver.BuildOptions) (
resolver.Resolver, error) { resolver.Resolver, error) {
var addrs []resolver.Address var addrs []resolver.Address
endpoints := strings.FieldsFunc(target.Endpoint, func(r rune) bool { endpoints := strings.FieldsFunc(targets.GetEndpoints(target), func(r rune) bool {
return r == EndpointSepChar return r == EndpointSepChar
}) })

@ -2,6 +2,7 @@ package internal
import ( import (
"fmt" "fmt"
"net/url"
"strconv" "strconv"
"strings" "strings"
"testing" "testing"
@ -31,9 +32,11 @@ func TestDirectBuilder_Build(t *testing.T) {
} }
var b directBuilder var b directBuilder
cc := new(mockedClientConn) cc := new(mockedClientConn)
_, err := b.Build(resolver.Target{ target := fmt.Sprintf("%s:///%s", DirectScheme, strings.Join(servers, ","))
Scheme: DirectScheme, uri, err := url.Parse(target)
Endpoint: strings.Join(servers, ","), assert.Nil(t, err)
_, err = b.Build(resolver.Target{
URL: *uri,
}, cc, resolver.BuildOptions{}) }, cc, resolver.BuildOptions{})
assert.Nil(t, err) assert.Nil(t, err)
size := mathx.MinInt(test, subsetSize) size := mathx.MinInt(test, subsetSize)

@ -5,6 +5,7 @@ import (
"github.com/zeromicro/go-zero/core/discov" "github.com/zeromicro/go-zero/core/discov"
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/zrpc/resolver/internal/targets"
"google.golang.org/grpc/resolver" "google.golang.org/grpc/resolver"
) )
@ -12,10 +13,10 @@ type discovBuilder struct{}
func (b *discovBuilder) Build(target resolver.Target, cc resolver.ClientConn, _ resolver.BuildOptions) ( func (b *discovBuilder) Build(target resolver.Target, cc resolver.ClientConn, _ resolver.BuildOptions) (
resolver.Resolver, error) { resolver.Resolver, error) {
hosts := strings.FieldsFunc(target.Authority, func(r rune) bool { hosts := strings.FieldsFunc(targets.GetAuthority(target), func(r rune) bool {
return r == EndpointSepChar return r == EndpointSepChar
}) })
sub, err := discov.NewSubscriber(hosts, target.Endpoint) sub, err := discov.NewSubscriber(hosts, targets.GetEndpoints(target))
if err != nil { if err != nil {
return nil, err return nil, err
} }

@ -5,6 +5,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/zeromicro/go-zero/zrpc/resolver/internal/targets"
"google.golang.org/grpc/resolver" "google.golang.org/grpc/resolver"
) )
@ -25,14 +26,15 @@ type Service struct {
// ParseTarget parses the resolver.Target. // ParseTarget parses the resolver.Target.
func ParseTarget(target resolver.Target) (Service, error) { func ParseTarget(target resolver.Target) (Service, error) {
var service Service var service Service
service.Namespace = target.Authority service.Namespace = targets.GetAuthority(target)
if len(service.Namespace) == 0 { if len(service.Namespace) == 0 {
service.Namespace = defaultNamespace service.Namespace = defaultNamespace
} }
segs := strings.SplitN(target.Endpoint, colon, 2) endpoints := targets.GetEndpoints(target)
segs := strings.SplitN(endpoints, colon, 2)
if len(segs) < 2 { if len(segs) < 2 {
return emptyService, fmt.Errorf("bad endpoint: %s", target.Endpoint) return emptyService, fmt.Errorf("bad endpoint: %s", endpoints)
} }
service.Name = segs[0] service.Name = segs[0]

@ -1,6 +1,7 @@
package kube package kube
import ( import (
"net/url"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -10,17 +11,13 @@ import (
func TestParseTarget(t *testing.T) { func TestParseTarget(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
input resolver.Target input string
expect Service expect Service
hasErr bool hasErr bool
}{ }{
{ {
name: "normal case", name: "normal case",
input: resolver.Target{ input: "k8s://ns1/my-svc:8080",
Scheme: "k8s",
Authority: "ns1",
Endpoint: "my-svc:8080",
},
expect: Service{ expect: Service{
Namespace: "ns1", Namespace: "ns1",
Name: "my-svc", Name: "my-svc",
@ -29,11 +26,7 @@ func TestParseTarget(t *testing.T) {
}, },
{ {
name: "normal case", name: "normal case",
input: resolver.Target{ input: "k8s:///my-svc:8080",
Scheme: "k8s",
Authority: "",
Endpoint: "my-svc:8080",
},
expect: Service{ expect: Service{
Namespace: defaultNamespace, Namespace: defaultNamespace,
Name: "my-svc", Name: "my-svc",
@ -42,36 +35,26 @@ func TestParseTarget(t *testing.T) {
}, },
{ {
name: "no port", name: "no port",
input: resolver.Target{ input: "k8s://ns1/my-svc:",
Scheme: "k8s",
Authority: "ns1",
Endpoint: "my-svc:",
},
hasErr: true, hasErr: true,
}, },
{ {
name: "no port, no colon", name: "no port, no colon",
input: resolver.Target{ input: "k8s://ns1/my-svc",
Scheme: "k8s",
Authority: "ns1",
Endpoint: "my-svc",
},
hasErr: true, hasErr: true,
}, },
{ {
name: "bad port", name: "bad port",
input: resolver.Target{ input: "k8s://ns1/my-svc:800a",
Scheme: "k8s",
Authority: "ns1",
Endpoint: "my-svc:800a",
},
hasErr: true, hasErr: true,
}, },
} }
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
svc, err := ParseTarget(test.input) uri, err := url.Parse(test.input)
assert.Nil(t, err)
svc, err := ParseTarget(resolver.Target{URL: *uri})
if test.hasErr { if test.hasErr {
assert.NotNil(t, err) assert.NotNil(t, err)
} else { } else {

@ -0,0 +1,19 @@
package targets
import (
"strings"
"google.golang.org/grpc/resolver"
)
const slashSeparator = "/"
// GetAuthority returns the authority of the target.
func GetAuthority(target resolver.Target) string {
return target.URL.Host
}
// GetEndpoints returns the endpoints from the given target.
func GetEndpoints(target resolver.Target) string {
return strings.Trim(target.URL.Path, slashSeparator)
}

@ -0,0 +1,89 @@
package targets
import (
"net/url"
"testing"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc/resolver"
)
func TestGetAuthority(t *testing.T) {
tests := []struct {
name string
url string
want string
}{
{
name: "test",
url: "direct://my_authority/localhost",
want: "my_authority",
},
{
name: "test with port",
url: "direct://my_authority/localhost:8080",
want: "my_authority",
},
{
name: "test with multiple hosts",
url: "direct://my_authority1,my_authority2/localhost,localhost",
want: "my_authority1,my_authority2",
},
{
name: "test with multiple hosts with port",
url: "direct://my_authority1:3000,my_authority2:3001/localhost:8080,localhost:8081",
want: "my_authority1:3000,my_authority2:3001",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
uri, err := url.Parse(test.url)
assert.Nil(t, err)
target := resolver.Target{
URL: *uri,
}
assert.Equal(t, test.want, GetAuthority(target))
})
}
}
func TestGetEndpoints(t *testing.T) {
tests := []struct {
name string
url string
want string
}{
{
name: "test",
url: "direct:///localhost",
want: "localhost",
},
{
name: "test with port",
url: "direct:///localhost:8080",
want: "localhost:8080",
},
{
name: "test with multiple hosts",
url: "direct:///localhost,localhost",
want: "localhost,localhost",
},
{
name: "test with multiple hosts with port",
url: "direct:///localhost:8080,localhost:8081",
want: "localhost:8080,localhost:8081",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
uri, err := url.Parse(test.url)
assert.Nil(t, err)
target := resolver.Target{
URL: *uri,
}
assert.Equal(t, test.want, GetEndpoints(target))
})
}
}
Loading…
Cancel
Save