Skip to content
Snippets Groups Projects
Commit b016f017 authored by 邹毅贤's avatar 邹毅贤
Browse files

refactor router code

parent 18cac918
No related branches found
No related tags found
No related merge requests found
......@@ -42,6 +42,10 @@ const (
PRIORITY = "priority"
)
var (
router_pattern = regexp.MustCompile(`([&!=,]*)\s*([^&!=,\s]+)`)
)
//ConditionRouter condition router struct
type ConditionRouter struct {
Pattern string
......@@ -166,15 +170,10 @@ func parseRule(rule string) (map[string]MatchPair, error) {
}
var (
pair MatchPair
startIndex int
pair MatchPair
)
values := gxset.NewSet()
reg := regexp.MustCompile(`([&!=,]*)\s*([^&!=,\s]+)`)
if indexTuple := reg.FindIndex([]byte(rule)); len(indexTuple) > 0 {
startIndex = indexTuple[0]
}
matches := reg.FindAllSubmatch([]byte(rule), -1)
matches := router_pattern.FindAllSubmatch([]byte(rule), -1)
for _, groups := range matches {
separator := string(groups[1])
content := string(groups[2])
......@@ -197,22 +196,26 @@ func parseRule(rule string) (map[string]MatchPair, error) {
}
case "=":
if &pair == nil {
var startIndex = getStartIndex(rule)
return nil, perrors.Errorf("Illegal route rule \"%s\", The error char '%s' at index %d before \"%d\".", rule, separator, startIndex, startIndex)
}
values = pair.Matches
values.Add(content)
case "!=":
if &pair == nil {
var startIndex = getStartIndex(rule)
return nil, perrors.Errorf("Illegal route rule \"%s\", The error char '%s' at index %d before \"%d\".", rule, separator, startIndex, startIndex)
}
values = pair.Mismatches
values.Add(content)
case ",":
if values.Empty() {
var startIndex = getStartIndex(rule)
return nil, perrors.Errorf("Illegal route rule \"%s\", The error char '%s' at index %d before \"%d\".", rule, separator, startIndex, startIndex)
}
values.Add(content)
default:
var startIndex = getStartIndex(rule)
return nil, perrors.Errorf("Illegal route rule \"%s\", The error char '%s' at index %d before \"%d\".", rule, separator, startIndex, startIndex)
}
......@@ -220,6 +223,13 @@ func parseRule(rule string) (map[string]MatchPair, error) {
return condition, nil
}
func getStartIndex(rule string) int {
if indexTuple := router_pattern.FindIndex([]byte(rule)); len(indexTuple) > 0 {
return indexTuple[0]
}
return -1
}
//
func (c *ConditionRouter) MatchWhen(url common.URL, invocation protocol.Invocation) (bool, error) {
condition, err := matchCondition(c.WhenCondition, &url, nil, invocation)
......
/*
* 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 router
import (
"testing"
)
import (
"github.com/stretchr/testify/assert"
)
func TestNewConditionRouterFactory(t *testing.T) {
factory := NewConditionRouterFactory()
assert.NotNil(t, factory)
}
......@@ -18,6 +18,8 @@ package config
import (
"context"
"io/ioutil"
"path"
"reflect"
"strconv"
"strings"
......@@ -25,6 +27,7 @@ import (
import (
perrors "github.com/pkg/errors"
"gopkg.in/yaml.v2"
)
import (
......@@ -350,3 +353,23 @@ func initializeStruct(t reflect.Type, v reflect.Value) {
}
}
func loadYmlConfig(confRouterFile string, i interface{}) error {
if len(confRouterFile) == 0 {
return perrors.Errorf("application configure(provider) file name is nil")
}
if path.Ext(confRouterFile) != ".yml" {
return perrors.Errorf("application configure file name{%v} suffix must be .yml", confRouterFile)
}
confFileStream, err := ioutil.ReadFile(confRouterFile)
if err != nil {
return perrors.Errorf("ioutil.ReadFile(file:%s) = error:%v", confRouterFile, perrors.WithStack(err))
}
err = yaml.Unmarshal(confFileStream, i)
if err != nil {
return perrors.Errorf("yaml.Unmarshal() = error:%v", perrors.WithStack(err))
}
return nil
}
......@@ -18,18 +18,12 @@ package config
import (
"encoding/base64"
"io/ioutil"
"net/url"
"os"
"path"
"strconv"
"strings"
"sync"
)
import (
perrors "github.com/pkg/errors"
"gopkg.in/yaml.v2"
)
import (
"github.com/apache/dubbo-go/cluster/directory"
"github.com/apache/dubbo-go/common"
......@@ -42,7 +36,7 @@ var (
)
/////////////////////////
// routerConfig
// conditionRouterConfig
/////////////////////////
type ConditionRouterConfig struct {
RawRule string `yaml:"rawRule"`
......@@ -62,26 +56,14 @@ func (*ConditionRouterConfig) Prefix() string {
}
func RouterInit(confRouterFile string) error {
if len(confRouterFile) == 0 {
return perrors.Errorf("application configure(provider) file name is nil")
}
if path.Ext(confRouterFile) != ".yml" {
return perrors.Errorf("application configure file name{%v} suffix must be .yml", confRouterFile)
}
confFileStream, err := ioutil.ReadFile(confRouterFile)
if err != nil {
return perrors.Errorf("ioutil.ReadFile(file:%s) = error:%v", confRouterFile, perrors.WithStack(err))
}
routerConfig = &ConditionRouterConfig{}
e := loadYmlConfig(confRouterFile, routerConfig)
err = yaml.Unmarshal(confFileStream, routerConfig)
if err != nil {
return perrors.Errorf("yaml.Unmarshal() = error:%v", perrors.WithStack(err))
if e != nil {
return e
}
logger.Debugf("provider config{%#v}\n", routerConfig)
logger.Debugf("router config{%#v}\n", routerConfig)
directory.RouterUrlSet.Add(initRouterUrl())
logger.Debug("=====", directory.RouterUrlSet.Size())
return nil
......
......@@ -19,6 +19,8 @@ package config
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestString(t *testing.T) {
......@@ -36,3 +38,36 @@ func TestString(t *testing.T) {
println(n2[0], " ", n2[1])
}
func TestLoadYmlConfig(t *testing.T) {
routerConfig = &ConditionRouterConfig{}
e := loadYmlConfig("testdata/router_config.yml", routerConfig)
assert.Nil(t, e)
assert.NotNil(t, routerConfig)
assert.Equal(t, routerConfig.RawRule, "kkk")
assert.Equal(t, routerConfig.Scope, "dubbo")
assert.Equal(t, routerConfig.Priority, 1)
assert.Equal(t, routerConfig.Enabled, false)
assert.Equal(t, routerConfig.Dynamic, true)
assert.Equal(t, routerConfig.Valid, false)
assert.Equal(t, routerConfig.Force, true)
assert.Equal(t, routerConfig.Runtime, false)
assert.Equal(t, routerConfig.Key, "abc")
assert.Equal(t, len(routerConfig.Conditions), 2)
}
func TestRouterInit(t *testing.T) {
e := RouterInit("testdata/router_config.yml")
assert.Nil(t, e)
assert.NotNil(t, routerConfig)
assert.Equal(t, routerConfig.RawRule, "kkk")
assert.Equal(t, routerConfig.Scope, "dubbo")
assert.Equal(t, routerConfig.Priority, 1)
assert.Equal(t, routerConfig.Enabled, false)
assert.Equal(t, routerConfig.Dynamic, true)
assert.Equal(t, routerConfig.Valid, false)
assert.Equal(t, routerConfig.Force, true)
assert.Equal(t, routerConfig.Runtime, false)
assert.Equal(t, routerConfig.Key, "abc")
assert.Equal(t, len(routerConfig.Conditions), 2)
}
# dubbo router yaml configure file
rawRule: "kkk"
scope: "dubbo"
priority: 1
enabled: false
dynamic: true
valid: false
force: true
runtime: false
key: "abc"
conditions : ["a => b","c => d"]
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment