From 875829ff21931863a5dcb4a9dee762386eb4ac02 Mon Sep 17 00:00:00 2001 From: Andy Xie Date: Mon, 12 Nov 2018 10:19:06 +0800 Subject: [PATCH] backport: https://github.com/nsqio/nsq/pull/1099 --- nsqlookupd/http.go | 8 +++++++- nsqlookupd/registration_db.go | 8 +++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/nsqlookupd/http.go b/nsqlookupd/http.go index 23163a8e2..3cc22ad9f 100644 --- a/nsqlookupd/http.go +++ b/nsqlookupd/http.go @@ -324,6 +324,7 @@ func (s *httpServer) doNodes(w http.ResponseWriter, req *http.Request, ps httpro producers := s.ctx.nsqlookupd.DB.FindProducers("client", "", "").FilterByActive( s.ctx.nsqlookupd.opts.InactiveProducerTimeout, 0) nodes := make([]*node, len(producers)) + topicProducersMap := make(map[string]Producers) for i, p := range producers { topics := s.ctx.nsqlookupd.DB.LookupRegistrations(p.peerInfo.id).Filter("topic", "*", "").Keys() @@ -331,10 +332,15 @@ func (s *httpServer) doNodes(w http.ResponseWriter, req *http.Request, ps httpro // to add tombstone information tombstones := make([]bool, len(topics)) for j, t := range topics { - topicProducers := s.ctx.nsqlookupd.DB.FindProducers("topic", t, "") + if _, exists := topicProducersMap[t]; !exists { + topicProducersMap[t] = s.ctx.nsqlookupd.DB.FindProducers("topic", t, "") + } + + topicProducers := topicProducersMap[t] for _, tp := range topicProducers { if tp.peerInfo == p.peerInfo { tombstones[j] = tp.IsTombstoned(s.ctx.nsqlookupd.opts.TombstoneLifetime) + break } } } diff --git a/nsqlookupd/registration_db.go b/nsqlookupd/registration_db.go index 5e2b09759..39f0fdc32 100644 --- a/nsqlookupd/registration_db.go +++ b/nsqlookupd/registration_db.go @@ -186,7 +186,8 @@ func (r *RegistrationDB) FindProducers(category string, key string, subkey strin return ProducerMap2Slice(r.registrationMap[k]) } - results := make(map[string]*Producer) + results := make(map[string]struct{}) + var retProducers Producers for k, producers := range r.registrationMap { if !k.IsMatch(category, key, subkey) { continue @@ -194,11 +195,12 @@ func (r *RegistrationDB) FindProducers(category string, key string, subkey strin for _, producer := range producers { _, found := results[producer.peerInfo.id] if found == false { - results[producer.peerInfo.id] = producer + results[producer.peerInfo.id] = struct{}{} + retProducers = append(retProducers, producer) } } } - return ProducerMap2Slice(results) + return retProducers } func (r *RegistrationDB) LookupRegistrations(id string) Registrations {