-
Notifications
You must be signed in to change notification settings - Fork 549
perf: bound NATS user-sync KV work and cut /sub plus usage-job CPU #893
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
85e5f3e
feat(user): enhance user inbound tag retrieval and subscription updat…
x0sina ce739e2
fix: discard stale subscription updates and align FinalMask test (#886)
DrSaeedHub 327afcc
fix: read flushed subscription updates from a fresh snapshot (#888)
DrSaeedHub 03c8b5f
perf(nats): optimize user sync and bound queue processing (#892)
DrSaeedHub 533bd69
Merge branch 'dev' into pref-boost
x0sina bb9c348
test(hosts): match FinalMask noise fixture to Xray array packet
x0sina fbd57c7
fix(cleanup): add timeout handling for node-sync key compaction and i…
x0sina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| """Compact completed node-sync work on the scheduler leader only.""" | ||
|
|
||
| import asyncio | ||
|
|
||
| from app import scheduler | ||
| from app.nats import needs_shared_bridge_memory | ||
| from app.nats.client import create_nats_client, get_jetstream_context | ||
| from app.nats.kv_cleanup import compact_deleted_keys | ||
| from app.nats.leader import is_job_leader | ||
| from app.utils.logger import get_logger | ||
| from config import nats_settings, runtime_settings | ||
|
|
||
| logger = get_logger("jobs") | ||
|
|
||
|
|
||
| async def cleanup_node_sync(): | ||
| if not is_job_leader(): | ||
| return | ||
| nc = await create_nats_client() | ||
| if nc is None: | ||
| return | ||
| try: | ||
| async with asyncio.timeout(240): | ||
| count = await compact_deleted_keys(await get_jetstream_context(nc), nats_settings.node_user_sync_kv_bucket) | ||
| if count: | ||
| logger.info("Compacted %s completed node-sync keys", count) | ||
| except TimeoutError: | ||
| logger.warning("node-sync key compaction timed out; retrying on the next interval") | ||
| finally: | ||
| await nc.close() | ||
|
|
||
|
|
||
| if runtime_settings.role.runs_node and needs_shared_bridge_memory(): | ||
| scheduler.add_job( | ||
| cleanup_node_sync, | ||
| "interval", | ||
| seconds=300, | ||
| max_instances=1, | ||
| coalesce=True, | ||
| id="cleanup_node_sync", | ||
| replace_existing=True, | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
Bound the compaction run with a timeout.
KvWatcherlimits eachfetchcall to five seconds, but it retries afterTimeoutErrorwhilenum_pendingremains nonzero. The snapshot can therefore run without an overall deadline.purge_streamuses nats-py’s five-second JetStream API timeout, so each purge is bounded individually. However,compact_deleted_keyscan still spend an unbounded total time processing purges. Withmax_instances=1, APScheduler skips overlapping runs, andcoalesce=Truemerges missed runs. A stalled compaction can therefore prevent later runs from starting and allow tombstones to accumulate.🔧 Proposed change
📝 Committable suggestion
🤖 Prompt for AI Agents