-
Notifications
You must be signed in to change notification settings - Fork 989
Feat: add v3router to dubbogo #1187
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f265786
fix: add v3router
LaurenceLiZhixin 9f7ec6a
fi
LaurenceLiZhixin fbd10c8
fix: fix import block
LaurenceLiZhixin b368f68
fix: code review
LaurenceLiZhixin 940194d
fix
LaurenceLiZhixin c603af6
fix: fix nil ptr bug
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 hidden or 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 v3router | ||
|
|
||
| import ( | ||
| "github.com/apache/dubbo-go/common" | ||
| "github.com/apache/dubbo-go/config" | ||
| "github.com/apache/dubbo-go/protocol" | ||
| ) | ||
|
|
||
| // nolint | ||
| type DubboRouterRule struct { | ||
| uniformRules []*UniformRule | ||
| } | ||
|
|
||
| func newDubboRouterRule(dubboRoutes []*config.DubboRoute, | ||
| destinationMap map[string]map[string]string) (*DubboRouterRule, error) { | ||
|
|
||
| uniformRules := make([]*UniformRule, 0) | ||
| for _, v := range dubboRoutes { | ||
| uniformRule, err := newUniformRule(v, destinationMap) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| uniformRules = append(uniformRules, uniformRule) | ||
| } | ||
|
|
||
| return &DubboRouterRule{ | ||
| uniformRules: uniformRules, | ||
| }, nil | ||
| } | ||
|
|
||
| func (drr *DubboRouterRule) route(invokers []protocol.Invoker, url *common.URL, | ||
| invocation protocol.Invocation) []protocol.Invoker { | ||
|
|
||
| resultInvokers := make([]protocol.Invoker, 0) | ||
| for _, v := range drr.uniformRules { | ||
| if resultInvokers = v.route(invokers, url, invocation); len(resultInvokers) == 0 { | ||
| continue | ||
| } | ||
| // once there is a uniformRule successfully get target invoker lists, return it | ||
| return resultInvokers | ||
| } | ||
| // return s empty invoker list | ||
| return resultInvokers | ||
| } |
This file contains hidden or 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,35 @@ | ||
| /* | ||
| * 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 v3router | ||
|
|
||
| import ( | ||
| "github.com/apache/dubbo-go/cluster/router" | ||
| ) | ||
|
|
||
| // UniformRouteFactory is uniform router's factory | ||
| type UniformRouteFactory struct{} | ||
|
|
||
| // NewUniformRouterFactory constructs a new PriorityRouterFactory | ||
| func NewUniformRouterFactory() router.PriorityRouterFactory { | ||
| return &UniformRouteFactory{} | ||
| } | ||
|
|
||
| // NewPriorityRouter construct a new UniformRouteFactory as PriorityRouter | ||
| func (f *UniformRouteFactory) NewPriorityRouter(vsConfigBytes, distConfigBytes []byte, notify chan struct{}) (router.PriorityRouter, error) { | ||
| return NewUniformRouterChain(vsConfigBytes, distConfigBytes, notify) | ||
| } |
This file contains hidden or 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,35 @@ | ||
| /* | ||
| * 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 v3router | ||
|
|
||
| import ( | ||
| "testing" | ||
| ) | ||
|
|
||
| import ( | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| // TestUniformRouterFacotry created a new factory that can new uniform router | ||
| func TestUniformRouterFacotry(t *testing.T) { | ||
| factory := NewUniformRouterFactory() | ||
| assert.NotNil(t, factory) | ||
| router, err := factory.NewPriorityRouter([]byte{}, []byte{}, make(chan struct{})) | ||
| assert.Nil(t, err) | ||
| assert.NotNil(t, router) | ||
| } |
This file contains hidden or 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,82 @@ | ||
| /* | ||
| * 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 judger | ||
|
|
||
| import ( | ||
| "github.com/apache/dubbo-go/config" | ||
| "github.com/apache/dubbo-go/protocol" | ||
| ) | ||
|
|
||
| type AttachmentMatchJudger struct { | ||
| config.DubboAttachmentMatch | ||
| } | ||
|
|
||
| // nolint | ||
| func (amj *AttachmentMatchJudger) Judge(invocation protocol.Invocation) bool { | ||
| invAttaMap := invocation.Attachments() | ||
| if amj.EagleeyeContext != nil { | ||
| for k, v := range amj.EagleeyeContext { | ||
| invAttaValue, ok := invAttaMap[k] | ||
| if !ok { | ||
| if v.Empty == "" { | ||
| return false | ||
| } | ||
| continue | ||
| } | ||
| // exist this key | ||
| str, ok := invAttaValue.(string) | ||
| if !ok { | ||
| return false | ||
| } | ||
| strJudger := NewStringMatchJudger(v) | ||
| if !strJudger.Judge(str) { | ||
| return false | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if amj.DubboContext != nil { | ||
| for k, v := range amj.DubboContext { | ||
| invAttaValue, ok := invAttaMap[k] | ||
| if !ok { | ||
| if v.Empty == "" { | ||
| return false | ||
| } | ||
| continue | ||
| } | ||
| // exist this key | ||
| str, ok := invAttaValue.(string) | ||
| if !ok { | ||
| return false | ||
| } | ||
| strJudger := NewStringMatchJudger(v) | ||
| if !strJudger.Judge(str) { | ||
| return false | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return true | ||
| } | ||
|
|
||
| // nolint | ||
| func NewAttachmentMatchJudger(matchConf *config.DubboAttachmentMatch) *AttachmentMatchJudger { | ||
| return &AttachmentMatchJudger{ | ||
| DubboAttachmentMatch: *matchConf, | ||
| } | ||
| } | ||
49 changes: 49 additions & 0 deletions
49
cluster/router/v3router/judger/attachment_match_judger_test.go
This file contains hidden or 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,49 @@ | ||
| /* | ||
| * 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 judger | ||
|
|
||
| import ( | ||
| "testing" | ||
| ) | ||
|
|
||
| import ( | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| import ( | ||
| "github.com/apache/dubbo-go/config" | ||
| "github.com/apache/dubbo-go/protocol/invocation" | ||
| ) | ||
|
|
||
| func TestAttachmentMatchJudger(t *testing.T) { | ||
| dubboCtxMap := make(map[string]*config.StringMatch) | ||
| dubboIvkMap := make(map[string]interface{}) | ||
| dubboCtxMap["test-key"] = &config.StringMatch{ | ||
| Exact: "abc", | ||
| } | ||
| dubboIvkMap["test-key"] = "abc" | ||
| assert.True(t, NewAttachmentMatchJudger(&config.DubboAttachmentMatch{ | ||
| DubboContext: dubboCtxMap, | ||
| }).Judge(invocation.NewRPCInvocation("method", nil, dubboIvkMap))) | ||
|
|
||
| dubboIvkMap["test-key"] = "abd" | ||
| assert.False(t, NewAttachmentMatchJudger(&config.DubboAttachmentMatch{ | ||
| DubboContext: dubboCtxMap, | ||
| }).Judge(invocation.NewRPCInvocation("method", nil, dubboIvkMap))) | ||
|
|
||
| } |
This file contains hidden or 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,37 @@ | ||
| /* | ||
| * 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 judger | ||
|
|
||
| import "github.com/apache/dubbo-go/config" | ||
|
|
||
| // nolint | ||
| type BoolMatchJudger struct { | ||
| config.BoolMatch | ||
| } | ||
|
|
||
| // nolint | ||
| func (lsmj *BoolMatchJudger) Judge(input bool) bool { | ||
| return input == lsmj.Exact | ||
| } | ||
|
|
||
| // nolint | ||
| func newBoolMatchJudger(matchConf *config.BoolMatch) *BoolMatchJudger { | ||
| return &BoolMatchJudger{ | ||
| BoolMatch: *matchConf, | ||
| } | ||
| } |
This file contains hidden or 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,48 @@ | ||
| /* | ||
| * 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 judger | ||
|
|
||
| import ( | ||
| "testing" | ||
| ) | ||
|
|
||
| import ( | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| import ( | ||
| "github.com/apache/dubbo-go/config" | ||
| ) | ||
|
|
||
| func TestBoolMatchJudger(t *testing.T) { | ||
| assert.True(t, newBoolMatchJudger(&config.BoolMatch{ | ||
| Exact: true, | ||
| }).Judge(true)) | ||
|
|
||
| assert.True(t, newBoolMatchJudger(&config.BoolMatch{ | ||
| Exact: false, | ||
| }).Judge(false)) | ||
|
|
||
| assert.False(t, newBoolMatchJudger(&config.BoolMatch{ | ||
| Exact: true, | ||
| }).Judge(false)) | ||
|
|
||
| assert.False(t, newBoolMatchJudger(&config.BoolMatch{ | ||
| Exact: false, | ||
| }).Judge(true)) | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.