You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by GitBox <gi...@apache.org> on 2021/03/11 11:47:43 UTC

[GitHub] [dubbo-go] watermelo commented on a change in pull request #1081: feat: add pass through proxy factory

watermelo commented on a change in pull request #1081:
URL: https://github.com/apache/dubbo-go/pull/1081#discussion_r592283423



##########
File path: common/constant/key.go
##########
@@ -50,7 +50,8 @@ const (
 	PROTOCOL_KEY             = "protocol"
 	PATH_SEPARATOR           = "/"
 	//DUBBO_KEY                = "dubbo"
-	SSL_ENABLED_KEY = "ssl-enabled"
+	SSL_ENABLED_KEY  = "ssl-enabled"
+	ParameterTypeKey = "parameter-type-names" // ParameterType key used in dapr, to tranfer rsp param type

Review comment:
       Maybe follow the code`SSL_ENABLED_KEY `.

##########
File path: common/proxy/proxy_factory/pass_through.go
##########
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package proxy_factory
+
+import (
+	"context"
+	"reflect"
+)
+
+import (
+	perrors "github.com/pkg/errors"
+)
+
+import (
+	"github.com/apache/dubbo-go/common"
+	"github.com/apache/dubbo-go/common/constant"
+	"github.com/apache/dubbo-go/common/extension"
+	"github.com/apache/dubbo-go/common/proxy"
+	"github.com/apache/dubbo-go/protocol"
+)
+
+func init() {
+	extension.SetProxyFactory(constant.PassThroughProxyFactoryKey, NewPassThroughProxyFactory)
+}
+
+// PassThroughProxyFactory is the factory of PassThroughProxyInvoker
+type PassThroughProxyFactory struct {
+}
+
+// NewPassThroughProxyFactory returns a proxy factory instance
+func NewPassThroughProxyFactory(_ ...proxy.Option) proxy.ProxyFactory {
+	return &PassThroughProxyFactory{}
+}
+
+// GetProxy gets a proxy
+func (factory *PassThroughProxyFactory) GetProxy(invoker protocol.Invoker, url *common.URL) *proxy.Proxy {
+	return factory.GetAsyncProxy(invoker, nil, url)
+}
+
+// GetAsyncProxy gets a async proxy
+func (factory *PassThroughProxyFactory) GetAsyncProxy(invoker protocol.Invoker, callBack interface{}, url *common.URL) *proxy.Proxy {
+	//create proxy
+	attachments := map[string]string{}
+	attachments[constant.ASYNC_KEY] = url.GetParam(constant.ASYNC_KEY, "false")
+	return proxy.NewProxy(invoker, callBack, attachments)
+}
+
+// GetInvoker gets a invoker
+func (factory *PassThroughProxyFactory) GetInvoker(url *common.URL) protocol.Invoker {
+	return &PassThroughProxyInvoker{
+		ProxyInvoker: &ProxyInvoker{
+			BaseInvoker: *protocol.NewBaseInvoker(url),
+		},
+	}
+}
+
+// PassThroughProxyInvoker is a invoker struct, it calls service with specific method 'Serivce' and params:
+// Service(method string, argsTypes []string, args [][]byte, attachment map[string]interface{})
+// PassThroughProxyInvoker pass through raw invocation data and method name to service, which will deal with them.
+type PassThroughProxyInvoker struct {
+	*ProxyInvoker
+}
+
+// Invoke is used to call service method by invocation
+func (pi *PassThroughProxyInvoker) Invoke(ctx context.Context, invocation protocol.Invocation) protocol.Result {
+	result := &protocol.RPCResult{}
+	result.SetAttachments(invocation.Attachments())
+	url := getProviderURL(pi.GetUrl())
+
+	arguments := invocation.Arguments()
+	srv := common.ServiceMap.GetServiceByServiceKey(url.Protocol, url.ServiceKey())
+
+	var args [][]byte
+	if len(arguments) == 0 {
+		args = [][]byte{}
+	} else {
+		args = make([][]byte, 0, len(arguments))
+		for _, arg := range arguments {
+			if v, ok := arg.([]byte); ok {
+				args = append(args, v)
+			} else {
+				result.Err = perrors.New("the param type is not []byte")
+				return result
+			}
+		}
+	}
+	method := srv.Method()["Service"]
+
+	in := []reflect.Value{srv.Rcvr()}
+	in = append(in, reflect.ValueOf(invocation.MethodName()))
+	in = append(in, reflect.ValueOf(invocation.Attachment(constant.ParameterTypeKey)))
+	in = append(in, reflect.ValueOf(args))
+	in = append(in, reflect.ValueOf(invocation.Attachments()))
+
+	returnValues := method.Method().Func.Call(in)
+
+	var retErr interface{}
+	replyv := returnValues[0]
+	retErr = returnValues[1].Interface()
+
+	if retErr != nil {

Review comment:
       Maybe more appropriate.
   ```go
   	if retErr != nil {
   		result.SetError(retErr.(error))
                   return result
   	}
   	if replyv.IsValid() && (replyv.Kind() != reflect.Ptr || replyv.Kind() == reflect.Ptr && replyv.Elem().IsValid()) {
   		result.SetResult(replyv.Interface())
   	}
   	return result
   ```

##########
File path: common/proxy/proxy_factory/pass_through.go
##########
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package proxy_factory
+
+import (
+	"context"
+	"reflect"
+)
+
+import (
+	perrors "github.com/pkg/errors"
+)
+
+import (
+	"github.com/apache/dubbo-go/common"
+	"github.com/apache/dubbo-go/common/constant"
+	"github.com/apache/dubbo-go/common/extension"
+	"github.com/apache/dubbo-go/common/proxy"
+	"github.com/apache/dubbo-go/protocol"
+)
+
+func init() {
+	extension.SetProxyFactory(constant.PassThroughProxyFactoryKey, NewPassThroughProxyFactory)
+}
+
+// PassThroughProxyFactory is the factory of PassThroughProxyInvoker
+type PassThroughProxyFactory struct {
+}
+
+// NewPassThroughProxyFactory returns a proxy factory instance
+func NewPassThroughProxyFactory(_ ...proxy.Option) proxy.ProxyFactory {
+	return &PassThroughProxyFactory{}
+}
+
+// GetProxy gets a proxy
+func (factory *PassThroughProxyFactory) GetProxy(invoker protocol.Invoker, url *common.URL) *proxy.Proxy {
+	return factory.GetAsyncProxy(invoker, nil, url)
+}
+
+// GetAsyncProxy gets a async proxy
+func (factory *PassThroughProxyFactory) GetAsyncProxy(invoker protocol.Invoker, callBack interface{}, url *common.URL) *proxy.Proxy {
+	//create proxy
+	attachments := map[string]string{}
+	attachments[constant.ASYNC_KEY] = url.GetParam(constant.ASYNC_KEY, "false")
+	return proxy.NewProxy(invoker, callBack, attachments)
+}
+
+// GetInvoker gets a invoker
+func (factory *PassThroughProxyFactory) GetInvoker(url *common.URL) protocol.Invoker {
+	return &PassThroughProxyInvoker{
+		ProxyInvoker: &ProxyInvoker{
+			BaseInvoker: *protocol.NewBaseInvoker(url),
+		},
+	}
+}
+
+// PassThroughProxyInvoker is a invoker struct, it calls service with specific method 'Serivce' and params:
+// Service(method string, argsTypes []string, args [][]byte, attachment map[string]interface{})
+// PassThroughProxyInvoker pass through raw invocation data and method name to service, which will deal with them.
+type PassThroughProxyInvoker struct {
+	*ProxyInvoker
+}
+
+// Invoke is used to call service method by invocation
+func (pi *PassThroughProxyInvoker) Invoke(ctx context.Context, invocation protocol.Invocation) protocol.Result {
+	result := &protocol.RPCResult{}
+	result.SetAttachments(invocation.Attachments())
+	url := getProviderURL(pi.GetUrl())
+
+	arguments := invocation.Arguments()
+	srv := common.ServiceMap.GetServiceByServiceKey(url.Protocol, url.ServiceKey())
+
+	var args [][]byte
+	if len(arguments) == 0 {
+		args = [][]byte{}
+	} else {

Review comment:
       We can reduce a `if-else` branch by
   ```go
   if len(arguments) > 0 {
   	args = make([][]byte, 0, len(arguments))
   	for _, arg := range arguments {
   		if v, ok := arg.([]byte); ok {
   			args = append(args, v)
   		} else {
   			result.Err = perrors.New("the param type is not []byte")
   			return result
   		}
   	}
   }
   ```

##########
File path: common/proxy/proxy_factory/pass_through.go
##########
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package proxy_factory
+
+import (
+	"context"
+	"reflect"
+)
+
+import (
+	perrors "github.com/pkg/errors"
+)
+
+import (
+	"github.com/apache/dubbo-go/common"
+	"github.com/apache/dubbo-go/common/constant"
+	"github.com/apache/dubbo-go/common/extension"
+	"github.com/apache/dubbo-go/common/proxy"
+	"github.com/apache/dubbo-go/protocol"
+)
+
+func init() {
+	extension.SetProxyFactory(constant.PassThroughProxyFactoryKey, NewPassThroughProxyFactory)
+}
+
+// PassThroughProxyFactory is the factory of PassThroughProxyInvoker
+type PassThroughProxyFactory struct {
+}
+
+// NewPassThroughProxyFactory returns a proxy factory instance
+func NewPassThroughProxyFactory(_ ...proxy.Option) proxy.ProxyFactory {
+	return &PassThroughProxyFactory{}
+}
+
+// GetProxy gets a proxy
+func (factory *PassThroughProxyFactory) GetProxy(invoker protocol.Invoker, url *common.URL) *proxy.Proxy {
+	return factory.GetAsyncProxy(invoker, nil, url)
+}
+
+// GetAsyncProxy gets a async proxy
+func (factory *PassThroughProxyFactory) GetAsyncProxy(invoker protocol.Invoker, callBack interface{}, url *common.URL) *proxy.Proxy {
+	//create proxy
+	attachments := map[string]string{}
+	attachments[constant.ASYNC_KEY] = url.GetParam(constant.ASYNC_KEY, "false")
+	return proxy.NewProxy(invoker, callBack, attachments)
+}
+
+// GetInvoker gets a invoker
+func (factory *PassThroughProxyFactory) GetInvoker(url *common.URL) protocol.Invoker {
+	return &PassThroughProxyInvoker{
+		ProxyInvoker: &ProxyInvoker{
+			BaseInvoker: *protocol.NewBaseInvoker(url),
+		},
+	}
+}
+
+// PassThroughProxyInvoker is a invoker struct, it calls service with specific method 'Serivce' and params:
+// Service(method string, argsTypes []string, args [][]byte, attachment map[string]interface{})
+// PassThroughProxyInvoker pass through raw invocation data and method name to service, which will deal with them.
+type PassThroughProxyInvoker struct {
+	*ProxyInvoker
+}
+
+// Invoke is used to call service method by invocation
+func (pi *PassThroughProxyInvoker) Invoke(ctx context.Context, invocation protocol.Invocation) protocol.Result {
+	result := &protocol.RPCResult{}
+	result.SetAttachments(invocation.Attachments())
+	url := getProviderURL(pi.GetUrl())
+
+	arguments := invocation.Arguments()
+	srv := common.ServiceMap.GetServiceByServiceKey(url.Protocol, url.ServiceKey())
+
+	var args [][]byte
+	if len(arguments) == 0 {
+		args = [][]byte{}
+	} else {
+		args = make([][]byte, 0, len(arguments))
+		for _, arg := range arguments {
+			if v, ok := arg.([]byte); ok {
+				args = append(args, v)
+			} else {
+				result.Err = perrors.New("the param type is not []byte")
+				return result
+			}
+		}
+	}
+	method := srv.Method()["Service"]
+
+	in := []reflect.Value{srv.Rcvr()}

Review comment:
       Init with length `in := make([]reflect.Value, 5)`.

##########
File path: registry/directory/directory.go
##########
@@ -187,9 +187,12 @@ func (dir *RegistryDirectory) refreshAllInvokers(events []*registry.ServiceEvent
 		// loop the updateEvents
 		for _, event := range addEvents {
 			logger.Debugf("registry update, result{%s}", event)
-			logger.Infof("selector add service url{%s}", event.Service)
-			// FIXME: routers are built in every address notification?
-			dir.configRouters()
+			if event.Service != nil {

Review comment:
       `if event != nil && event.Service != nil`




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org