gozargah-node icon indicating copy to clipboard operation
gozargah-node copied to clipboard

Preserve recent node logs and fan out log streams per subscriber

Open W0lfD opened this issue 6 months ago • 3 comments

Summary

This PR changes node log streaming from a live-only/shared-consumer model to a bounded fan-out log stream.

The node now keeps the latest log lines in an in-memory ring buffer. When a Logs client subscribes, it receives the buffered recent lines first and then continues receiving live updates. Each subscriber has its own stream, so multiple open panels no longer compete for log lines.

Problem

Before this change, the Logs page only showed lines received while the current stream was connected. This caused a few confusing and inconvenient behaviors:

  • Refreshing the Logs page lost all previously displayed lines
  • If no new Xray log was produced, the page could look like it was still loading or disconnected
  • Temporary panel disconnects during proxy/network testing caused missed log lines
  • Multiple open Logs pages could compete for lines from the same stream

This made the logs less useful exactly during the setup/debugging flow where they are most needed

Changes

  • Added a reusable bounded logstream.Buffer
  • Store recent node log lines in memory
  • Changed backend log access to a subscription model
  • New subscribers receive a snapshot of recent buffered lines before live updates
  • Fan out live log lines to all active subscribers
  • Updated REST and gRPC log streaming to use independent subscriptions
  • Added/updated tests for buffer behavior and log streaming behavior

Validation performed:

  • make test
  • manual testing with a fully running panel + node setup

Summary by CodeRabbit

  • Refactor
    • Log streaming API updated: Logs() method replaced with context-aware SubscribeLogs() for improved cancellation handling
    • Enhanced multi-subscriber log buffering for better resource management during concurrent log access
    • Log resources now properly cleaned up during service shutdown

W0lfD avatar Jun 04 '26 13:06 W0lfD

Review in Change Stack →

[!IMPORTANT]

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 3e5d8b95-cf64-4771-b1b0-34d10e7988aa

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • [ ] 🔍 Trigger review

Walkthrough

This PR refactors backend log streaming from simple unbuffered channels to a context-aware buffered subscription model. A new logstream.Buffer provides concurrent ring-buffering with multi-subscriber fan-out, replay of recent log lines, and context-driven lifecycle management. The Backend interface method signature changes from Logs() to SubscribeLogs(context.Context), and WireGuard, XRay, and controller implementations are updated accordingly.

Changes

Log Subscription Refactoring

Layer / File(s) Summary
Interface contract and Buffer abstraction
backend/backend.go, backend/logstream/buffer.go, backend/logstream/buffer_test.go
Backend interface replaces Logs() with SubscribeLogs(context.Context). New logstream.Buffer struct maintains a fixed-size ring buffer with mutex-protected access, publishes log lines to all registered subscribers via non-blocking sends, subscribers receive a snapshot of buffered lines upon subscribe and then live lines, and context cancellation or buffer close triggers channel closure and cleanup. Three comprehensive tests verify tail replay across multiple subscribers, non-blocking behavior on slow subscribers, and proper closure semantics.
WireGuard log refactoring
backend/wireguard/wireguard.go, backend/wireguard/log.go, backend/wireguard/wireguard_lifecycle_test.go
WireGuard struct field changes from logChan chan string to logs *logstream.Buffer. Initialization creates the buffer from configuration size. New SubscribeLogs(ctx) method returns subscriptions via buffer or a closed channel if nil. emitLogLocked publishes lines via logs.Publish() instead of non-blocking channel send. Shutdown closes and nils the buffer. Lifecycle tests are updated to subscribe via SubscribeLogs and verify startup/shutdown log messages and buffer cleanup.
XRay log refactoring
backend/xray/core.go, backend/xray/log.go, backend/xray/xray.go, backend/xray/xray_test.go
XRay Core struct field changes from logsChan to logs *logstream.Buffer, initialized in NewXRayCore. New SubscribeLogs(ctx) and CloseLogs() methods expose subscription and cleanup. Log capture functions captureStartupLogLine and captureRuntimeLogLine publish to buffer via logs.Publish(). Xray wrapper adds SubscribeLogs(ctx) that safely delegates to core's subscription, returning a closed channel when core is nil, and shutdown calls CloseLogs(). Test is updated to use the new subscription API.
Controller integration
controller/rest/log.go, controller/rpc/log.go
REST and RPC log handlers call Backend().SubscribeLogs(r.Context()) and Backend().SubscribeLogs(stream.Context()) respectively, binding log stream subscriptions to request/RPC lifetimes instead of using a static backend channel.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 8.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: introducing log preservation in a ring buffer and converting to a fan-out subscription model instead of a shared channel.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


✨ Finishing Touches
🧪 Generate unit tests (beta)
  • [ ] Create a new PR


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

🐰 Logs now flow through buffered streams so fine,
Context-bound subscriptions keep them in line,
Ring buffers replay the tail so dear,
Multiple subscribers need not fear!
WireGuard and XRay both sing their tune,
Request contexts close them just in tune. 🐇✨

Comment @coderabbitai help to get the list of available commands.

coderabbitai[bot] avatar Jun 04 '26 13:06 coderabbitai[bot]

this should be in pkg directory not backend, this is not a backend

M03ED avatar Jun 04 '26 13:06 M03ED

backend/logstream/buffer.go creates each subscriber channel with capacity equal to the replay buffer size, then fills it completely with the snapshot before registering the subscriber. If the buffer is full, the new subscriber starts with a full channel. Any Publish that happens before the REST/RPC handler drains at least one replayed line will hit the nonblocking default path and drop that live log for this newly connected client. For a “replay tail, then stream live logs” API, that creates a connection-time loss window.

M03ED avatar Jun 04 '26 13:06 M03ED

Thanks a lot for this, @W0lfD! Concurrent log viewers splitting the lines between them and streams hanging after a restart were real problems, and your fan-out buffer fixes both. Since the PR had been quiet for a while, we finished it on your branch:

  • merged dev and retargeted the PR to dev, where node work lands now (one conflict in controller/rpc/log.go; the controller test stub now implements SubscribeLogs),
  • each subscriber channel now has room for the replayed tail plus a full ring of live lines, so lines published while a new viewer is still reading the replay are no longer dropped (with a test),
  • moved the package to pkg/logstream, as asked in review.

Thanks again!

T3ST3ST3R0N avatar Oct 12 '26 17:10 T3ST3ST3R0N