From 94519a0af11cb4d61d677615fde39d62acde6fe0 Mon Sep 17 00:00:00 2001
From: fangyincheng <fangyincheng@sina.com>
Date: Wed, 3 Jul 2019 16:50:56 +0800
Subject: [PATCH] Mod:modify rpcinvocation.go

---
 cluster/cluster_impl/failover_cluster_test.go |  3 +-
 cluster/loadbalance/least_active_test.go      |  3 +-
 cluster/loadbalance/random_test.go            |  6 +-
 common/proxy/proxy.go                         |  4 +-
 filter/impl/echo_filter_test.go               |  4 +-
 protocol/dubbo/dubbo_invoker_test.go          |  3 +-
 protocol/dubbo/listener.go                    |  2 +-
 protocol/invocation/rpcinvocation.go          | 96 ++++++++++---------
 protocol/jsonrpc/jsonrpc_invoker_test.go      |  4 +-
 protocol/jsonrpc/server.go                    |  2 +-
 10 files changed, 65 insertions(+), 62 deletions(-)

diff --git a/cluster/cluster_impl/failover_cluster_test.go b/cluster/cluster_impl/failover_cluster_test.go
index cc533ea09..dc039db8d 100644
--- a/cluster/cluster_impl/failover_cluster_test.go
+++ b/cluster/cluster_impl/failover_cluster_test.go
@@ -143,8 +143,7 @@ func Test_FailoverInvoke2(t *testing.T) {
 	urlParams.Set(constant.RETRIES_KEY, "2")
 	urlParams.Set("methods.test."+constant.RETRIES_KEY, "3")
 
-	ivc := &invocation.RPCInvocation{}
-	ivc.SetMethod("test")
+	ivc := invocation.NewRPCInvocationWithOptions(invocation.WithMethodName("test"))
 	result := normalInvoke(t, 3, urlParams, ivc)
 	assert.NoError(t, result.Error())
 	count = 0
diff --git a/cluster/loadbalance/least_active_test.go b/cluster/loadbalance/least_active_test.go
index c29a2092a..7663ea3ce 100644
--- a/cluster/loadbalance/least_active_test.go
+++ b/cluster/loadbalance/least_active_test.go
@@ -43,8 +43,7 @@ func TestLeastActiveByWeight(t *testing.T) {
 		invokers = append(invokers, protocol.NewBaseInvoker(url))
 	}
 
-	inv := new(invocation.RPCInvocation)
-	inv.SetMethod("test")
+	inv := invocation.NewRPCInvocationWithOptions(invocation.WithMethodName("test"))
 	protocol.BeginCount(invokers[2].GetUrl(), inv.MethodName())
 
 	loop = 10000
diff --git a/cluster/loadbalance/random_test.go b/cluster/loadbalance/random_test.go
index 09d3d259a..ffe65d78a 100644
--- a/cluster/loadbalance/random_test.go
+++ b/cluster/loadbalance/random_test.go
@@ -67,8 +67,7 @@ func Test_RandomlbSelectWeight(t *testing.T) {
 	urlParams.Set("methods.test."+constant.WEIGHT_KEY, "10000000000000")
 	urll, _ := common.NewURL(context.TODO(), fmt.Sprintf("dubbo://192.168.1.100:20000/com.ikurento.user.UserProvider"), common.WithParams(urlParams))
 	invokers = append(invokers, protocol.NewBaseInvoker(urll))
-	ivc := &invocation.RPCInvocation{}
-	ivc.SetMethod("test")
+	ivc := invocation.NewRPCInvocationWithOptions(invocation.WithMethodName("test"))
 
 	var selectedInvoker []protocol.Invoker
 	var selected float64
@@ -99,8 +98,7 @@ func Test_RandomlbSelectWarmup(t *testing.T) {
 	urlParams.Set(constant.REMOTE_TIMESTAMP_KEY, strconv.FormatInt(time.Now().Add(time.Minute*(-9)).Unix(), 10))
 	urll, _ := common.NewURL(context.TODO(), fmt.Sprintf("dubbo://192.168.1.100:20000/com.ikurento.user.UserProvider"), common.WithParams(urlParams))
 	invokers = append(invokers, protocol.NewBaseInvoker(urll))
-	ivc := &invocation.RPCInvocation{}
-	ivc.SetMethod("test")
+	ivc := invocation.NewRPCInvocationWithOptions(invocation.WithMethodName("test"))
 
 	var selectedInvoker []protocol.Invoker
 	var selected float64
diff --git a/common/proxy/proxy.go b/common/proxy/proxy.go
index 96d42eb21..1c079f6bc 100644
--- a/common/proxy/proxy.go
+++ b/common/proxy/proxy.go
@@ -116,7 +116,9 @@ func (p *Proxy) Implement(v common.RPCService) {
 				}
 			}
 
-			inv = invocation_impl.NewRPCInvocationForConsumer(methodName, nil, inArr, reply.Interface(), p.callBack, common.URL{}, nil)
+			inv = invocation_impl.NewRPCInvocationWithOptions(invocation_impl.WithMethodName(methodName),
+				invocation_impl.WithArguments(inArr), invocation_impl.WithReply(reply.Interface()),
+				invocation_impl.WithCallBack(p.callBack))
 
 			for k, value := range p.attachments {
 				inv.SetAttachments(k, value)
diff --git a/filter/impl/echo_filter_test.go b/filter/impl/echo_filter_test.go
index e2752c85b..e2e592974 100644
--- a/filter/impl/echo_filter_test.go
+++ b/filter/impl/echo_filter_test.go
@@ -34,11 +34,11 @@ import (
 func TestEchoFilter_Invoke(t *testing.T) {
 	filter := GetFilter()
 	result := filter.Invoke(protocol.NewBaseInvoker(common.URL{}),
-		invocation.NewRPCInvocationForProvider("$echo", []interface{}{"OK"}, nil))
+		invocation.NewRPCInvocation("$echo", []interface{}{"OK"}, nil))
 	assert.Equal(t, "OK", result.Result())
 
 	result = filter.Invoke(protocol.NewBaseInvoker(common.URL{}),
-		invocation.NewRPCInvocationForProvider("MethodName", []interface{}{"OK"}, nil))
+		invocation.NewRPCInvocation("MethodName", []interface{}{"OK"}, nil))
 	assert.Nil(t, result.Error())
 	assert.Nil(t, result.Result())
 }
diff --git a/protocol/dubbo/dubbo_invoker_test.go b/protocol/dubbo/dubbo_invoker_test.go
index 182d6d8e0..e88e78273 100644
--- a/protocol/dubbo/dubbo_invoker_test.go
+++ b/protocol/dubbo/dubbo_invoker_test.go
@@ -44,7 +44,8 @@ func TestDubboInvoker_Invoke(t *testing.T) {
 	invoker := NewDubboInvoker(url, c)
 	user := &User{}
 
-	inv := invocation.NewRPCInvocationForConsumer("GetUser", nil, []interface{}{"1", "username"}, user, nil, url, nil)
+	inv := invocation.NewRPCInvocationWithOptions(invocation.WithMethodName("GetUser"), invocation.WithArguments([]interface{}{"1", "username"}),
+		invocation.WithReply(user))
 
 	// Call
 	res := invoker.Invoke(inv)
diff --git a/protocol/dubbo/listener.go b/protocol/dubbo/listener.go
index 120ecf9a4..1333fd3dc 100644
--- a/protocol/dubbo/listener.go
+++ b/protocol/dubbo/listener.go
@@ -223,7 +223,7 @@ func (h *RpcServerHandler) OnMessage(session getty.Session, pkg interface{}) {
 	}
 	invoker := exporter.(protocol.Exporter).GetInvoker()
 	if invoker != nil {
-		result := invoker.Invoke(invocation.NewRPCInvocationForProvider(p.Service.Method, p.Body.(map[string]interface{})["args"].([]interface{}), map[string]string{
+		result := invoker.Invoke(invocation.NewRPCInvocation(p.Service.Method, p.Body.(map[string]interface{})["args"].([]interface{}), map[string]string{
 			constant.PATH_KEY:      p.Service.Path,
 			constant.GROUP_KEY:     p.Service.Group,
 			constant.INTERFACE_KEY: p.Service.Interface,
diff --git a/protocol/invocation/rpcinvocation.go b/protocol/invocation/rpcinvocation.go
index d515cc4c8..2124a22f1 100644
--- a/protocol/invocation/rpcinvocation.go
+++ b/protocol/invocation/rpcinvocation.go
@@ -22,8 +22,6 @@ import (
 )
 
 import (
-	"github.com/apache/dubbo-go/common"
-	"github.com/apache/dubbo-go/common/constant"
 	"github.com/apache/dubbo-go/protocol"
 )
 
@@ -41,27 +39,7 @@ type RPCInvocation struct {
 	invoker        protocol.Invoker
 }
 
-func NewRPCInvocationForConsumer(methodName string, parameterTypes []reflect.Type, arguments []interface{},
-	reply interface{}, callBack interface{}, url common.URL, invoker protocol.Invoker) *RPCInvocation {
-
-	attachments := map[string]string{}
-	attachments[constant.PATH_KEY] = url.Path
-	attachments[constant.GROUP_KEY] = url.GetParam(constant.GROUP_KEY, "")
-	attachments[constant.INTERFACE_KEY] = url.GetParam(constant.INTERFACE_KEY, "")
-	attachments[constant.VERSION_KEY] = url.GetParam(constant.VERSION_KEY, "")
-
-	return &RPCInvocation{
-		methodName:     methodName,
-		parameterTypes: parameterTypes,
-		arguments:      arguments,
-		reply:          reply,
-		callBack:       callBack,
-		attachments:    attachments,
-		invoker:        invoker,
-	}
-}
-
-func NewRPCInvocationForProvider(methodName string, arguments []interface{}, attachments map[string]string) *RPCInvocation {
+func NewRPCInvocation(methodName string, arguments []interface{}, attachments map[string]string) *RPCInvocation {
 	return &RPCInvocation{
 		methodName:  methodName,
 		arguments:   arguments,
@@ -69,26 +47,6 @@ func NewRPCInvocationForProvider(methodName string, arguments []interface{}, att
 	}
 }
 
-type option func(invo *RPCInvocation)
-
-func WithMethodName(methodName string) option {
-	return func(invo *RPCInvocation) {
-		invo.methodName = methodName
-	}
-}
-
-func WithParameterTypes(parameterTypes []reflect.Type) option {
-	return func(invo *RPCInvocation) {
-		invo.parameterTypes = parameterTypes
-	}
-}
-
-func WithArguments(arguments []interface{}) option {
-	return func(invo *RPCInvocation) {
-		invo.arguments = arguments
-	}
-}
-
 func NewRPCInvocationWithOptions(opts ...option) *RPCInvocation {
 	invo := &RPCInvocation{}
 	for _, opt := range opts {
@@ -147,14 +105,58 @@ func (r *RPCInvocation) SetInvoker() protocol.Invoker {
 	return r.invoker
 }
 
+func (r *RPCInvocation) CallBack() interface{} {
+	return r.callBack
+}
+
 func (r *RPCInvocation) SetCallBack(c interface{}) {
 	r.callBack = c
 }
 
-func (r *RPCInvocation) CallBack() interface{} {
-	return r.callBack
+///////////////////////////
+// option
+///////////////////////////
+
+type option func(invo *RPCInvocation)
+
+func WithMethodName(methodName string) option {
+	return func(invo *RPCInvocation) {
+		invo.methodName = methodName
+	}
+}
+
+func WithParameterTypes(parameterTypes []reflect.Type) option {
+	return func(invo *RPCInvocation) {
+		invo.parameterTypes = parameterTypes
+	}
+}
+
+func WithArguments(arguments []interface{}) option {
+	return func(invo *RPCInvocation) {
+		invo.arguments = arguments
+	}
+}
+
+func WithReply(reply interface{}) option {
+	return func(invo *RPCInvocation) {
+		invo.reply = reply
+	}
+}
+
+func WithCallBack(callBack interface{}) option {
+	return func(invo *RPCInvocation) {
+		invo.callBack = callBack
+	}
+}
+
+func WithAttachments(attachments map[string]string) option {
+	return func(invo *RPCInvocation) {
+		invo.attachments = attachments
+	}
 }
 
-func (r *RPCInvocation) SetMethod(method string) {
-	r.methodName = method
+func WithInvoker(invoker protocol.Invoker) option {
+	return func(invo *RPCInvocation) {
+		invo.invoker = invoker
+	}
 }
diff --git a/protocol/jsonrpc/jsonrpc_invoker_test.go b/protocol/jsonrpc/jsonrpc_invoker_test.go
index 1a7f61b64..bc88759bf 100644
--- a/protocol/jsonrpc/jsonrpc_invoker_test.go
+++ b/protocol/jsonrpc/jsonrpc_invoker_test.go
@@ -57,7 +57,9 @@ func TestJsonrpcInvoker_Invoke(t *testing.T) {
 
 	jsonInvoker := NewJsonrpcInvoker(url, client)
 	user := &User{}
-	res := jsonInvoker.Invoke(invocation.NewRPCInvocationForConsumer("GetUser", nil, []interface{}{"1", "username"}, user, nil, url, nil))
+	res := jsonInvoker.Invoke(invocation.NewRPCInvocationWithOptions(invocation.WithMethodName("GetUser"), invocation.WithArguments([]interface{}{"1", "username"}),
+		invocation.WithReply(user)))
+
 	assert.NoError(t, res.Error())
 	assert.Equal(t, User{Id: "1", Name: "username"}, *res.Result().(*User))
 
diff --git a/protocol/jsonrpc/server.go b/protocol/jsonrpc/server.go
index a88896ae9..6b3a39c68 100644
--- a/protocol/jsonrpc/server.go
+++ b/protocol/jsonrpc/server.go
@@ -325,7 +325,7 @@ func serveRequest(ctx context.Context,
 	exporter, _ := jsonrpcProtocol.ExporterMap().Load(path)
 	invoker := exporter.(*JsonrpcExporter).GetInvoker()
 	if invoker != nil {
-		result := invoker.Invoke(invocation.NewRPCInvocationForProvider(methodName, args, map[string]string{
+		result := invoker.Invoke(invocation.NewRPCInvocation(methodName, args, map[string]string{
 			constant.PATH_KEY:    path,
 			constant.VERSION_KEY: codec.req.Version,
 		}))
-- 
GitLab