forked from st3v/go-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
label.go
169 lines (136 loc) · 3.3 KB
/
label.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
// Package label is a priority label based selector.
package label
import (
"context"
"sync"
"github.com/micro/go-micro/cmd"
"github.com/micro/go-micro/registry"
"github.com/micro/go-micro/selector"
)
/*
A priority based label selector. Rather than just returning nodes with specific labels
this selector orders the nodes based on a list of labels. If no labels match all the
nodes are still returned. The priority based label selector is useful for such things
as rudimentary AZ based routing where requests made to other services should remain
in the same AZ.
*/
type labelSelector struct {
so selector.Options
}
func init() {
cmd.DefaultSelectors["label"] = NewSelector
}
func prioritise(nodes []*registry.Node, labels []label) []*registry.Node {
var lnodes []*registry.Node
marked := make(map[string]bool)
for _, label := range labels {
for _, node := range nodes {
// already used
if _, ok := marked[node.Id]; ok {
continue
}
// nil metadata?
if node.Metadata == nil {
continue
}
// matching label?
if val, ok := node.Metadata[label.key]; !ok || label.val != val {
continue
}
// matched! mark it
marked[node.Id] = true
// append to nodes
lnodes = append(lnodes, node)
}
}
// grab the leftovers
for _, node := range nodes {
if _, ok := marked[node.Id]; ok {
continue
}
lnodes = append(lnodes, node)
}
return lnodes
}
func next(nodes []*registry.Node) func() (*registry.Node, error) {
var i int
var mtx sync.Mutex
return func() (*registry.Node, error) {
mtx.Lock()
if i >= len(nodes) {
i = 0
}
node := nodes[i]
i++
mtx.Unlock()
return node, nil
}
}
func (r *labelSelector) Init(opts ...selector.Option) error {
for _, o := range opts {
o(&r.so)
}
return nil
}
func (r *labelSelector) Options() selector.Options {
return r.so
}
func (r *labelSelector) Select(service string, opts ...selector.SelectOption) (selector.Next, error) {
var sopts selector.SelectOptions
for _, opt := range opts {
opt(&sopts)
}
// get the service
services, err := r.so.Registry.GetService(service)
if err != nil {
return nil, err
}
// apply the filters
for _, filter := range sopts.Filters {
services = filter(services)
}
// if there's nothing left, return
if len(services) == 0 {
return nil, selector.ErrNotFound
}
var nodes []*registry.Node
// flatten node list
for _, service := range services {
for _, node := range service.Nodes {
nodes = append(nodes, node)
}
}
// any nodes left?
if len(nodes) == 0 {
return nil, selector.ErrNotFound
}
// now prioritise the list based on labels
// oh god the O(n)^2 cruft or well not really
// more like O(m*n) or something like that
if labels, ok := r.so.Context.Value(labelKey{}).([]label); ok {
nodes = prioritise(nodes, labels)
}
return next(nodes), nil
}
func (r *labelSelector) Mark(service string, node *registry.Node, err error) {
return
}
func (r *labelSelector) Reset(service string) {
return
}
func (r *labelSelector) Close() error {
return nil
}
func (r *labelSelector) String() string {
return "label"
}
func NewSelector(opts ...selector.Option) selector.Selector {
sopts := selector.Options{
Context: context.TODO(),
Registry: registry.DefaultRegistry,
}
for _, opt := range opts {
opt(&sopts)
}
return &labelSelector{sopts}
}