Skip to content

Skip live packet formatting when the WebSocket hub is empty - #2511

Merged
efiten merged 3 commits into
Kpa-clawbot:masterfrom
n30nex:codex/beacon-port-20261129
Dec 1, 2026
Merged

efiten merged 3 commits into
Kpa-clawbot:masterfrom
n30nex:codex/beacon-port-20261129

Conversation

@n30nex

@n30nex n30nex commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #2504. Related to #1672.

With no WebSocket listeners, the poller now skips building and encoding live packet frames. SQLite polling, late observations, relay indexes and analytics caches continue to update. A client joining between the transmission and observation reads can receive that tick’s late observations. Connected clients retain the existing wire format.

This independently adapts Beacon’s subscriber demand check to CoreScope’s poller.

Eight alternating runs of 30 SQLite batches each used 32 new transmissions and 32 late observations per batch on one Windows machine with one Go processor.

Listeners Median time before → after Bytes/batch before → after Allocations/batch before → after
None 6.739 → 3.802 ms 666,643 → 173,309 11,303 → 4,169
One 5.753 → 5.917 ms 666,635 → 661,717 11,303 → 11,291

Idle batches allocated 74% fewer bytes. The connected client received all 64 frames. In a real browser, both sessions received new and late observations across a disconnect and reconnect. The three regressions, related integration tests, race tests and complete server suite passed locally. Tests before the fix

Adapt the demand check from MeshCore-Beacon/beacon-server commit dd79ca5ca11a329aa734022edd3253d02381b23b to CoreScope's single packet format. Sample listeners after reading each batch, and keep store updates independent of outgoing frames.

Fixes Kpa-clawbot#2504.
@n30nex

n30nex commented Nov 29, 2026

Copy link
Copy Markdown
Contributor Author

Kent Beck Gate: PASS (test quality) at 102f57c8a. The poller test uses real SQLite rows, including observations arriving after their transmission. It checks the stored records, both reconnects, frame contents and a client joining between the two reads. The benchmark verifies all 64 frames reach the connected client, alongside the idle allocation measurement.

@n30nex
n30nex marked this pull request as ready for review November 29, 2026 18:09

@zfouts zfouts left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed at 102f57c8. Approving.

  • The store still updates when no client is connected: the relay, path-hop and distance indexes, pickBestObservation and the evidence updates all run. Only the per-observation broadcast maps are skipped.
  • The new cache-invalidation gate (len(broadcastTxs)>0 && inv.hasNewObservations) matches the old len(result)>0, since result had one entry per observation of each new transmission.
  • I checked the race with a client connecting just after the count is sampled. It misses that tick's frames, and nothing is ever partial (the maps are built whole or not at all). Nothing downstream depends on the skipped work: Hub.Broadcast is the only consumer. A client joining mid-stream already sees this on master.
  • ClientCount takes only hub.mu briefly, and no store lock is held while waiting on the hub.
  • -race on TestBroadcast*, TestPoller* and BenchmarkPollerLiveFrames passes. The benchmark shows 340 KB / 4,021 allocs per op with no clients against 794 KB / 7,015 with one, matching the PR's claim.

Nits:

  1. cmd/server/store.go:3283: "The relay indexes above also need the path when the feed is idle" sits directly on the branch that skips the stash when idle, so at first it reads backwards. Something like "the path above is computed regardless; this copy is only kept for frames" would be clearer.
  2. cmd/server/websocket.go:177: with the early return, a marshal error is no longer logged when nobody is connected. It doesn't matter in practice.

