0 | 0 |
package dnssrv
|
1 | 1 |
|
2 | 2 |
import (
|
|
3 |
"errors"
|
3 | 4 |
"fmt"
|
4 | 5 |
"net"
|
5 | 6 |
"time"
|
|
8 | 9 |
"github.com/go-kit/kit/sd"
|
9 | 10 |
"github.com/go-kit/kit/sd/internal/instance"
|
10 | 11 |
)
|
|
12 |
|
|
13 |
// ErrPortZero is returned by the resolve machinery
|
|
14 |
// when a DNS resolver returns an SRV record with its
|
|
15 |
// port set to zero.
|
|
16 |
var ErrPortZero = errors.New("resolver returned SRV record with port 0")
|
11 | 17 |
|
12 | 18 |
// Instancer yields instances from the named DNS SRV record. The name is
|
13 | 19 |
// resolved on a fixed schedule. Priorities and weights are ignored.
|
|
56 | 62 |
}
|
57 | 63 |
|
58 | 64 |
// Stop terminates the Instancer.
|
59 | |
func (p *Instancer) Stop() {
|
60 | |
close(p.quit)
|
|
65 |
func (in *Instancer) Stop() {
|
|
66 |
close(in.quit)
|
61 | 67 |
}
|
62 | 68 |
|
63 | |
func (p *Instancer) loop(t *time.Ticker, lookup Lookup) {
|
|
69 |
func (in *Instancer) loop(t *time.Ticker, lookup Lookup) {
|
64 | 70 |
defer t.Stop()
|
65 | 71 |
for {
|
66 | 72 |
select {
|
67 | 73 |
case <-t.C:
|
68 | |
instances, err := p.resolve(lookup)
|
|
74 |
instances, err := in.resolve(lookup)
|
69 | 75 |
if err != nil {
|
70 | |
p.logger.Log("name", p.name, "err", err)
|
71 | |
p.cache.Update(sd.Event{Err: err})
|
|
76 |
in.logger.Log("name", in.name, "err", err)
|
|
77 |
in.cache.Update(sd.Event{Err: err})
|
72 | 78 |
continue // don't replace potentially-good with bad
|
73 | 79 |
}
|
74 | |
p.cache.Update(sd.Event{Instances: instances})
|
|
80 |
in.cache.Update(sd.Event{Instances: instances})
|
75 | 81 |
|
76 | |
case <-p.quit:
|
|
82 |
case <-in.quit:
|
77 | 83 |
return
|
78 | 84 |
}
|
79 | 85 |
}
|
80 | 86 |
}
|
81 | 87 |
|
82 | |
func (p *Instancer) resolve(lookup Lookup) ([]string, error) {
|
83 | |
_, addrs, err := lookup("", "", p.name)
|
|
88 |
func (in *Instancer) resolve(lookup Lookup) ([]string, error) {
|
|
89 |
_, addrs, err := lookup("", "", in.name)
|
84 | 90 |
if err != nil {
|
85 | 91 |
return nil, err
|
86 | 92 |
}
|
87 | 93 |
instances := make([]string, len(addrs))
|
88 | 94 |
for i, addr := range addrs {
|
|
95 |
if addr.Port == 0 {
|
|
96 |
return nil, ErrPortZero
|
|
97 |
}
|
89 | 98 |
instances[i] = net.JoinHostPort(addr.Target, fmt.Sprint(addr.Port))
|
90 | 99 |
}
|
91 | 100 |
return instances, nil
|
92 | 101 |
}
|
93 | 102 |
|
94 | 103 |
// Register implements Instancer.
|
95 | |
func (s *Instancer) Register(ch chan<- sd.Event) {
|
96 | |
s.cache.Register(ch)
|
|
104 |
func (in *Instancer) Register(ch chan<- sd.Event) {
|
|
105 |
in.cache.Register(ch)
|
97 | 106 |
}
|
98 | 107 |
|
99 | 108 |
// Deregister implements Instancer.
|
100 | |
func (s *Instancer) Deregister(ch chan<- sd.Event) {
|
101 | |
s.cache.Deregister(ch)
|
|
109 |
func (in *Instancer) Deregister(ch chan<- sd.Event) {
|
|
110 |
in.cache.Deregister(ch)
|
102 | 111 |
}
|