Skip to content

fix(nacos): restrict registry fetch to privileged agent only#13236

Open
Baoyuantop wants to merge 1 commit intoapache:masterfrom
Baoyuantop:fix/nacos-privileged-agent-only
Open

fix(nacos): restrict registry fetch to privileged agent only#13236
Baoyuantop wants to merge 1 commit intoapache:masterfrom
Baoyuantop:fix/nacos-privileged-agent-only

Conversation

@Baoyuantop
Copy link
Copy Markdown
Contributor

Description

All other discovery modules (tars, kubernetes, consul) restrict service data fetching to the privileged agent process only. The nacos module was missing this restriction, causing every worker process to independently start timers and fetch from nacos servers.

This results in:

  • N worker processes × redundant nacos API requests
  • N × unnecessary JSON encoding/decoding overhead
  • Unnecessary load on nacos servers

This PR adds the same process.type() ~= "privileged agent" check used by other discovery modules, so only the privileged agent fetches from nacos while workers read from the shared dict populated by the agent.

Reference: This is a minimal fix extracted from the optimization discussed in #12867, adapted to the current codebase.

Which issue(s) this PR fixes:

Related to #12867

Checklist

  • I have explained the need for this PR and the problem it solves
  • I have explained the changes or the new features added to this PR
  • I have added tests corresponding to this change
  • I have updated the documentation to reflect this change
  • I have verified that this change is backward compatible (If not, please discuss on the APISIX mailing list first)

All other discovery modules (tars, kubernetes, consul) only fetch
service data in the privileged agent process. The nacos module was
missing this restriction, causing every worker process to
independently fetch from nacos servers, resulting in N-times
redundant API requests and JSON parsing overhead.

Add the same process.type() check used by other discovery modules
so only the privileged agent fetches from nacos, while workers
read from the shared dict populated by the agent.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@dosubot dosubot Bot added size:XS This PR changes 0-9 lines, ignoring generated files. bug Something isn't working labels Apr 17, 2026
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Restricts Nacos service discovery fetching to the privileged agent process (matching behavior of other discovery modules) to prevent every worker from starting timers and issuing redundant Nacos API requests.

Changes:

  • Add ngx.process usage to detect process type in the Nacos discovery module.
  • Short-circuit init_worker() unless running in the "privileged agent" process, so only that process performs periodic Nacos registry fetches.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +295 to +297
if process.type() ~= "privileged agent" then
return
end
Copy link

Copilot AI Apr 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new privileged-agent guard changes runtime behavior (workers no longer fetch/refresh Nacos data themselves). There are existing Nacos discovery tests under t/discovery/, but none assert that fetching happens only in the privileged agent (or that workers don’t start the fetch timer). Please add a regression test that fails if non-privileged workers perform Nacos fetches (e.g., by asserting request counts/log lines or by using worker_processes > 1 and verifying only one periodic fetch).

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants