forked from go-kit/kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
publisher.go
174 lines (143 loc) · 3.57 KB
/
publisher.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
package consul
import (
"fmt"
"strings"
consul "github.com/hashicorp/consul/api"
"github.com/go-kit/kit/endpoint"
"github.com/go-kit/kit/loadbalancer"
"github.com/go-kit/kit/log"
)
const defaultIndex = 0
// Publisher yields endpoints for a service in Consul. Updates to the service
// are watched and will update the Publisher endpoints.
type Publisher struct {
cache *loadbalancer.EndpointCache
client Client
logger log.Logger
service string
tags []string
endpointsc chan []endpoint.Endpoint
quitc chan struct{}
}
// NewPublisher returns a Consul publisher which returns Endpoints for the
// requested service. It only returns instances for which all of the passed
// tags are present.
func NewPublisher(
client Client,
factory loadbalancer.Factory,
logger log.Logger,
service string,
tags ...string,
) (*Publisher, error) {
p := &Publisher{
cache: loadbalancer.NewEndpointCache(factory, logger),
client: client,
logger: logger,
service: service,
tags: tags,
quitc: make(chan struct{}),
}
instances, index, err := p.getInstances(defaultIndex)
if err == nil {
logger.Log("service", service, "tags", strings.Join(tags, ", "), "instances", len(instances))
} else {
logger.Log("service", service, "tags", strings.Join(tags, ", "), "err", err)
}
p.cache.Replace(instances)
go p.loop(index)
return p, nil
}
// Endpoints implements the Publisher interface.
func (p *Publisher) Endpoints() ([]endpoint.Endpoint, error) {
return p.cache.Endpoints()
}
// Stop terminates the publisher.
func (p *Publisher) Stop() {
close(p.quitc)
}
func (p *Publisher) loop(lastIndex uint64) {
var (
errc = make(chan error, 1)
resc = make(chan response, 1)
)
for {
go func() {
instances, index, err := p.getInstances(lastIndex)
if err != nil {
errc <- err
return
}
resc <- response{
index: index,
instances: instances,
}
}()
select {
case err := <-errc:
p.logger.Log("service", p.service, "err", err)
case res := <-resc:
p.cache.Replace(res.instances)
lastIndex = res.index
case <-p.quitc:
return
}
}
}
func (p *Publisher) getInstances(lastIndex uint64) ([]string, uint64, error) {
tag := ""
if len(p.tags) > 0 {
tag = p.tags[0]
}
entries, meta, err := p.client.Service(
p.service,
tag,
&consul.QueryOptions{
WaitIndex: lastIndex,
},
)
if err != nil {
return nil, 0, err
}
// If more than one tag is passed we need to filter it in the publisher until
// Consul supports multiple tags[0].
//
// [0] https://github.com/hashicorp/consul/issues/294
if len(p.tags) > 1 {
entries = filterEntries(entries, p.tags[1:]...)
}
return makeInstances(entries), meta.LastIndex, nil
}
// response is used as container to transport instances as well as the updated
// index.
type response struct {
index uint64
instances []string
}
func filterEntries(entries []*consul.ServiceEntry, tags ...string) []*consul.ServiceEntry {
var es []*consul.ServiceEntry
ENTRIES:
for _, entry := range entries {
ts := make(map[string]struct{}, len(entry.Service.Tags))
for _, tag := range entry.Service.Tags {
ts[tag] = struct{}{}
}
for _, tag := range tags {
if _, ok := ts[tag]; !ok {
continue ENTRIES
}
}
es = append(es, entry)
}
return es
}
func makeInstances(entries []*consul.ServiceEntry) []string {
instances := make([]string, len(entries))
for i, entry := range entries {
addr := entry.Node.Address
if entry.Service.Address != "" {
addr = entry.Service.Address
}
instances[i] = fmt.Sprintf("%s:%d", addr, entry.Service.Port)
}
return instances
}