-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolver.go
154 lines (142 loc) · 3.48 KB
/
resolver.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
package http
import (
"context"
"errors"
"net/url"
"strings"
"time"
"github.com/google/uuid"
"github.com/nextmicro/next/internal/endpoint"
"github.com/go-kratos/aegis/subset"
"github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/registry"
"github.com/go-kratos/kratos/v2/selector"
)
// Target is resolver target
type Target struct {
Scheme string
Authority string
Endpoint string
}
func parseTarget(endpoint string, insecure bool) (*Target, error) {
if !strings.Contains(endpoint, "://") {
if insecure {
endpoint = "http://" + endpoint
} else {
endpoint = "https://" + endpoint
}
}
u, err := url.Parse(endpoint)
if err != nil {
return nil, err
}
target := &Target{Scheme: u.Scheme, Authority: u.Host}
if len(u.Path) > 1 {
target.Endpoint = u.Path[1:]
}
return target, nil
}
type resolver struct {
rebalancer selector.Rebalancer
target *Target
watcher registry.Watcher
selecterKey string
subsetSize int
insecure bool
}
func newResolver(ctx context.Context, discovery registry.Discovery, target *Target,
rebalancer selector.Rebalancer, block, insecure bool, subsetSize int,
) (*resolver, error) {
// this is new resolver
watcher, err := discovery.Watch(ctx, target.Endpoint)
if err != nil {
return nil, err
}
r := &resolver{
target: target,
watcher: watcher,
rebalancer: rebalancer,
insecure: insecure,
selecterKey: uuid.New().String(),
subsetSize: subsetSize,
}
if block {
done := make(chan error, 1)
go func() {
for {
services, err := watcher.Next()
if err != nil {
done <- err
return
}
if r.update(services) {
done <- nil
return
}
}
}()
select {
case err := <-done:
if err != nil {
stopErr := watcher.Stop()
if stopErr != nil {
log.Errorf("failed to http client watch stop: %v, error: %+v", target, stopErr)
}
return nil, err
}
case <-ctx.Done():
log.Errorf("http client watch service %v reaching context deadline!", target)
stopErr := watcher.Stop()
if stopErr != nil {
log.Errorf("failed to http client watch stop: %v, error: %+v", target, stopErr)
}
return nil, ctx.Err()
}
}
go func() {
for {
services, err := watcher.Next()
if err != nil {
if errors.Is(err, context.Canceled) {
return
}
log.Errorf("http client watch service %v got unexpected error:=%v", target, err)
time.Sleep(time.Second)
continue
}
r.update(services)
}
}()
return r, nil
}
func (r *resolver) update(services []*registry.ServiceInstance) bool {
filtered := make([]*registry.ServiceInstance, 0, len(services))
for _, ins := range services {
ept, err := endpoint.ParseEndpoint(ins.Endpoints, endpoint.Scheme("http", !r.insecure))
if err != nil {
log.Errorf("Failed to parse (%v) discovery endpoint: %v error %v", r.target, ins.Endpoints, err)
continue
}
if ept == "" {
continue
}
filtered = append(filtered, ins)
}
if r.subsetSize != 0 {
filtered = subset.Subset(r.selecterKey, filtered, r.subsetSize)
}
nodes := make([]selector.Node, 0, len(filtered))
for _, ins := range filtered {
ept, _ := endpoint.ParseEndpoint(ins.Endpoints, endpoint.Scheme("http", !r.insecure))
nodes = append(nodes, selector.NewNode("http", ept, ins))
}
if len(nodes) == 0 {
log.Warnf("[http resolver]Zero endpoint found,refused to write,set: %s ins: %v", r.target.Endpoint, nodes)
return false
}
r.rebalancer.Apply(nodes)
return true
}
func (r *resolver) Close() error {
return r.watcher.Stop()
}