Skip to content

Commit

Permalink
Feat/router label (#115)
Browse files Browse the repository at this point in the history
* feat: 添加就近路由支持文档

* rebase upstream/master

* feat:support new route label

* feat:support new route label

* feat:support new route label

* feat:support new route label

* feat:support new route label
  • Loading branch information
chuntaojun authored Nov 24, 2022
1 parent 96891e4 commit 783ba15
Show file tree
Hide file tree
Showing 12 changed files with 274 additions and 12 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ test/other/other_test_suit.go

/vendor/

.vscode/
.vscode/

style_tool/
goimports-reviser
15 changes: 15 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,21 @@ type ProcessRoutersRequest struct {
model.ProcessRoutersRequest
}

func (r *ProcessRoutersRequest) convert() {
if len(r.Arguments) == 0 {
return
}

if len(r.SourceService.Metadata) == 0 {
r.SourceService.Metadata = map[string]string{}
}

for i := range r.Arguments {
arg := r.Arguments[i]
arg.ToLabels(r.SourceService.Metadata)
}
}

// ProcessLoadBalanceRequest process load balancer to get the target instances
type ProcessLoadBalanceRequest struct {
model.ProcessLoadBalanceRequest
Expand Down
38 changes: 38 additions & 0 deletions api/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,49 @@ type GetOneInstanceRequest struct {
model.GetOneInstanceRequest
}

func (r *GetOneInstanceRequest) convert() {
if len(r.Arguments) == 0 {
return
}

serviceInfo := r.SourceService
if serviceInfo == nil {
r.SourceService = &model.ServiceInfo{
Metadata: map[string]string{},
}
serviceInfo = r.SourceService
}

for i := range r.Arguments {
arg := r.Arguments[i]
arg.ToLabels(serviceInfo.Metadata)
}
}

// GetInstancesRequest 获取多个服务的请求对象
type GetInstancesRequest struct {
model.GetInstancesRequest
}

func (r *GetInstancesRequest) convert() {
if len(r.Arguments) == 0 {
return
}

serviceInfo := r.SourceService
if serviceInfo == nil {
r.SourceService = &model.ServiceInfo{
Metadata: map[string]string{},
}
serviceInfo = r.SourceService
}

for i := range r.Arguments {
arg := r.Arguments[i]
arg.ToLabels(serviceInfo.Metadata)
}
}

// GetAllInstancesRequest 获取服务下所有实例的请求对象
type GetAllInstancesRequest struct {
model.GetAllInstancesRequest
Expand Down
2 changes: 2 additions & 0 deletions api/consumer_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func (c *consumerAPI) GetOneInstance(req *GetOneInstanceRequest) (*model.OneInst
if err := req.Validate(); err != nil {
return nil, err
}
req.convert()
return c.context.GetEngine().SyncGetOneInstance(&req.GetOneInstanceRequest)
}

Expand All @@ -54,6 +55,7 @@ func (c *consumerAPI) GetInstances(req *GetInstancesRequest) (*model.InstancesRe
if err := req.Validate(); err != nil {
return nil, err
}
req.convert()
return c.context.GetEngine().SyncGetInstances(&req.GetInstancesRequest)
}

Expand Down
77 changes: 77 additions & 0 deletions api/consumer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* Tencent is pleased to support the open source community by making polaris-go available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* 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 api

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/polarismesh/polaris-go/pkg/model"
)

func TestGetInstancesRequest_convert(t *testing.T) {
type fields struct {
GetInstancesRequest model.GetInstancesRequest
}
tests := []struct {
name string
fields fields
ret map[string]string
}{
{
fields: fields{
GetInstancesRequest: model.GetInstancesRequest{
SourceService: &model.ServiceInfo{
Metadata: map[string]string{
"uid": "123",
},
},
Arguments: []model.Argument{
model.BuildHeaderArgument("uid", "123"),
},
},
},
ret: map[string]string{
"uid": "123",
"$header.uid": "123",
},
},
{
fields: fields{
GetInstancesRequest: model.GetInstancesRequest{
Arguments: []model.Argument{
model.BuildHeaderArgument("uid", "123"),
},
},
},
ret: map[string]string{
"$header.uid": "123",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := &GetInstancesRequest{
GetInstancesRequest: tt.fields.GetInstancesRequest,
}
r.convert()
assert.Equal(t, tt.ret, r.SourceService.Metadata)
})
}
}
1 change: 1 addition & 0 deletions api_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func (r *routerAPI) ProcessRouters(request *ProcessRoutersRequest) (*model.Insta
if err := request.Validate(); err != nil {
return nil, err
}
request.convert()
return r.sdkCtx.GetEngine().ProcessRouters(&request.ProcessRoutersRequest)
}

Expand Down
13 changes: 7 additions & 6 deletions examples/route/dynamic/consumer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (

"github.com/polarismesh/polaris-go"
"github.com/polarismesh/polaris-go/pkg/config"
"github.com/polarismesh/polaris-go/pkg/model"
)

var (
Expand Down Expand Up @@ -104,7 +105,7 @@ func (svr *PolarisConsumer) runWebServer() {
routerRequest.DstInstances = instancesResp
routerRequest.SourceService.Service = selfService
routerRequest.SourceService.Namespace = selfNamespace
routerRequest.SourceService.Metadata = convertQuery(r.URL.RawQuery)
routerRequest.AddArguments(convertQuery(r.URL.RawQuery)...)
routerInstancesResp, err := svr.router.ProcessRouters(routerRequest)
if nil != err {
log.Printf("[error] fail to processRouters, err is %v", err)
Expand Down Expand Up @@ -190,19 +191,19 @@ func main() {

}

func convertQuery(rawQuery string) map[string]string {
meta := make(map[string]string)
func convertQuery(rawQuery string) []model.Argument {
arguments := make([]model.Argument, 0, 4)
if len(rawQuery) == 0 {
return meta
return arguments
}
tokens := strings.Split(rawQuery, "&")
if len(tokens) > 0 {
for _, token := range tokens {
values := strings.Split(token, "=")
meta[values[0]] = values[1]
arguments = append(arguments, model.BuildQueryArgument(values[0], values[1]))
}
}
return meta
return arguments
}

func getLocalHost(serverAddr string) (string, error) {
Expand Down
41 changes: 41 additions & 0 deletions import-format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/bash
# Tencent is pleased to support the open source community by making Polaris available.
#
# Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
#
# Licensed under the BSD 3-Clause License (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://opensource.org/licenses/BSD-3-Clause
#
# 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.


# 格式化 go.mod
go mod tidy -compat=1.17


# 处理 go imports 的格式化
rm -rf style_tool
rm -rf goimports-reviser

mkdir -p style_tool

cd style_tool

wget https://github.com/incu6us/goimports-reviser/releases/download/v3.1.1/goimports-reviser_3.1.1_linux_amd64.tar.gz
tar -zxvf goimports-reviser_3.1.1_linux_amd64.tar.gz
mv goimports-reviser ../

cd ../

ls -lstrh

find . -name "*.go" -type f | grep -v .pb.go|grep -v test/tools/tools.go | grep -v ./pkg/plugin/register/plugins.go | xargs -I {} goimports-reviser -rm-unused -format {} -project-name github.com/polarismesh/polaris-go

# 处理 go 代码格式化
go fmt ./...
33 changes: 32 additions & 1 deletion pkg/model/argument.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const (
ArgumentTypeQuery
ArgumentTypeCallerService
ArgumentTypeCallerIP
ArgumentTypePath
ArgumentTypeCookie
)

var argumentTypeToName = map[int]string{
Expand All @@ -38,6 +40,8 @@ var argumentTypeToName = map[int]string{
ArgumentTypeQuery: "QUERY",
ArgumentTypeCallerService: "CALLER_SERVICE",
ArgumentTypeCallerIP: "CALLER_IP",
ArgumentTypePath: "PATH",
ArgumentTypeCookie: "COOKIE",
}

const (
Expand All @@ -46,9 +50,11 @@ const (
LabelKeyQuery = "$query."
LabelKeyCallerService = "$caller_service."
LabelKeyCallerIp = "$caller_ip"
LabelKeyPath = "$path"
LabelKeyCookie = "$cookie."
)

// Argument 限流参数
// Argument 限流/路由参数
type Argument struct {
argumentType int

Expand Down Expand Up @@ -119,13 +125,31 @@ func BuildCallerIPArgument(callerIP string) Argument {
}
}

func BuildPathArgument(path string) Argument {
return Argument{
argumentType: ArgumentTypePath,
value: path,
}
}

func BuildCookieArgument(key, value string) Argument {
return Argument{
argumentType: ArgumentTypeCookie,
key: key,
value: value,
}
}

func BuildArgumentFromLabel(labelKey string, labelValue string) Argument {
if labelKey == LabelKeyMethod {
return BuildMethodArgument(labelValue)
}
if labelKey == LabelKeyCallerIp {
return BuildCallerIPArgument(labelValue)
}
if labelKey == LabelKeyPath {
return BuildPathArgument(labelValue)
}
if strings.HasPrefix(labelKey, LabelKeyHeader) {
return BuildHeaderArgument(labelKey[len(LabelKeyHeader):], labelValue)
}
Expand All @@ -135,6 +159,9 @@ func BuildArgumentFromLabel(labelKey string, labelValue string) Argument {
if strings.HasPrefix(labelKey, LabelKeyCallerService) {
return BuildCallerServiceArgument(labelKey[len(LabelKeyCallerService):], labelValue)
}
if strings.HasPrefix(labelKey, LabelKeyCookie) {
return BuildCookieArgument(labelKey[len(LabelKeyCookie):], labelValue)
}
return BuildCustomArgument(labelKey, labelValue)
}

Expand All @@ -152,5 +179,9 @@ func (a Argument) ToLabels(labels map[string]string) {
labels[LabelKeyCallerService+a.key] = a.value
case ArgumentTypeCustom:
labels[a.key] = a.value
case ArgumentTypePath:
labels[LabelKeyPath] = a.value
case ArgumentTypeCookie:
labels[LabelKeyCookie+a.key] = a.value
}
}
10 changes: 10 additions & 0 deletions pkg/model/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type ProcessRoutersRequest struct {
Routers []string
// SourceService indicate the source service to match the route rule, optional.
SourceService ServiceInfo
// Arguments traffic labels
Arguments []Argument
// DstInstances indicate the destination instances resolved from discovery, required.
// Two implementations to ServiceInstances:
// 1. InstancesResponse, returned from ConsumerAPI.GetAllInstances.
Expand All @@ -47,6 +49,14 @@ func (p *ProcessRoutersRequest) GetResponse() *InstancesResponse {
return &p.response
}

// AddArgument add one traffic label
func (p *ProcessRoutersRequest) AddArguments(arg ...Argument) {
if len(p.Arguments) == 0 {
p.Arguments = make([]Argument, 0, 4)
}
p.Arguments = append(p.Arguments, arg...)
}

// Validate validate the request object
func (p *ProcessRoutersRequest) Validate() error {
if nil == p {
Expand Down
Loading

0 comments on commit 783ba15

Please sign in to comment.