-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuilder.go
97 lines (80 loc) · 2.3 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
package cloudmap
import (
"context"
"time"
"google.golang.org/grpc/grpclog"
grpcresolver "google.golang.org/grpc/resolver"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/servicediscovery"
)
const (
Scheme = "cloudmap"
HealthStatusFilterAll = servicediscovery.HealthStatusFilterAll
HealthStatusFilterHealthy = servicediscovery.HealthStatusFilterHealthy
HealthStatusFilterUnhealthy = servicediscovery.HealthStatusFilterUnhealthy
)
func init() {
Register()
}
type builder struct {
sess *session.Session // default: session.NewSession()
healthStatusFilter string // default: HEALTHY
maxResults int64 // default: 100
refreshInterval time.Duration // default: 30s
}
// Register builds builder with given opts and register it to the resolver map.
// If you don't give any options, the builder will be registered with default options listed below.
//
// The default builder was already registered by the init function,
// so you don't need to call this function to register the default builder.
//
// Default Options:
//
// Session: session.NewSession()
// HealthStatusFilter: HealthStatusFilterHealthy
// MaxResults: 100
// RefreshInterval: 30s
func Register(opts ...Opt) {
b := &builder{
healthStatusFilter: HealthStatusFilterHealthy,
maxResults: 100,
refreshInterval: 30 * time.Second,
}
for _, opt := range opts {
opt(b)
}
grpcresolver.Register(b)
}
func (b *builder) Scheme() string {
return Scheme
}
func (b *builder) Build(t grpcresolver.Target, cc grpcresolver.ClientConn, _ grpcresolver.BuildOptions) (grpcresolver.Resolver, error) {
cmT, err := parseTarget(t)
if err != nil {
return nil, err
}
sess := b.sess
if sess == nil {
sess, err = session.NewSession()
if err != nil {
return nil, err
}
}
ctx, cancel := context.WithCancel(context.Background())
r := &resolver{
logger: grpclog.Component(b.Scheme()),
cc: cc,
sd: servicediscovery.New(sess),
namespace: cmT.namespace,
service: cmT.service,
healthStatusFilter: b.healthStatusFilter,
maxResults: b.maxResults,
ctx: ctx,
cancel: cancel,
ticker: time.NewTicker(b.refreshInterval),
resolveCmd: make(chan struct{}, 1),
}
r.wg.Add(1)
go r.watcher()
return r, nil
}