// NewSubscriberDetailed is the same as NewSubscriber, but allows users to // provide an explicit lookup refresh ticker instead of a TTL, and specify the // lookup function instead of using net.LookupSRV. func NewSubscriberDetailed( name string, refresh *time.Ticker, lookup Lookup, factory sd.Factory, logger log.Logger, ) *Subscriber { p := &Subscriber{ name: name, cache: cache.New(factory, logger), logger: logger, quit: make(chan struct{}), } instances, err := p.resolve(lookup) if err == nil { logger.Log("name", name, "instances", len(instances)) } else { logger.Log("name", name, "err", err) } p.cache.Update(instances) go p.loop(refresh, lookup) return p }
// NewSubscriber returns a ZooKeeper subscriber. ZooKeeper will start watching // the given path for changes and update the Subscriber endpoints. func NewSubscriber(c Client, path string, factory sd.Factory, logger log.Logger) (*Subscriber, error) { s := &Subscriber{ client: c, path: path, cache: cache.New(factory, logger), logger: logger, quitc: make(chan struct{}), } err := s.client.CreateParentNodes(s.path) if err != nil { return nil, err } instances, eventc, err := s.client.GetEntries(s.path) if err != nil { logger.Log("path", s.path, "msg", "failed to retrieve entries", "err", err) return nil, err } logger.Log("path", s.path, "instances", len(instances)) s.cache.Update(instances) go s.loop(eventc) return s, nil }
// NewSubscriber returns an etcd subscriber. It will start watching the given // prefix for changes, and update the endpoints. func NewSubscriber(c Client, prefix string, factory sd.Factory, logger log.Logger) (*Subscriber, error) { s := &Subscriber{ client: c, prefix: prefix, cache: cache.New(factory, logger), 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(instances) go s.loop() return s, nil }
// NewSubscriber returns a Consul subscriber which returns endpoints for the // requested service. It only returns instances for which all of the passed tags // are present. func NewSubscriber(client Client, factory sd.Factory, logger log.Logger, service string, tags []string, passingOnly bool) *Subscriber { s := &Subscriber{ cache: cache.New(factory, logger), client: client, logger: log.NewContext(logger).With("service", service, "tags", fmt.Sprint(tags)), service: service, tags: tags, passingOnly: passingOnly, quitc: make(chan struct{}), } instances, index, err := s.getInstances(defaultIndex, nil) if err == nil { s.logger.Log("instances", len(instances)) } else { s.logger.Log("err", err) } s.cache.Update(instances) go s.loop(index) return s }