Implement the following plan: NATS API Throughput Improvements: Configurable Semaphores,…
- User
Implement the following plan:
NATS API Throughput Improvements: Configurable Semaphores, Queue Groups, Channel-Based Subscriptions
Context
The NATS API has hardcoded semaphore sizes (8 job, 2 node), uses basic
Subscribe()instead of queue groups, and directly processes messages in NATS callbacks. These limit throughput and scalability.Changes
1. Configurable semaphore sizes —
internal/config/config.goAdd
JobConcurrencyandNodeConcurrencyfields toNATSConfig:gotype NATSConfig struct { SubjectJobEvent string `json:"subject-job-event"` SubjectNodeState string `json:"subject-node-state"` QueueGroup string `json:"queue-group"` JobConcurrency int `json:"job-concurrency"` NodeConcurrency int `json:"node-concurrency"` }Defaults:
JobConcurrency=8,NodeConcurrency=2,QueueGroup=""(empty = no queue group for backward compat).2. Queue groups —
internal/api/nats.goIn
StartSubscriptions(), useSubscribeQueue()whenQueueGroupis configured, otherwise fall back toSubscribe(). This enables horizontal scaling across multiple cc-backend instances.3. Channel-based subscriptions with worker pool —
internal/api/nats.goReplace direct
Subscribe/SubscribeQueuecallback model withSubscribeChan()+ buffered channels + worker goroutines. This:- Adds a Go-side buffer (configurable via semaphore size) between NATS delivery and processing
- Decouples NATS message delivery from processing
- Workers draw from the channel, applying the same semaphore-like concurrency via channel capacity + worker count
Architecture:
codeNATS server → SubscribeChan(buffered chan) → N worker goroutines → process messageThe number of workers =
JobConcurrency/NodeConcurrency. The channel buffer provides additional absorb capacity for bursts.Files to modify
Assistant
Let me start by reading the relevant files to understand the current implementation.
Assistant
Let me check the NATS client interface to understand available methods.
Assistant
Now I have a clear picture. The
SubscribeChandoesn't support queue groups in cc-lib, so I'll use callback-based subscribe that pushes to buffered channels, with worker goroutines consuming from them. This cleanly replaces the semaphore pattern.Let me implement all three changes.
1. Add fields to
NATSConfig:Assistant
2. Rework
nats.go— replace semaphores with channel-based worker pool, add queue group support:Assistant
Now remove the semaphore acquire/release from
handleJobEventandhandleNodeStatesince workers handle concurrency:Assistant
There are still references to the old semaphore fields. Let me check what's on those lines.
Assistant
Good, no more references. The diagnostics must have been stale. Now let me build and test.
Assistant
Build and vet pass. Now run the tests.
Assistant
All tests pass. Here's a summary of the changes:
internal/config/config.go- Added
QueueGroup,JobConcurrency, andNodeConcurrencyfields toNATSConfig
internal/api/nats.go- Replaced semaphore pattern with channel-based worker pool:
jobSem/nodeSemchannels replaced byjobCh/nodeChmessage channels with dedicated worker goroutines - Configurable concurrency:
NewNatsAPI()readsJobConcurrency(default 8) andNodeConcurrency(default 2) from config to set channel buffer size and worker count - Queue group support: New
subscribe()helper usesSubscribeQueue()whenQueueGroupis configured, falls back toSubscribe()otherwise - Architecture: NATS callbacks now push to buffered channels; N worker goroutines per channel consume and process messages, decoupling delivery from processing
- Removed semaphore acquire/release from
handleJobEventandhandleNodeState
- Added
- User
Remove Queue group support again
Assistant
There's a stale reference somewhere. Let me check.
Assistant
No matches — the diagnostic was stale. Let me verify the build.
Assistant
Done. Removed the
QueueGroupfield fromNATSConfigand thesubscribe()helper function.StartSubscriptions()now usesclient.Subscribe()directly.