|
| 1 | +/** |
| 2 | + * Tencent is pleased to support the open source community by making polaris-go available. |
| 3 | + * |
| 4 | + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. |
| 5 | + * |
| 6 | + * Licensed under the BSD 3-Clause License (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * https://opensource.org/licenses/BSD-3-Clause |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software distributed |
| 13 | + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 14 | + * CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 15 | + * specific language governing permissions and limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package pb |
| 19 | + |
| 20 | +import ( |
| 21 | + "github.com/golang/protobuf/proto" |
| 22 | + "github.com/modern-go/reflect2" |
| 23 | + apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage" |
| 24 | + apitraffic "github.com/polarismesh/specification/source/go/api/v1/traffic_manage" |
| 25 | + "google.golang.org/protobuf/types/known/wrapperspb" |
| 26 | + |
| 27 | + "github.com/polarismesh/polaris-go/pkg/log" |
| 28 | + "github.com/polarismesh/polaris-go/pkg/model" |
| 29 | +) |
| 30 | + |
| 31 | +// NearbyRoutingAssistant 就近路由规则解析助手 |
| 32 | +type NearbyRoutingAssistant struct { |
| 33 | +} |
| 34 | + |
| 35 | +// ParseRuleValue 解析出具体的规则值 |
| 36 | +func (n *NearbyRoutingAssistant) ParseRuleValue(resp *apiservice.DiscoverResponse) (proto.Message, string) { |
| 37 | + var revision string |
| 38 | + serviceKey := "" |
| 39 | + if resp.Service != nil { |
| 40 | + serviceKey = resp.Service.GetNamespace().GetValue() + "/" + resp.Service.GetName().GetValue() |
| 41 | + } |
| 42 | + |
| 43 | + if resp.NearbyRouteRules == nil || len(resp.NearbyRouteRules) == 0 { |
| 44 | + // 当没有就近路由规则时,使用 service.revision |
| 45 | + revision = resp.GetService().GetRevision().GetValue() |
| 46 | + log.GetBaseLogger().Debugf("NearbyRoutingAssistant.ParseRuleValue: service=%s, no nearby route rules found, using service revision=%s", |
| 47 | + serviceKey, revision) |
| 48 | + return nil, revision |
| 49 | + } |
| 50 | + |
| 51 | + // 遍历规则列表,优先从开启的规则中获取revision |
| 52 | + var selectedRule *apitraffic.RouteRule |
| 53 | + var minPriorityRule *apitraffic.RouteRule |
| 54 | + |
| 55 | + for _, rule := range resp.NearbyRouteRules { |
| 56 | + // 如果找到开启的规则,直接使用 |
| 57 | + if rule.GetEnable() { |
| 58 | + selectedRule = rule |
| 59 | + break |
| 60 | + } |
| 61 | + |
| 62 | + // 记录 priority 值最小的规则 |
| 63 | + if minPriorityRule == nil || rule.GetPriority() < minPriorityRule.GetPriority() { |
| 64 | + minPriorityRule = rule |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + // 如果没有开启的规则,使用 priority 值最小的规则 |
| 69 | + if selectedRule == nil { |
| 70 | + selectedRule = minPriorityRule |
| 71 | + } |
| 72 | + |
| 73 | + // 使用服务级别的 revision,而不是单个规则的 revision |
| 74 | + // 这样可以避免因 revision 不匹配导致的循环刷新问题 |
| 75 | + revision = resp.GetService().GetRevision().GetValue() |
| 76 | + routing := &apitraffic.Routing{ |
| 77 | + Namespace: resp.Service.Namespace, |
| 78 | + Service: resp.Service.Name, |
| 79 | + Rules: resp.NearbyRouteRules, |
| 80 | + Revision: wrapperspb.String(revision), |
| 81 | + } |
| 82 | + |
| 83 | + log.GetBaseLogger().Debugf("NearbyRoutingAssistant.ParseRuleValue: service=%s, rules=%d, revision=%s, enabled=%v, priority=%d", |
| 84 | + serviceKey, len(resp.NearbyRouteRules), revision, selectedRule.GetEnable(), selectedRule.GetPriority()) |
| 85 | + |
| 86 | + return routing, revision |
| 87 | +} |
| 88 | + |
| 89 | +// Validate 规则校验 |
| 90 | +func (n *NearbyRoutingAssistant) Validate(message proto.Message, ruleCache model.RuleCache) error { |
| 91 | + if reflect2.IsNil(message) { |
| 92 | + return nil |
| 93 | + } |
| 94 | + // 就近路由规则不需要特殊校验,复用RoutingAssistant的校验逻辑 |
| 95 | + routingValue := message.(*apitraffic.Routing) |
| 96 | + assistant := &RoutingAssistant{} |
| 97 | + return assistant.Validate(routingValue, ruleCache) |
| 98 | +} |
| 99 | + |
| 100 | +// SetDefault 设置默认值 |
| 101 | +func (n *NearbyRoutingAssistant) SetDefault(message proto.Message) { |
| 102 | + // do nothing |
| 103 | +} |
0 commit comments