Skip to content

nsqd: fix Stats() only returning one pub count when client publishes to multiple topics#1525

Open
troclaux wants to merge 1 commit into
nsqio:masterfrom
troclaux:fix/stats-multi-topic-pubcounts
Open

nsqd: fix Stats() only returning one pub count when client publishes to multiple topics#1525
troclaux wants to merge 1 commit into
nsqio:masterfrom
troclaux:fix/stats-multi-topic-pubcounts

Conversation

@troclaux

Copy link
Copy Markdown

Problem

clientV2.Stats() builds the PubCounts slice by ranging over the internal pubCounts map. A spurious break on line 325 of client_v2.go exits the loop immediately after appending the first matching entry, so only one topic's count is ever returned — even when the client has published to multiple topics.

Reported in #1492.

Before:

# client publishes to topic-a and topic-b
Stats("").PubCounts → [{topic-a 3}]   # topic-b missing

After:

Stats("").PubCounts → [{topic-a 4} {topic-b 7}]

Fix

Remove the break. The existing continue already handles the topicName filter; the break serves no purpose and was causing the data loss.

Test

Added TestStatsMultiTopicPubCounts to nsqd/stats_test.go which:

  1. Creates a client and calls PublishedMessage for two different topics
  2. Asserts both topics and their cumulative counts appear in Stats("")

@troclaux troclaux force-pushed the fix/stats-multi-topic-pubcounts branch from 75c7c37 to 6e28916 Compare May 10, 2026 23:56
…to multiple topics

clientV2.Stats() builds a PubCounts slice by ranging over the pubCounts
map. A spurious break statement exited the loop after appending the first
entry, so only a single topic's count was ever recorded regardless of how
many topics the client had published to.

Remove the break so the loop collects every matching entry.

Fixes nsqio#1492
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant