Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ftr: triple multi params support #1344

Merged
merged 6 commits into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/creasty/defaults v1.5.1
github.com/dubbogo/go-zookeeper v1.0.3
github.com/dubbogo/gost v1.11.14
github.com/dubbogo/triple v1.0.1
github.com/dubbogo/triple v1.0.2
github.com/emicklei/go-restful/v3 v3.4.0
github.com/frankban/quicktest v1.4.1 // indirect
github.com/fsnotify/fsnotify v1.4.9
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ github.com/dubbogo/gost v1.11.14/go.mod h1:vIcP9rqz2KsXHPjsAwIUtfJIJjppQLQDcYaZT
github.com/dubbogo/jsonparser v1.0.1/go.mod h1:tYAtpctvSP/tWw4MeelsowSPgXQRVHHWbqL6ynps8jU=
github.com/dubbogo/net v0.0.3 h1:2k53mh+1U8h1gFjJ8ykzyP4wNdAdgjc5moD+xVHI/AE=
github.com/dubbogo/net v0.0.3/go.mod h1:B6/ka3g8VzcyrmdCH4VkHP1K0aHeI37FmclS+TCwIBU=
github.com/dubbogo/triple v1.0.1 h1:fdBkXTR0I4UBBCDcN+k22uJWU338ZK5mvOPpIBIL5h8=
github.com/dubbogo/triple v1.0.1/go.mod h1:O6vQD2XLCWugzAk0P27HTW4+Uhkd8sjaQn0BZijdGzU=
github.com/dubbogo/triple v1.0.2 h1:+QKw6XbTyHP4yqUQ4gXRvV1FbhJZ7ldl9WrN0AjmCPM=
github.com/dubbogo/triple v1.0.2/go.mod h1:O6vQD2XLCWugzAk0P27HTW4+Uhkd8sjaQn0BZijdGzU=
github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
Expand Down
28 changes: 16 additions & 12 deletions protocol/dubbo3/dubbo3_protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,18 @@ func (dp *DubboProtocol) Export(invoker protocol.Invoker) protocol.Exporter {
valueOf := reflect.ValueOf(service)
typeOf := valueOf.Type()
numField := valueOf.NumMethod()
tripleService := &Dubbo3HessianService{proxyImpl: invoker}
tripleService := &UnaryService{proxyImpl: invoker}
for i := 0; i < numField; i++ {
ft := typeOf.Method(i)
if ft.Name == "Reference" {
continue
}
// num out is checked in common/rpc_service.go
if ft.Type.NumIn() != 3 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why delete the check cond?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thisk Block limit the number of method input params, if not 1 param, it would panic.
Now, we support any number of param.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awesome.

panic(fmt.Sprintf("function %s input params num = %d not supported, which should be 2", ft.Name, ft.Type.NumIn()-1))
// get all method params type
typs := make([]reflect.Type, 0)
for j := 2; j < ft.Type.NumIn(); j++ {
typs = append(typs, ft.Type.In(j))
}
typ := ft.Type.In(2)
tripleService.setReqParamsInterface(ft.Name, typ)
tripleService.setReqParamsTypes(ft.Name, typs)
}
service = tripleService
triSerializationType = tripleConstant.CodecType(serializationType)
Expand Down Expand Up @@ -164,25 +164,29 @@ type Dubbo3GrpcService interface {
ServiceDesc() *grpc.ServiceDesc
}

type Dubbo3HessianService struct {
type UnaryService struct {
proxyImpl protocol.Invoker
reqTypeMap sync.Map
}

func (d *Dubbo3HessianService) setReqParamsInterface(methodName string, typ reflect.Type) {
func (d *UnaryService) setReqParamsTypes(methodName string, typ []reflect.Type) {
d.reqTypeMap.Store(methodName, typ)
}

func (d *Dubbo3HessianService) GetReqParamsInteface(methodName string) (interface{}, bool) {
func (d *UnaryService) GetReqParamsInterfaces(methodName string) ([]interface{}, bool) {
val, ok := d.reqTypeMap.Load(methodName)
if !ok {
return nil, false
}
typ := val.(reflect.Type)
return reflect.New(typ).Interface(), true
typs := val.([]reflect.Type)
reqParamsInterfaces := make([]interface{}, 0, len(typs))
for _, typ := range typs {
reqParamsInterfaces = append(reqParamsInterfaces, reflect.New(typ).Interface())
}
return reqParamsInterfaces, true
}

func (d *Dubbo3HessianService) InvokeWithArgs(ctx context.Context, methodName string, arguments []interface{}) (interface{}, error) {
func (d *UnaryService) InvokeWithArgs(ctx context.Context, methodName string, arguments []interface{}) (interface{}, error) {
res := d.proxyImpl.Invoke(ctx, invocation.NewRPCInvocation(methodName, arguments, nil))
return res.Result(), res.Error()
}
Expand Down
63 changes: 63 additions & 0 deletions protocol/dubbo3/dubbo3_protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
package dubbo3

import (
"context"
"reflect"
"testing"
"time"
)

import (
hessian "github.com/apache/dubbo-go-hessian2"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -85,3 +88,63 @@ func TestDubboProtocolRefer(t *testing.T) {
invokersLen = len(proto.(*DubboProtocol).Invokers())
assert.Equal(t, 0, invokersLen)
}

type MockUser struct {
Name string
}

func (m *MockUser) JavaClassName() string {
return "mockuser"
}

type MockService struct {
}

func (m *MockService) GetUser(ctx context.Context, user, user2 *MockUser) (*MockUser, error) {
return user, nil
}

func TestDubbo3UnaryService_GetReqParamsInterfaces(t *testing.T) {
hessian.RegisterPOJO(&MockUser{})
srv := UnaryService{}
valueOf := reflect.ValueOf(&MockService{})
typeOf := valueOf.Type()
numField := valueOf.NumMethod()
for i := 0; i < numField; i++ {
ft := typeOf.Method(i)
// num in/out is checked in common/rpc_service.go
typs := make([]reflect.Type, 0)
for j := 2; j < ft.Type.NumIn(); j++ {
typs = append(typs, ft.Type.In(j))
}
srv.setReqParamsTypes("GetUser", typs)
}
paramsInterfaces, ok := srv.GetReqParamsInterfaces("GetUser")
assert.True(t, ok)
enc := hessian.NewEncoder()
err := enc.Encode(&MockUser{
Name: "laurence",
})
assert.Nil(t, err)
data := enc.Buffer()
decoder := hessian.NewDecoder(data)
val, err := decoder.Decode()
assert.Nil(t, err)
assert.Equal(t, 2, len(paramsInterfaces))
subTest(t, val, paramsInterfaces)
args := make([]interface{}, 0, 1)
for _, v := range paramsInterfaces {
tempParamObj := reflect.ValueOf(v).Elem().Interface()
args = append(args, tempParamObj)
}
assert.Equal(t, "laurence", args[0].(*MockUser).Name)
assert.Equal(t, "laurence", args[1].(*MockUser).Name)
}

func subTest(t *testing.T, val, paramsInterfaces interface{}) {
list := paramsInterfaces.([]interface{})
for k, _ := range list {
err := hessian.ReflectResponse(val, list[k])
assert.Nil(t, err)
}
}