forked from tencentyun/tsf-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
route.go
182 lines (168 loc) · 4.33 KB
/
route.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package router
import (
"context"
"fmt"
"math/rand"
"sync"
"sync/atomic"
"github.com/go-kratos/kratos/v2/errors"
"github.com/hisonsoft/tsf-go/log"
"github.com/hisonsoft/tsf-go/pkg/config"
"github.com/hisonsoft/tsf-go/pkg/config/consul"
"github.com/hisonsoft/tsf-go/pkg/naming"
"github.com/hisonsoft/tsf-go/pkg/route"
"github.com/hisonsoft/tsf-go/pkg/sys/env"
)
var (
_ route.Router = &Router{}
mu sync.Mutex
defaultRoute *Router
)
type Config struct {
NamespaceID string
}
type Router struct {
conf *Config
watcher config.Watcher
services atomic.Value
ctx context.Context
cancel context.CancelFunc
}
func DefaultRouter() *Router {
mu.Lock()
defer mu.Unlock()
if defaultRoute == nil {
defaultRoute = New(
&Config{
NamespaceID: env.NamespaceID(),
},
consul.DefaultConsul(),
)
}
return defaultRoute
}
func New(conf *Config, cfg config.Source) *Router {
watcher := cfg.Subscribe(fmt.Sprintf("route/%s/", conf.NamespaceID))
r := &Router{
conf: conf,
watcher: watcher,
services: atomic.Value{},
}
r.ctx, r.cancel = context.WithCancel(context.Background())
go r.refresh()
return r
}
func (r *Router) Select(ctx context.Context, svc naming.Service, nodes []naming.Instance) (selects []naming.Instance) {
if len(nodes) == 0 {
selects = nodes
return
}
if svc.Namespace == "" || svc.Namespace == "local" {
svc.Namespace = env.NamespaceID()
}
services, ok := r.services.Load().(map[naming.Service]RuleGroup)
if !ok {
selects = nodes
return
}
ruleGroup, ok := services[svc]
if !ok || len(ruleGroup.RuleList) == 0 {
selects = nodes
return
}
var hit bool
for _, rule := range ruleGroup.RuleList {
t := rule.toCommonTagRule()
if t.Hit(ctx) {
log.DefaultLog.Debugw("msg", "[route]: hit rule", "svc", svc, "rule", rule)
hit = true
selects = r.matchByRule(rule, nodes)
if len(selects) != 0 {
break
}
} else {
log.DefaultLog.Debugw("msg", "[route]: not hit rule", "svc", svc, "rule", rule)
}
}
if !hit {
selects = nodes
} else if len(selects) == 0 && ruleGroup.FallbackStatus {
selects = nodes
}
return selects
}
func (r *Router) matchByRule(rule Rule, nodes []naming.Instance) []naming.Instance {
var sum int64
candidates := make(map[string]struct {
inss []naming.Instance
weight int64
})
for _, node := range nodes {
for _, dest := range rule.DestList {
match := true
for _, item := range dest.DestItemList {
if node.Metadata[item.DestItemField] != item.DestItemValue {
match = false
}
}
if match {
selects, ok := candidates[dest.DestId]
if !ok {
sum += dest.DestWeight
}
selects.inss = append(selects.inss, node)
selects.weight = dest.DestWeight
candidates[dest.DestId] = selects
}
}
}
if sum == 0 {
return nil
}
cur := rand.Int63n(sum)
for _, dest := range candidates {
sum = sum - dest.weight
if sum <= cur {
return dest.inss
}
}
panic(fmt.Errorf("Route.matchByRule impossible code reached! sum:%d candidates:%v", sum, candidates))
}
func (r *Router) refresh() {
for {
specs, err := r.watcher.Watch(r.ctx)
if err != nil {
if errors.IsGatewayTimeout(err) || errors.IsClientClosed(err) {
log.DefaultLog.Error("msg", "watch route config deadline or clsoe!exit now!", "err", err)
return
}
log.DefaultLog.Errorw("msg", "watch route config failed!", "msg", err)
continue
}
services := make(map[naming.Service]RuleGroup)
for _, spec := range specs {
var ruleGroup []RuleGroup
err = spec.Data.Unmarshal(&ruleGroup)
if err != nil || len(ruleGroup) == 0 {
log.DefaultLog.Errorw("msg", "unmarshal route config failed!", "err", err, "raw", string(spec.Data.Raw()))
continue
}
svc := naming.NewService(ruleGroup[0].NamespaceId, ruleGroup[0].MicroserviceName)
services[svc] = ruleGroup[0]
if ruleGroup[0].NamespaceId != "" && ruleGroup[0].NamespaceId != env.NamespaceID() {
// 拉到的不是本命名空间的,则说明是全局命名空间的
svc := naming.NewService(naming.NsGlobal, ruleGroup[0].MicroserviceName)
services[svc] = ruleGroup[0]
}
}
if len(services) == 0 && err != nil {
log.DefaultLog.Error("get route config failed,not override old data!")
continue
}
log.DefaultLog.Infof("[route] found new route,replace now! services: %v", services)
r.services.Store(services)
}
}
func (r *Router) Close() {
r.cancel()
}