-
Notifications
You must be signed in to change notification settings - Fork 930
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: add pass through proxy factory #1081
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
2e08039
feat: add pass through proxy factory
LaurenceLiZhixin e41d6fb
Merge branch '3.0' into Ftr/proxy-service
LaurenceLiZhixin feff21f
fix: add test
LaurenceLiZhixin b30e46b
fix: fmt import block
LaurenceLiZhixin 6f22087
fix
LaurenceLiZhixin 3bf0f46
Fix: try ci
LaurenceLiZhixin 1f7d4e4
fix: fix cr
LaurenceLiZhixin fb2fb83
fix cr
LaurenceLiZhixin da6f6f6
fix: restart ci
LaurenceLiZhixin e3b2cf2
Merge branch '3.0' into Ftr/proxy-service
LaurenceLiZhixin 71a50f9
fix: fix conflict
LaurenceLiZhixin 6879502
fix: conflict
LaurenceLiZhixin 82d26db
fix: change passthrough factory param
LaurenceLiZhixin 77f9cea
fix: change passthrough factory param
LaurenceLiZhixin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
* 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 = 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 := make([]reflect.Value, 5) | ||
in = append(in, srv.Rcvr()) | ||
in = append(in, reflect.ValueOf(invocation.MethodName())) | ||
in = append(in, reflect.ValueOf(invocation.Attachment(constant.PARAMS_TYPE_Key))) | ||
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe more appropriate. 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed! |
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* 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 ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
import ( | ||
"github.com/apache/dubbo-go/common" | ||
"github.com/apache/dubbo-go/protocol" | ||
) | ||
|
||
func TestPassThroughProxyFactoryGetProxy(t *testing.T) { | ||
proxyFactory := NewPassThroughProxyFactory() | ||
url := common.NewURLWithOptions() | ||
proxy := proxyFactory.GetProxy(protocol.NewBaseInvoker(url), url) | ||
assert.NotNil(t, proxy) | ||
} | ||
|
||
type TestPassThroughProxyFactoryAsync struct { | ||
} | ||
|
||
func (u *TestPassThroughProxyFactoryAsync) CallBack(res common.CallbackResponse) { | ||
fmt.Println("CallBack res:", res) | ||
} | ||
|
||
func TestPassThroughProxyFactoryGetAsyncProxy(t *testing.T) { | ||
proxyFactory := NewPassThroughProxyFactory() | ||
url := common.NewURLWithOptions() | ||
async := &TestPassThroughProxyFactoryAsync{} | ||
proxy := proxyFactory.GetAsyncProxy(protocol.NewBaseInvoker(url), async.CallBack, url) | ||
assert.NotNil(t, proxy) | ||
} | ||
|
||
func TestPassThroughProxyFactoryGetInvoker(t *testing.T) { | ||
proxyFactory := NewPassThroughProxyFactory() | ||
url := common.NewURLWithOptions() | ||
invoker := proxyFactory.GetInvoker(url) | ||
assert.True(t, invoker.IsAvailable()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the same comment as line 93: pls using gxbytes.AcquireBytes instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gxbytes.AcquireBytes() is used to get specific make([]byte, 0, size), not to make [][]byte.