Skip to content

Commit

Permalink
ftr: triple multi params support (#1344)
Browse files Browse the repository at this point in the history
* ftr: triple multi params support

* fix: import

* fix: fix ci and gomod

* fix: fmt

* fix: add develop back to branch
  • Loading branch information
LaurenceLiZhixin authored Jul 29, 2021
1 parent fb0d226 commit ff50bd2
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 48 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/github-actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: CI

on:
push:
branches: [master, develop]
branches: [master, develop, "1.5", "3.0"]
pull_request:
branches: "*"

Expand Down
7 changes: 1 addition & 6 deletions common/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import (

import (
"dubbo.apache.org/dubbo-go/v3/common/constant"
"dubbo.apache.org/dubbo-go/v3/common/logger"
)

// dubbo role type constant
Expand Down Expand Up @@ -206,11 +205,7 @@ func WithToken(token string) Option {
if len(token) > 0 {
value := token
if strings.ToLower(token) == "true" || strings.ToLower(token) == "default" {
u, err := uuid.NewV4()
if err != nil {
logger.Errorf("could not generator UUID: %v", err)
return
}
u := uuid.NewV4()
value = u.String()
}
url.SetParam(constant.TOKEN_KEY, value)
Expand Down
14 changes: 5 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,26 @@ 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
github.com/ghodss/yaml v1.0.0
github.com/go-co-op/gocron v0.1.1
github.com/go-resty/resty/v2 v2.3.0
github.com/golang/mock v1.4.4
github.com/golang/protobuf v1.5.2
github.com/grpc-ecosystem/grpc-opentracing v0.0.0-20180507213350-8e809c8a8645
github.com/hashicorp/vault/sdk v0.1.14-0.20200519221838-e0cfd64bc267
github.com/jinzhu/copier v0.0.0-20190625015134-976e0346caa8
github.com/hashicorp/vault/sdk v0.2.1
github.com/jinzhu/copier v0.3.2
github.com/magiconair/properties v1.8.5
github.com/mitchellh/mapstructure v1.4.1
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd
github.com/nacos-group/nacos-sdk-go v1.0.8
github.com/natefinch/lumberjack v2.0.0+incompatible
github.com/opentracing/opentracing-go v1.2.0
github.com/pierrec/lz4 v2.2.6+incompatible // indirect
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.9.0
github.com/satori/go.uuid v1.2.1-0.20181028125025-b2ce2384e17b
github.com/stretchr/objx v0.2.0 // indirect
github.com/satori/go.uuid v1.2.0
github.com/stretchr/testify v1.7.0
github.com/zouyx/agollo/v3 v3.4.5
go.etcd.io/etcd/api/v3 v3.5.0-alpha.0
Expand All @@ -44,10 +41,9 @@ require (
go.uber.org/zap v1.16.0
google.golang.org/grpc v1.38.0
google.golang.org/protobuf v1.26.0
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/yaml.v2 v2.4.0
k8s.io/api v0.16.9
k8s.io/apimachinery v0.16.9
k8s.io/client-go v0.16.9
k8s.io/kube-openapi v0.0.0-20191107075043-30be4d16710a // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
)
104 changes: 86 additions & 18 deletions go.sum

Large diffs are not rendered by default.

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 {
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)
}
}
4 changes: 2 additions & 2 deletions registry/service_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ type ServiceInstance interface {

// nolint
type Endpoint struct {
Port int `json:"port, omitempty"`
Protocol string `json:"protocol, omitempty"`
Port int `json:"port,omitempty"`
Protocol string `json:"protocol,omitempty"`
}

// DefaultServiceInstance the default implementation of ServiceInstance
Expand Down

0 comments on commit ff50bd2

Please sign in to comment.