4 "github.com/go-kit/kit/log"
5 "github.com/go-kit/kit/sd"
6 "github.com/go-kit/kit/sd/internal/instance"
9 // Instancer yields instances stored in a certain etcd keyspace. Any kind of
10 // change in that keyspace is watched and will update the Instancer's Instancers.
11 type Instancer struct {
19 // NewInstancer returns an etcd instancer. It will start watching the given
20 // prefix for changes, and update the subscribers.
21 func NewInstancer(c Client, prefix string, logger log.Logger) (*Instancer, error) {
25 cache: instance.NewCache(),
27 quitc: make(chan struct{}),
30 instances, err := s.client.GetEntries(s.prefix)
32 logger.Log("prefix", s.prefix, "instances", len(instances))
34 logger.Log("prefix", s.prefix, "err", err)
36 s.cache.Update(sd.Event{Instances: instances, Err: err})
42 func (s *Instancer) loop() {
43 ch := make(chan struct{})
44 go s.client.WatchPrefix(s.prefix, ch)
48 instances, err := s.client.GetEntries(s.prefix)
50 s.logger.Log("msg", "failed to retrieve entries", "err", err)
51 s.cache.Update(sd.Event{Err: err})
54 s.cache.Update(sd.Event{Instances: instances})
62 // Stop terminates the Instancer.
63 func (s *Instancer) Stop() {
67 // Register implements Instancer.
68 func (s *Instancer) Register(ch chan<- sd.Event) {
72 // Deregister implements Instancer.
73 func (s *Instancer) Deregister(ch chan<- sd.Event) {
74 s.cache.Deregister(ch)