@efiten
efiten merged commit 144937c into Kpa-clawbot:master Dec 1, 2026
17 checks passed
zfouts added a commit to zfouts/CoreScope that referenced this pull request Dec 1, 2026
Conflict resolutions:
- cmd/server/store.go ingestNewFromDB: keep both. Each transmission is
  still redacted once (redactLocatedAdvert); master's per-observation
  observedFrameEvidence (Kpa-clawbot#2497) runs on the observation's own frame
  before it is dropped, so it reads exact bytes but keeps only the hop
  width and reception kind.
- cmd/server/store.go nodeZeroHopRegionCounts: take master's version,
  which moved the frame checks into StoreObs.receivedDirectAdvert. Its
  legacy-singleton fallback read packetpath.AdvertRouteEvidence(tx.RawHex),
  which is empty for a located advert under privacy, and the evidence
  needs the payload length, so frameHead cannot supply it. hiddenFrame now
  keeps routeEvidence (computed from the whole frame at redaction) and
  receivedDirectAdvert reads tx.frameRouteEvidence(). Test:
  TestRedactZeroHopLegacySingletonParity.

Follow-ups for master's changes:
- TestLocationLeakRoutes: classify GET /api/analytics/observer-comparison
  (Kpa-clawbot#2492). It returns observer IDs, names, reception counts and SNR
  deltas, no position.
- packet_redact_frames_test.go: ingestNewFromDB takes a clientCount (Kpa-clawbot#2511).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
zfouts added a commit to zfouts/CoreScope that referenced this pull request Dec 2, 2026
… and the advert reception changes to Postgres

Brings in 10 master commits: Kpa-clawbot#2366 (catalogue key tier), Kpa-clawbot#2497 (regions
count only received adverts), Kpa-clawbot#2498 (archived foreign-advert flags), Kpa-clawbot#2492
(observer comparison), Kpa-clawbot#2511, Kpa-clawbot#2513, Kpa-clawbot#2514, Kpa-clawbot#2517, Kpa-clawbot#2518, Kpa-clawbot#2519.

Conflicts:
- cmd/ingestor/approved_channels.go: both sides added a mutex to
  channelKeySet. One mu now serialises refresh, master's setCatalog and the
  branch's Close, and guards the users.db/Postgres accounts readers as well
  as master's approvedNames and catalogue tier.
- cmd/ingestor/db.go, nodes upsert: master's foreign_advert carry-over on
  top of the branch's qualified columns and lastSeenClampSQL. Master looked
  up the archived flag with ?1, which dbx.Rebind turns into $11 on Postgres;
  it is now a plain ? bound to the public key again, and the EXISTS is
  wrapped in CASE so Postgres stores 0/1 in the BIGINT column.
- cmd/ingestor/db.go, MoveStaleNodes: master's UPDATE that restores an
  archived foreign flag runs before the branch's portable moveStaleNodesSQL
  (not master's INSERT OR REPLACE ... SELECT *), with IS DISTINCT FROM 1
  instead of the SQLite-only IS NOT 1.

Ported to Postgres:
- catalog_keys (Kpa-clawbot#2366): pgschema migration 0004_catalog_keys.sql (same
  columns, CHECKs, defaults and key; LatestVersion 4), the table in
  sqlite_endstate.sql, and the worker's upsert sets updated_at from
  excluded.updated_at instead of strftime('%s','now').
- corescope-migrate copies catalog_keys and verify never prints
  catalog_keys.key_hex (channel secrets), for any store.
- GetNodes region filter (Kpa-clawbot#2497) and the observer comparison (Kpa-clawbot#2492) used
  IS NOT 'tx'; both use IS DISTINCT FROM 'tx'. The comparison's
  ORDER BY label COLLATE NOCASE is lower(<label expression>) on Postgres,
  which folds ASCII only under COLLATE "C", as NOCASE does.
- docs/catalogue-keys.md: the revert SQL on PostgreSQL.

Tests on both backends:
- The catalogue key tests that use a store and the Kpa-clawbot#2498 foreign-advert
  tests run on SQLite and Postgres (onBothStores; insertTx uses RETURNING).
- New dual tests for the observer comparison and the GetNodes region filter.
- pgschema: an upgrade from version 3 with data; the control migration tests
  no longer assume 3 is the latest version.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

Skip live packet presentation work when the WebSocket has no listeners

3 participants