-
Notifications
You must be signed in to change notification settings - Fork 1
/
builder.go
167 lines (143 loc) · 3.98 KB
/
builder.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
package discov
import (
"context"
"errors"
"fmt"
"go.etcd.io/etcd/clientv3"
"google.golang.org/grpc/resolver"
"net"
"sync"
"time"
)
// Builder implements the gRPC resolver.Builder interface.
// It Builds a discov Resolver that implements the gRPC
// resolver.Resolver interface.
type Builder struct {
options *builderOptions
}
type builderOptions struct {
// etcd
etcdClient *clientv3.Client
kvResolver EtcdKvResolver
// dns
dnsPollingInterval time.Duration
}
// BuilderOption is the option that passed to NewBuilder function.
type BuilderOption struct {
applyTo func(*builderOptions)
}
// WithEtcdClient injects an etcd client. The option is required
// when the authority of discov is etcd.
func WithEtcdClient(cli *clientv3.Client) BuilderOption {
return BuilderOption{
applyTo: func(options *builderOptions) {
options.etcdClient = cli
},
}
}
// WithEtcdKvResolver injects a custom EtcdKvResolver implementation
// to determine how the internal etcdResolver watches keys and retrieves addrs.
func WithEtcdKvResolver(r EtcdKvResolver) BuilderOption {
return BuilderOption{
applyTo: func(options *builderOptions) {
options.kvResolver = r
},
}
}
// WithDNSPollingInterval customizes the polling frequency of
// the internal dnsResolver.
func WithDNSPollingInterval(d time.Duration) BuilderOption {
return BuilderOption{
applyTo: func(options *builderOptions) {
options.dnsPollingInterval = d
},
}
}
// NewBuilder returns a Builder that contain the passed options.
func NewBuilder(opts ...BuilderOption) *Builder {
options := new(builderOptions)
for _, option := range opts {
option.applyTo(options)
}
return &Builder{options: options}
}
// Scheme returns the scheme "discov" correspond to the Builder.
func (b *Builder) Scheme() string {
return "discov"
}
// Build creates a new resolver for the given target.
//
// If the authority of target that passed to grpc dial method
// is etcd, Build returns an internal etcdResolver, if the
// authority is dns, returns an internal dnsResolver.
//
// The options passed to NewBuilder will be used in this method.
func (b *Builder) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOptions) (r resolver.Resolver, err error) {
_, authority, endpoint, err := parseTarget(target)
if err != nil {
err = fmt.Errorf("parse target: %v", err)
return
}
if authority == authorityEtcd {
if b.options.etcdClient == nil {
err = fmt.Errorf("the authority in target is %q but missing WithEtcdClient option when NewBuilder",
authority)
return
}
if !isEtcdClientAvailable(b.options.etcdClient) {
err = errors.New("the passed etcd client is unavailable")
return
}
if b.options.kvResolver == nil {
b.options.kvResolver = new(DefaultEtcdKvResolver)
}
ctx, cancel := context.WithCancel(context.Background())
er := &etcdResolver{
cli: b.options.etcdClient,
srv: endpoint,
kvResolver: b.options.kvResolver,
cc: cc,
ctx: ctx,
cancel: cancel,
disableSrvCfg: opts.DisableServiceConfig,
}
er.wg.Add(1)
go er.watcher()
r = er
return
}
if authority == authorityDNS {
var (
dnsName string
port int
)
dnsName, port, err = parseEndpointInDNSTarget(endpoint)
if err != nil {
err = fmt.Errorf("parse endpoint failed: %v", err)
return
}
if b.options.dnsPollingInterval == 0 {
b.options.dnsPollingInterval = time.Second * 30
}
ctx, cancel := context.WithCancel(context.Background())
dr := &dnsResolver{
dnsName: dnsName,
port: port,
resolver: net.DefaultResolver,
pollingInterval: b.options.dnsPollingInterval,
ctx: ctx,
cancel: cancel,
wg: sync.WaitGroup{},
cc: cc,
rn: make(chan struct{}, 1),
disableSrvCfg: opts.DisableServiceConfig,
}
dr.wg.Add(1)
go dr.watcher()
dr.ResolveNow(resolver.ResolveNowOptions{})
r = dr
return
}
err = errUnsupportedAuthorityInTarget
return
}