fix: add reloadable NATS connection with automatic reconnect handling#2987
Merged
migmartri merged 3 commits intochainloop-dev:mainfrom Apr 4, 2026
Merged
Conversation
When NATS pods restart, the control plane loses its connection and does not recover until restarted. This adds a ReloadableConnection wrapper that broadcasts reconnection events to all consumers (caches and audit publisher), allowing them to reinitialize their JetStream handles. The pkg/natsconn package is decoupled from the controlplane proto config so it can be imported by external consumers. Refs: chainloop-dev#2986 Signed-off-by: Miguel Martinez Trivino <miguel@chainloop.dev>
Remove dead js field and sync.RWMutex from AuditLogPublisher since Publish uses core NATS not JetStream. Return cleanup function from natsconn.New so Wire drains the connection on shutdown. Remove redundant WHAT comment. Signed-off-by: Miguel Martinez Trivino <miguel@chainloop.dev>
There was a problem hiding this comment.
1 issue found across 8 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="app/controlplane/cmd/wire.go">
<violation number="1" location="app/controlplane/cmd/wire.go:153">
P2: Reconnect subscriptions use `context.Background()`, so they are never canceled and never unsubscribed. Wire these to a lifecycle-canceled context to avoid lingering subscriber/watcher goroutines.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review, or fix all with cubic.
matiasinsaurralde
approved these changes
Apr 4, 2026
migmartri
commented
Apr 4, 2026
app/controlplane/cmd/wire.go
Outdated
| ) | ||
|
|
||
| func newClaimsCache(conn *nats.Conn, logger log.Logger) (cache.Cache[*jwt.MapClaims], error) { | ||
| func newClaimsCache(rc *natsconn.ReloadableConnection, logger log.Logger) (cache.Cache[*jwt.MapClaims], error) { |
Member
Author
There was a problem hiding this comment.
Would it make sense to provide context in all these initializer?
Pass the application context from main() through wireApp to all NATS reconnect subscribers (caches and audit publisher). When the context is cancelled on shutdown, subscriber channels are closed and watcher goroutines exit cleanly. Signed-off-by: Miguel Martinez Trivino <miguel@chainloop.dev>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
When NATS pods restart, the control plane loses its connection and does not recover until restarted. This adds a
ReloadableConnectionwrapper inpkg/natsconnthat broadcasts reconnection events to all consumers (caches and audit publisher), allowing them to reinitialize their JetStream handles automatically.pkg/natsconnpackage withReloadableConnectiontype providing Subscribe/Broadcast fan-out for reconnect events, decoupled from controlplane proto config for cross-repo importabilityWithReconnectplumbing that was previously unwiredFixes #2986