示例#1
0
文件: data.go 项目: CliffYuan/nsq
// GetNSQDTopics returns a []string containing all the topics produced by the given nsqd
func (c *ClusterInfo) GetNSQDTopics(nsqdHTTPAddrs []string) ([]string, error) {
	var topics []string
	var lock sync.Mutex
	var wg sync.WaitGroup
	var errs []error

	type respType struct {
		Topics []struct {
			Name string `json:"topic_name"`
		} `json:"topics"`
	}

	for _, addr := range nsqdHTTPAddrs {
		wg.Add(1)
		go func(addr string) {
			defer wg.Done()

			endpoint := fmt.Sprintf("http://%s/stats?format=json", addr)
			c.logf("CI: querying nsqd %s", endpoint)

			var resp respType
			err := c.client.NegotiateV1(endpoint, &resp)
			if err != nil {
				lock.Lock()
				errs = append(errs, err)
				lock.Unlock()
				return
			}

			lock.Lock()
			defer lock.Unlock()
			for _, topic := range resp.Topics {
				topics = stringy.Add(topics, topic.Name)
			}
		}(addr)
	}
	wg.Wait()

	if len(errs) == len(nsqdHTTPAddrs) {
		return nil, fmt.Errorf("Failed to query any nsqd: %s", ErrList(errs))
	}

	sort.Strings(topics)

	if len(errs) > 0 {
		return topics, ErrList(errs)
	}
	return topics, nil
}
示例#2
0
文件: data.go 项目: handsomesun/nsq
// GetNSQDTopics returns a []string containing all the topics produced by the given nsqd
func (c *ClusterInfo) GetNSQDTopics(nsqdHTTPAddrs []string) ([]string, error) {
	var success bool
	var topics []string
	var lock sync.Mutex
	var wg sync.WaitGroup

	type respType struct {
		Topics []struct {
			Name string `json:"topic_name"`
		} `json:"topics"`
	}

	for _, addr := range nsqdHTTPAddrs {
		wg.Add(1)
		endpoint := fmt.Sprintf("http://%s/stats?format=json", addr)
		c.logf("NSQD: querying %s", endpoint)
		go func(endpoint string) {
			defer wg.Done()

			var resp respType
			err := c.client.NegotiateV1(endpoint, &resp)
			if err != nil {
				c.logf("ERROR: lookupd %s - %s", endpoint, err)
				return
			}

			lock.Lock()
			defer lock.Unlock()
			success = true
			for _, topic := range resp.Topics {
				topics = stringy.Add(topics, topic.Name)
			}
		}(endpoint)
	}
	wg.Wait()

	if success == false {
		return nil, errors.New("unable to query any nsqd")
	}

	sort.Strings(topics)
	return topics, nil
}