sec: GC empty channels and rate-limit subscribe/unsubscribe - #20
Merged
Conversation
This was referenced May 7, 2026
Closes the architectural parts of #10. The mechanical channel-name length cap landed in the earlier #17 PR. Channel GC: registry.Sweep walks the registry, takes each channel's SubsMu, and if Subscribers is empty, sets channel.Deleted (atomic.Bool) and removes the entry from the registry. SweepLoop runs Sweep on a ticker (default: every 60s) for the lifetime of ctx. The TOCTOU race the previous code-comment warned about is closed by: - hub.Subscribe checks Deleted under the same SubsMu before adding to Subscribers. Returns ErrChannelDeleted if a sweep raced ahead. - handleSubscribe wraps GetOrCreate + hub.Subscribe in a small retry loop; on ErrChannelDeleted it loops, GetOrCreate returns a fresh channel (the deleted one is gone from the registry), and Subscribe succeeds against the fresh channel. Subscribe/unsubscribe rate-limiting: the publish bucket (c.rateLimit.Allow per-key) is now also consumed by handleSubscribe and handleUnsubscribe. A single key spamming control frames hits the existing 100/hour bucket and is RATE_LIMITED_CONTROL'd just like publish-spam. cmd/wirefan/main.go starts go registry.SweepLoop(ctx, reg, DefaultSweepInterval) alongside the existing PublishStatsLoop. Tests: - registry.TestSweepRemovesEmptyChannels - registry.TestSweepLoopHonoursContextCancel - registry.TestSweepDeletedChannelTriggersFreshGetOrCreate
EthanY33
force-pushed
the
sec/channel-gc-and-sub-ratelimit
branch
from
May 7, 2026 23:04
b032fd9 to
a95e7a0
Compare
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
Closes the architectural parts of #10. The mechanical channel-name length cap landed in the earlier PR #17.
Two related fixes:
Channel GC
Empty channels were intentionally retained ("NOTE") because of a TOCTOU race:
Rangeover the registry, observing an empty channel, deleting it, but a concurrentGetOrCreatehad already returned the same channel reference and a subscriber was about to land on it.This PR closes that race with
registry.Sweep:SubsMu. IfSubscribersis empty, setDeleted(atomic.Bool) under the lock, thenDeletefrom the registry.hub.SubscribereadsDeletedunder the sameSubsMubefore adding a subscriber. IfDeletedis true, return new errorErrChannelDeleted.handleSubscribewrapsGetOrCreate+Subscribein a small retry loop (3 attempts). OnErrChannelDeletedit loops; the nextGetOrCreatereturns a fresh channel (the deleted one is no longer in the registry), andSubscribesucceeds on the new one.SweepLoop(ctx, reg, interval)runs fromcmd/wirefan/main.goatDefaultSweepInterval = 60s.Subscribe / unsubscribe rate-limiting
The existing per-API-key publish bucket (
100/hour,200burst) is now also consumed byhandleSubscribeandhandleUnsubscribe. A peer spamming control frames hitsRATE_LIMITEDlike a peer spamming publishes.If 100/hour proves too strict for legitimate subscribe patterns, the bucket parameters are still hardcoded at one site (
cmd/wirefan/main.go) — adjust there.What changed
internal/registry/registry.go:Channel.Deleted atomic.Bool.internal/registry/sweep.go: NEWSweep+SweepLoop+DefaultSweepInterval.internal/registry/sweep_test.go: NEW (3 cases).internal/hub/channel.go: NEWErrChannelDeleted;SubscribechecksDeleted.internal/conn/handler.go: rate-limitsubscribe/unsubscribe; retry loop onErrChannelDeletedinhandleSubscribe; updated comment inhandleUnsubscribe.cmd/wirefan/main.go: startregistry.SweepLoopgoroutine.Test plan
go build ./...clean.go test ./internal/...passes (4 new tests).gh api ...against/metrics—wirefan_channel_countreturns to 0.RATE_LIMITEDafter ~200 ops (current burst).Conflict notes
Touches
cmd/wirefan/main.go— overlaps with PR #15 (resolveAdminToken) and PR #18 (parseFlags). All three changes are additive (different lines). Whichever lands last needs a trivial three-way merge; the other two can land in any order.