Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
77 lines (65 sloc) 1.71 KB
package etcdv3
import (
// Instancer yields instances stored in a certain etcd keyspace. Any kind of
// change in that keyspace is watched and will update the Instancer's Instancers.
type Instancer struct {
cache *instance.Cache
client Client
prefix string
logger log.Logger
quitc chan struct{}
// NewInstancer returns an etcd instancer. It will start watching the given
// prefix for changes, and update the subscribers.
func NewInstancer(c Client, prefix string, logger log.Logger) (*Instancer, error) {
s := &Instancer{
client: c,
prefix: prefix,
cache: instance.NewCache(),
logger: logger,
quitc: make(chan struct{}),
instances, err := s.client.GetEntries(s.prefix)
if err == nil {
logger.Log("prefix", s.prefix, "instances", len(instances))
} else {
logger.Log("prefix", s.prefix, "err", err)
s.cache.Update(sd.Event{Instances: instances, Err: err})
go s.loop()
return s, nil
func (s *Instancer) loop() {
ch := make(chan struct{})
go s.client.WatchPrefix(s.prefix, ch)
for {
select {
case <-ch:
instances, err := s.client.GetEntries(s.prefix)
if err != nil {
s.logger.Log("msg", "failed to retrieve entries", "err", err)
s.cache.Update(sd.Event{Err: err})
s.cache.Update(sd.Event{Instances: instances})
case <-s.quitc:
// Stop terminates the Instancer.
func (s *Instancer) Stop() {
// Register implements Instancer.
func (s *Instancer) Register(ch chan<- sd.Event) {
// Deregister implements Instancer.
func (s *Instancer) Deregister(ch chan<- sd.Event) {
You can’t perform that action at this time.