Manually fix up other publishers and consumers
Peter Bourgon
8 years ago
63 | 63 | } |
64 | 64 | |
65 | 65 | func factory(ctx context.Context, qps int) loadbalancer.Factory { |
66 | return func(instance string) (endpoint.Endpoint, error) { | |
66 | return func(instance string) (endpoint.Endpoint, loadbalancer.Closer, error) { | |
67 | 67 | var e endpoint.Endpoint |
68 | 68 | e = makeUppercaseProxy(ctx, instance) |
69 | 69 | e = circuitbreaker.Gobreaker(gobreaker.NewCircuitBreaker(gobreaker.Settings{}))(e) |
70 | 70 | e = kitratelimit.NewTokenBucketLimiter(jujuratelimit.NewBucketWithRate(float64(qps), int64(qps)))(e) |
71 | return e, nil | |
71 | return e, make(loadbalancer.Closer), nil | |
72 | 72 | } |
73 | 73 | } |
74 | 74 |
21 | 21 | // NewPublisher returs a etcd publisher. Etcd will start watching the given |
22 | 22 | // prefix for changes and update the Publisher endpoints. |
23 | 23 | func NewPublisher(c Client, prefix string, f loadbalancer.Factory, logger log.Logger) (*Publisher, error) { |
24 | logger = log.NewContext(logger).With("component", "Etcd Publisher") | |
25 | ||
24 | logger = log.NewContext(logger).With("component", "etcd Publisher") | |
26 | 25 | p := &Publisher{ |
27 | 26 | client: c, |
28 | 27 | prefix: prefix, |
31 | 30 | endpoints: make(chan []endpoint.Endpoint), |
32 | 31 | quit: make(chan struct{}), |
33 | 32 | } |
34 | ||
35 | 33 | entries, err := p.client.GetEntries(prefix) |
36 | 34 | if err != nil { |
37 | 35 | return nil, err |
40 | 38 | return p, nil |
41 | 39 | } |
42 | 40 | |
43 | func (p *Publisher) loop(endpoints []endpoint.Endpoint) { | |
41 | func (p *Publisher) loop(endpoints map[string]endpointCloser) { | |
44 | 42 | responseChan := make(chan *etcd.Response) |
45 | 43 | go p.client.WatchPrefix(p.prefix, responseChan) |
46 | ||
47 | 44 | for { |
48 | 45 | select { |
49 | case p.endpoints <- endpoints: | |
46 | case p.endpoints <- flatten(endpoints): | |
50 | 47 | |
51 | 48 | case <-responseChan: |
52 | 49 | entries, err := p.client.GetEntries(p.prefix) |
54 | 51 | p.logger.Log("msg", "failed to retrieve entries", "err", err) |
55 | 52 | continue |
56 | 53 | } |
57 | endpoints = makeEndpoints(entries, p.factory, p.logger) | |
54 | endpoints = migrate(endpoints, makeEndpoints(entries, p.factory, p.logger)) | |
58 | 55 | |
59 | 56 | case <-p.quit: |
60 | 57 | return |
77 | 74 | close(p.quit) |
78 | 75 | } |
79 | 76 | |
80 | func makeEndpoints(addrs []string, f loadbalancer.Factory, logger log.Logger) []endpoint.Endpoint { | |
81 | endpoints := make([]endpoint.Endpoint, 0, len(addrs)) | |
82 | ||
77 | func makeEndpoints(addrs []string, f loadbalancer.Factory, logger log.Logger) map[string]endpointCloser { | |
78 | m := make(map[string]endpointCloser, len(addrs)) | |
83 | 79 | for _, addr := range addrs { |
84 | endpoint, err := f(addr) | |
80 | if _, ok := m[addr]; ok { | |
81 | continue // duplicate | |
82 | } | |
83 | endpoint, closer, err := f(addr) | |
85 | 84 | if err != nil { |
86 | 85 | logger.Log("instance", addr, "err", err) |
87 | 86 | continue |
88 | 87 | } |
89 | endpoints = append(endpoints, endpoint) | |
88 | m[addr] = endpointCloser{endpoint, closer} | |
90 | 89 | } |
91 | return endpoints | |
90 | return m | |
92 | 91 | } |
92 | ||
93 | type endpointCloser struct { | |
94 | endpoint.Endpoint | |
95 | loadbalancer.Closer | |
96 | } | |
97 | ||
98 | func migrate(prev, curr map[string]endpointCloser) map[string]endpointCloser { | |
99 | for instance, ec := range prev { | |
100 | if _, ok := curr[instance]; !ok { | |
101 | close(ec.Closer) | |
102 | } | |
103 | } | |
104 | return curr | |
105 | } | |
106 | ||
107 | func flatten(m map[string]endpointCloser) []endpoint.Endpoint { | |
108 | a := make([]endpoint.Endpoint, 0, len(m)) | |
109 | for _, ec := range m { | |
110 | a = append(a, ec.Endpoint) | |
111 | } | |
112 | return a | |
113 | } |
31 | 31 | e = func(context.Context, interface{}) (interface{}, error) { return struct{}{}, nil } |
32 | 32 | ) |
33 | 33 | |
34 | factory := func(instance string) (endpoint.Endpoint, error) { | |
35 | return e, nil | |
34 | factory := func(string) (endpoint.Endpoint, loadbalancer.Closer, error) { | |
35 | return e, make(loadbalancer.Closer), nil | |
36 | 36 | } |
37 | 37 | |
38 | 38 | client := &fakeClient{ |
51 | 51 | } |
52 | 52 | |
53 | 53 | func TestBadFactory(t *testing.T) { |
54 | var ( | |
55 | logger = log.NewNopLogger() | |
56 | e = func(context.Context, interface{}) (interface{}, error) { return struct{}{}, nil } | |
57 | ) | |
54 | logger := log.NewNopLogger() | |
58 | 55 | |
59 | factory := func(instance string) (endpoint.Endpoint, error) { | |
60 | return e, errors.New("_") | |
56 | factory := func(string) (endpoint.Endpoint, loadbalancer.Closer, error) { | |
57 | return nil, nil, errors.New("kaboom") | |
61 | 58 | } |
62 | 59 | |
63 | 60 | client := &fakeClient{ |
81 | 78 | } |
82 | 79 | |
83 | 80 | func TestPublisherStoppped(t *testing.T) { |
84 | var ( | |
85 | logger = log.NewNopLogger() | |
86 | e = func(context.Context, interface{}) (interface{}, error) { return struct{}{}, nil } | |
87 | ) | |
81 | logger := log.NewNopLogger() | |
88 | 82 | |
89 | factory := func(instance string) (endpoint.Endpoint, error) { | |
90 | return e, errors.New("_") | |
83 | factory := func(string) (endpoint.Endpoint, loadbalancer.Closer, error) { | |
84 | return nil, nil, errors.New("kaboom") | |
91 | 85 | } |
92 | 86 | |
93 | 87 | client := &fakeClient{ |
7 | 7 | ) |
8 | 8 | |
9 | 9 | // Publisher yields a set of static endpoints as produced by the passed factory. |
10 | type Publisher struct{ *fixed.Publisher } | |
10 | type Publisher struct{ publisher *fixed.Publisher } | |
11 | 11 | |
12 | 12 | // NewPublisher returns a static endpoint Publisher. |
13 | 13 | func NewPublisher(instances []string, factory loadbalancer.Factory, logger log.Logger) Publisher { |
14 | 14 | logger = log.NewContext(logger).With("component", "Fixed Publisher") |
15 | 15 | endpoints := []endpoint.Endpoint{} |
16 | 16 | for _, instance := range instances { |
17 | e, err := factory(instance) | |
17 | e, _, err := factory(instance) // never close | |
18 | 18 | if err != nil { |
19 | 19 | _ = logger.Log("instance", instance, "err", err) |
20 | 20 | continue |
21 | 21 | } |
22 | 22 | endpoints = append(endpoints, e) |
23 | 23 | } |
24 | return Publisher{fixed.NewPublisher(endpoints)} | |
24 | return Publisher{publisher: fixed.NewPublisher(endpoints)} | |
25 | 25 | } |
26 | ||
27 | // Endpoints implements Publisher. | |
28 | func (p Publisher) Endpoints() ([]endpoint.Endpoint, error) { | |
29 | return p.publisher.Endpoints() | |
30 | } |
6 | 6 | "golang.org/x/net/context" |
7 | 7 | |
8 | 8 | "github.com/go-kit/kit/endpoint" |
9 | "github.com/go-kit/kit/loadbalancer" | |
9 | 10 | "github.com/go-kit/kit/loadbalancer/static" |
10 | 11 | "github.com/go-kit/kit/log" |
11 | 12 | ) |
18 | 19 | "bar": func(context.Context, interface{}) (interface{}, error) { return struct{}{}, nil }, |
19 | 20 | "baz": func(context.Context, interface{}) (interface{}, error) { return struct{}{}, nil }, |
20 | 21 | } |
21 | factory = func(instance string) (endpoint.Endpoint, error) { | |
22 | factory = func(instance string) (endpoint.Endpoint, loadbalancer.Closer, error) { | |
22 | 23 | if e, ok := endpoints[instance]; ok { |
23 | return e, nil | |
24 | return e, make(loadbalancer.Closer), nil | |
24 | 25 | } |
25 | return nil, fmt.Errorf("%s: not found", instance) | |
26 | return nil, nil, fmt.Errorf("%s: not found", instance) | |
26 | 27 | } |
27 | 28 | ) |
28 | 29 | p := static.NewPublisher(instances, factory, log.NewNopLogger()) |