Skip to content

Log broadcasts once per event instead of once per subscriber (#73)#1

Closed
russ wants to merge 1 commit into
masterfrom
fix-73-log-broadcast-once
Closed

Log broadcasts once per event instead of once per subscriber (#73)#1
russ wants to merge 1 commit into
masterfrom
fix-73-log-broadcast-once

Conversation

@russ

@russ russ commented May 29, 2026

Copy link
Copy Markdown
Owner

Problem

Fixes cable-cr#73 (originally surfaced in production via the JoystickTV report).

Cable::Server#send_to_channels logged the "transmitting" line inside the per-subscriber loop:

@channels[channel_identifier].each do |channel|
  ...
  Cable::Logger.info { "#{channel.class} transmitting #{parsed_message} (via streamed from #{channel.stream_identifier})" }
  channel.connection.socket.send(...)
end

So broadcasting one message to a stream with N connected clients produced N identical log lines. A chat room with 100 viewers logged "hi" 100 times; the only workaround was Cable::Logger.level = :none, which silences all Cable logging.

Change

Move the log outside the loop and emit a single line per broadcast event, including the number of subscribers it was delivered to:

Transmitting {"message" => "hi"} to 13 subscribers (via streamed from chat_room_1)
  • Counts only live connections actually transmitted to (closed connections are still reaped in the loop as before).
  • Skips the log entirely when there are no live subscribers.

The redundant per-message broadcast is still logged once at the call site (Channel#broadcast / .broadcast_to), so no information is lost.

Tests

Added spec/cable/server_spec.cr#send_to_channels — subscribes two connections to the same stream, captures logs, and asserts exactly one info line (... to 2 subscribers ...) is emitted. Full suite green (52 examples, 0 failures); ameba clean.

Note

This changes the log line format (<Class> transmitting <msg> (via streamed from <id>)Transmitting <msg> to <n> subscribers (via streamed from <id>)). Anything grepping the old string will need updating.

🤖 Generated with Claude Code

…able-cr#73

`Cable::Server#send_to_channels` logged a "transmitting" line inside the
per-subscriber loop, so a single broadcast to a stream with N connected
clients produced N identical log lines. A busy chat room could emit
dozens of duplicate entries for one message, flooding production logs.

Move the log outside the loop and emit a single line per broadcast that
includes the number of subscribers it was delivered to. Skips logging
entirely when there are no live subscribers.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Reduces log noise by emitting a single "Transmitting ... to N subscribers" line per broadcast event instead of one line per subscriber in Cable::Server#send_to_channels.

Changes:

  • Move the info log out of the per-subscriber loop and aggregate a transmitted count of live deliveries.
  • Skip the log entirely when no live subscribers received the message; pluralize "subscriber(s)" based on count.
  • Add a spec capturing logs to assert exactly one info line is emitted for a broadcast with two subscribers.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/cable/server.cr Track transmitted count and log a single aggregated info line after the fan-out loop.
spec/cable/server_spec.cr New #send_to_channels spec verifies one info log per broadcast using log/spec capture.

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

@russ russ marked this pull request as ready for review May 29, 2026 22:17
@russ

russ commented May 29, 2026

Copy link
Copy Markdown
Owner Author

Superseded by cable-cr#107 — opened against the upstream repo instead.

@russ russ closed this May 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Logging issues

2 participants